這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
09 |
* user : example@example.com login smtp server user |
10 |
* password: xxxxx login smtp server password |
11 |
* host: smtp.example.com:port smtp.163.com:25 |
12 |
* to: example@example.com;example1@163.com;example2@sina.com.cn;... |
13 |
* subject:The subject of mail |
14 |
* body: The content of mail |
15 |
* mailtyoe: mail type html or text |
19 |
func SendMail(user, password, host, to, subject, body, mailtype string) error{ |
20 |
hp := strings.Split(host, ":" ) |
21 |
auth := smtp.PlainAuth( "" , user, password, hp[0]) |
22 |
var content_type string |
23 |
if mailtype == "html" { |
24 |
content_type = "Content-Type: text/" + mailtype + "; charset=UTF-8" |
26 |
content_type = "Content-Type: text/plain" + "; charset=UTF-8" |
29 |
msg := []byte( "To: " + to + "\r\nFrom: " + user + "<" + user + ">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body) |
30 |
send_to := strings.Split(to, ";" ) |
31 |
err := smtp.SendMail(host, auth, user, send_to, msg) |
36 |
user := "xxxx@163.com" |
38 |
host := "smtp.163.com:25" |
39 |
to := "xxxx@gmail.com;ssssss@gmail.com" |
41 |
subject := "Test send email by golang" |
47 |
"Test send email by golang" |
52 |
fmt.Println( "send email" ) |
53 |
err := SendMail(user, password, host, to, subject, body, "html" ) |
55 |
fmt.Println( "send mail error!" ) |
58 |
fmt.Println( "send mail success!" ) |