以騰訊QQ企業郵箱來做系統報警郵件的收發,用Go來做郵件發送的客戶端。郵件能正常發送和接收但是QQ郵箱上有個系統提示“真實發送地址和宣稱的發件人地址不一致”,在foxmail客戶端表現的是類似於 "pengpengzhou on behalf of pengpengzhou"。
QQ郵箱提示:
Go源碼:
package main
import (
"fmt"
"net/smtp"
"strings"
)
func sendEmail(subject string, body string) {
user := "[email protected]"
password := "alFLikeFiHh&*60"
host := "smtp.exmail.qq.com:25"
to := "[email protected]"
hp := strings.Split(host, ":")
auth := smtp.PlainAuth("", user, password, hp[0])
content_type := "Content-Type: text/plain;charset=UTF-8"
msg := []byte("To: " + to + "\r\nFrom: " + 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)
if err != nil {
fmt.Println("Send mail error!")
fmt.Println(err)
} else {
fmt.Println("Send mail success!")
}
}
這段代碼是在《GO使用SMTP發送郵件》基礎上改的,經排查問題出在">\r\nSubject: "裏的“>”是多餘的,把這個">"去掉就正常了。原因是這個">"會導致服務器接收到的“From”字段多出來一個">"