package main
import(
"fmt"
"net/smtp"
"encoding/base64"
)
//html,plain
func SendMail( title,user,pswd,smtpserver,port,from,to,subject,body,format string ) error {
bs64 := base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
header := make(map[string]string)
header["From"] = title+"<"+from+">"
header["To"] = to
header["Subject"] = fmt.Sprintf("=?UTF-8?B?%s?=", bs64.EncodeToString([]byte(subject)))
header["MIME-Version"] = "1.0"
header["Content-Type"] = "text/"+format+"; charset=UTF-8"
header["Content-Transfer-Encoding"] = "base64"
data := ""
for k, v := range header {
data += k+": "+v+"\r\n"
}
data += "\r\n" + bs64.EncodeToString([]byte(body))
err := smtp.SendMail( smtpserver+":"+port,smtp.PlainAuth("",user,pswd,smtpserver),from,[]string{to},[]byte(data) )
return err
}
func main(){
title := "DeepData"
from := "info@deepdata.cn"
to := "123456@qq.com"
subject := "TEST SUBJECT"
body := "深数据 deepdata.cn"
smtpserver := "smtp.mxhichina.com"
pswd := "***password***"
err := SendMail( title,from,pswd,smtpserver,"25",from,to,subject,body,"plain" )
fmt.Println( err )
}
有疑问加站长微信联系(非本文作者)