GO使用SMTP发送邮件
0412 · · 48390 次点击 · · 开始浏览核心代码:smtp.SendMail(host, auth, user, send_to, msg),auth := smtp.PlainAuth("", user, password, hp[0])
可行方案:使用企业邮箱的SMTP发送邮件,host :="smtp.exmail.qq.com:25",因此需要一个企业邮箱的账号和密码
直接上代码:
main.go
package main
import (
"fmt"
"net/smtp"
"strings"
)
func SendToMail(user, password, host, to, subject, body, mailtype string) error {
hp := strings.Split(host, ":")
auth := smtp.PlainAuth("", user, password, hp[0])
var content_type string
if mailtype == "html" {
content_type = "Content-Type: text/" + mailtype + "; charset=UTF-8"
} else {
content_type = "Content-Type: text/plain" + "; charset=UTF-8"
}
msg := []byte("To: " + to + "\r\nFrom: " + user + ">\r\nSubject: " + "\r\n" + content_type + "\r\n\r\n" + body)
send_to := strings.Split(to, ";")
err := smtp.SendMail(host, auth, user, send_to, msg)
return err
}
func main() {
user := "yang**@yun*.com"
password := "***"
host := "smtp.exmail.qq.com:25"
to := "397685131@qq.com"
subject := "使用Golang发送邮件"
body := `
<html>
<body>
<h3>
"Test send to email"
</h3>
</body>
</html>
`
fmt.Println("send email")
err := SendToMail(user, password, host, to, subject, body, "html")
if err != nil {
fmt.Println("Send mail error!")
fmt.Println(err)
} else {
fmt.Println("Send mail success!")
}
}
遇到的问题:
1、body := ``,这里用的是反单引号,而非单引号;
2、err != nil,golang的nil在概念上和其它语言的null、None、nil、NULL一样,都指代零值或空值。nil是预先说明的标识符,也即通常意义上的关键字。在golang中,nil只能赋值给指针、channel、func、interface、map或slice类型的变量。如果未遵循这个规则,则会引发panic。对此官方有明确的说明:http://pkg.golang.org/pkg/builtin/#Type
3、参数auth,auth := smtp.PlainAuth("", user, password, hp[0])
4、强制类型转换,msg := []byte("To: " + to + "\r\nFrom: " + user + ">\r\nSubject: " + "\r\n" + content_type + "\r\n\r\n" + body)
5、多个邮箱:send_to := strings.Split(to, ";")
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
核心代码:smtp.SendMail(host, auth, user, send_to, msg),auth := smtp.PlainAuth("", user, password, hp[0])
可行方案:使用企业邮箱的SMTP发送邮件,host :="smtp.exmail.qq.com:25",因此需要一个企业邮箱的账号和密码
直接上代码:
main.go
package main
import (
"fmt"
"net/smtp"
"strings"
)
func SendToMail(user, password, host, to, subject, body, mailtype string) error {
hp := strings.Split(host, ":")
auth := smtp.PlainAuth("", user, password, hp[0])
var content_type string
if mailtype == "html" {
content_type = "Content-Type: text/" + mailtype + "; charset=UTF-8"
} else {
content_type = "Content-Type: text/plain" + "; charset=UTF-8"
}
msg := []byte("To: " + to + "\r\nFrom: " + user + ">\r\nSubject: " + "\r\n" + content_type + "\r\n\r\n" + body)
send_to := strings.Split(to, ";")
err := smtp.SendMail(host, auth, user, send_to, msg)
return err
}
func main() {
user := "yang**@yun*.com"
password := "***"
host := "smtp.exmail.qq.com:25"
to := "397685131@qq.com"
subject := "使用Golang发送邮件"
body := `
<html>
<body>
<h3>
"Test send to email"
</h3>
</body>
</html>
`
fmt.Println("send email")
err := SendToMail(user, password, host, to, subject, body, "html")
if err != nil {
fmt.Println("Send mail error!")
fmt.Println(err)
} else {
fmt.Println("Send mail success!")
}
}
遇到的问题:
1、body := ``,这里用的是反单引号,而非单引号;
2、err != nil,golang的nil在概念上和其它语言的null、None、nil、NULL一样,都指代零值或空值。nil是预先说明的标识符,也即通常意义上的关键字。在golang中,nil只能赋值给指针、channel、func、interface、map或slice类型的变量。如果未遵循这个规则,则会引发panic。对此官方有明确的说明:http://pkg.golang.org/pkg/builtin/#Type
3、参数auth,auth := smtp.PlainAuth("", user, password, hp[0])
4、强制类型转换,msg := []byte("To: " + to + "\r\nFrom: " + user + ">\r\nSubject: " + "\r\n" + content_type + "\r\n\r\n" + body)
5、多个邮箱:send_to := strings.Split(to, ";")