社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
一、实现功能
用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 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 + "rnFrom: " + user + "<"+ user +">rnSubject: " + subject + "rn" + content_type + "rnrn" + 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)JetBrainsGogland 171.3780.106binrunnerw.exe" D:/Gobingo.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 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 + "rnFrom: " + user + "<"+ user +">rnSubject: " + subject + "rn" + content_type + "rnrn" + 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!")
}
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!