The example in this article describes how the go language sends mail through SMTP. Share to everyone for your reference. The implementation methods are as follows:
Copy Code code as follows:
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 + "\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: = "xxxx@163.com"
Password: = "xxxx"
Host: = "smtp.163.com:25"
To: = "xxxx@gmail.com;ssssss@gmail.com"
Subject: = "Test send email by Golang"
Body: = '
<body>
"Test Send email by Golang"
</body>
`
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!")
}
}
I hope this article will help you with your go language program.