分享
golang发邮件
zb_liang · · 4490 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
01
package main
02
import (
03
"net/smtp"
04
"fmt"
05
"strings"
06
)
07
08
/*
09
* user : example@example.com login smtp server user
10
* password: xxxxx login smtp server password
11
* host: smtp.example.com:port smtp.163.com:25
12
* to: example@example.com;example1@163.com;example2@sina.com.cn;...
13
* subject:The subject of mail
14
* body: The content of mail
15
* mailtyoe: mail type html or text
16
*/
17
18
19
func SendMail(user, password, host, to, subject, body, mailtype string) error{
20
hp := strings.Split(host,
":")
21
auth := smtp.PlainAuth("", user, password, hp[0])
22
var content_type string
23
if
mailtype == "html"
{
24
content_type =
"Content-Type: text/"+ mailtype +
"; charset=UTF-8"
25
}else{
26
content_type =
"Content-Type: text/plain"
+ "; charset=UTF-8"
27
}
28
29
msg := []byte("To: "
+ to + "\r\nFrom: "
+ user + "<"+ user +">\r\nSubject: "
+ subject + "\r\n"
+ content_type + "\r\n\r\n"
+ body)
30
send_to := strings.Split(to,
";")
31
err := smtp.SendMail(host, auth, user, send_to, msg)
32
return
err
33
}
34
35
func main() {
36
user :=
"xxxx@163.com"
37
password :=
"xxxx"
38
host :=
"smtp.163.com:25"
39
to :=
"xxxx@gmail.com;ssssss@gmail.com"
40
41
subject :=
"Test send email by golang"
42
43
body := `
44
<html>
45
<body>
46
<h3>
47
"Test send email by golang"
48
</h3>
49
</body>
50
</html>
51
`
52
fmt.Println("send email")
53
err := SendMail(user, password, host, to, subject, body,
"html")
54
if
err != nil {
55
fmt.Println("send mail error!")
56
fmt.Println(err)
57
}else{
58
fmt.Println("send mail success!")
59
}
60
61
}
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信4490 次点击
上一篇:golang 文件按行读取
下一篇:golang编程之时间编程
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
01
package main
02
import (
03
"net/smtp"
04
"fmt"
05
"strings"
06
)
07
08
/*
09
* user : example@example.com login smtp server user
10
* password: xxxxx login smtp server password
11
* host: smtp.example.com:port smtp.163.com:25
12
* to: example@example.com;example1@163.com;example2@sina.com.cn;...
13
* subject:The subject of mail
14
* body: The content of mail
15
* mailtyoe: mail type html or text
16
*/
17
18
19
func SendMail(user, password, host, to, subject, body, mailtype string) error{
20
hp := strings.Split(host,
":")
21
auth := smtp.PlainAuth("", user, password, hp[0])
22
var content_type string
23
if
mailtype == "html"
{
24
content_type =
"Content-Type: text/"+ mailtype +
"; charset=UTF-8"
25
}else{
26
content_type =
"Content-Type: text/plain"
+ "; charset=UTF-8"
27
}
28
29
msg := []byte("To: "
+ to + "\r\nFrom: "
+ user + "<"+ user +">\r\nSubject: "
+ subject + "\r\n"
+ content_type + "\r\n\r\n"
+ body)
30
send_to := strings.Split(to,
";")
31
err := smtp.SendMail(host, auth, user, send_to, msg)
32
return
err
33
}
34
35
func main() {
36
user :=
"xxxx@163.com"
37
password :=
"xxxx"
38
host :=
"smtp.163.com:25"
39
to :=
"xxxx@gmail.com;ssssss@gmail.com"
40
41
subject :=
"Test send email by golang"
42
43
body := `
44
<html>
45
<body>
46
<h3>
47
"Test send email by golang"
48
</h3>
49
</body>
50
</html>
51
`
52
fmt.Println("send email")
53
err := SendMail(user, password, host, to, subject, body,
"html")
54
if
err != nil {
55
fmt.Println("send mail error!")
56
fmt.Println(err)
57
}else{
58
fmt.Println("send mail success!")
59
}
60
61
}