本文主要是介绍【go语言发送电子邮件】go语言版发送电子邮件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、实现功能
用go语言发送一封邮件
二、实现源代码
package main
import ("net/smtp""fmt""strings"
)/** user : example@example.com login smtp server user* password: xxxxx login smtp server password* host: smtp.example.com:port smtp.163.com:25* to: example@example.com;example1@163.com;example2@sina.com.cn;...* subject:The subject of mail* body: The content of mail* mailtyoe: mail type html or text*/func SendMail(user, password, host, to, subject, body, mailtype string) error{hp := strings.Split(host, ":")auth := smtp.PlainAuth("", user, password, hp[0])var content_type stringif 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 + "<"+ user +">\r\nSubject: " + subject + "\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 := "此处填写qq邮箱账号"password := "此处填写qq邮箱授权码"host := "smtp.qq.com:587"to := "18720081236m@sina.cn"subject := "Test send email by golang"body := `
`fmt.Println("send email")err := SendMail(user, password, host, to, subject, body, "text")if err != nil {fmt.Println("send mail error!")fmt.Println(err)}else{fmt.Println("send mail success!")}}
运行效果
"D:\Program Files (x86)\JetBrains\Gogland 171.3780.106\bin\runnerw.exe" D:/Go\bin\go.exe run D:/Go/code/src/awesomeProject/go_email.go
send email
send mail success!Process finished with exit code 0
版本2(发送多个人 ,HTML格式):
package main
import ("net/smtp""fmt""strings"
)/** user : example@example.com login smtp server user* password: xxxxx login smtp server password* host: smtp.example.com:port smtp.163.com:25* to: example@example.com;example1@163.com;example2@sina.com.cn;...* subject:The subject of mail* body: The content of mail* mailtyoe: mail type html or text*/func SendMail(user, password, host, to, subject, body, mailtype string) error{hp := strings.Split(host, ":")auth := smtp.PlainAuth("", user, password, hp[0])var content_type stringif 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 + "<"+ user +">\r\nSubject: " + subject + "\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 := "1973536419@qq.com"password := "XXXXXXXX"host := "smtp.qq.com:587"to := "defa.lai@cgtz.com;fang.chen@lg-finance.com";subject := "Test send email by golang"body := `<!DOCTYPE html><html><body><div style="text-align:center;width:78.78%;padding: 8px; line-height: 1.42857; vertical-align: top; border-top-width: 1px; border-top-color: rgb(221, 221, 221); background-color: #28a745;color:#fff"><strong>用户工资数据报表</strong></div><table border="1" style="width:80%;"><tr style="background-color:pink;text-align:center;"><th>月份</th><th>存款</th><th>工资</th><th>年薪</th></tr><tr style="text-align:center;"><td>一月</td><td>1000 元</td><td>3000 元</td><td>12000元</td></tr><tr style="text-align:center;"><td>二月</td><td>1500 元</td><td>4000 元</td><td>16000 元</td></tr></table></body></html>`fmt.Println("send email")err := SendMail(user, password, host, to, subject, body, "html")if err != nil {fmt.Println("send mail error!")fmt.Println(err)}else{fmt.Println("send mail success!")}}
这篇关于【go语言发送电子邮件】go语言版发送电子邮件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!