package mainimport ( "fmt" "net/smtp" "strings")//发送邮件的逻辑函数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" //smtp地址及端口 host := "smtp.163.com:25" //接收者,内容可重复,多个邮箱之间用;隔开 to := "xxxxxx@qq.com" //邮件主题 subject := "测试通过golang发送邮件" //邮件内容 text := "XXX你好!" body := `"测试通过golang发送邮件"` + text + `
` // fmt.Println("send email") //执行逻辑函数 err := SendMail(user, password, host, to, subject, body, "html") if err != nil { fmt.Println("发送邮件失败!") fmt.Println(err) } else { fmt.Println("发送邮件成功!") }}