一、創建Gmail應用專用密碼
官網教程
1.開啓兩步驗證
2.創建應用專用密碼入口
創建是app選customer,(注意不要選mail,否則就變成登錄mail的密碼,而不是代碼發送郵件的密碼)
二、實現
//這裏的body支持html的格式
func SendToMail(form, to, subject, body string) error {
mailContent := fmt.Sprintf("To:%v\r\n"+
"Subject: %v\r\n"+
"Content-Type: text/html; charset=UTF-8\r\n\r\n "+
"%v", to, subject, body)
message := []byte(mailContent)
host := "smtp.gmail.com:587"
user := "發送的郵箱"
password := "發送郵箱的密碼"
glog.Debug("host,user,password",host,user,password)
hp := strings.Split(host, ":")
auth := smtp.PlainAuth("", user, password, hp[0])
send_to := strings.Split(to, ";")
return smtp.SendMail(host, auth, form, send_to, message)
}
三、SMTP協議
查詢域名的命令 nslookup