package main
import (
"github.com/jordan-wright/email"
"net/smtp"
"net/textproto"
)
func main() {
e := &email.Email {
To: []string{"8888@qq.com"},
From: "8888@qq.com",
Subject: "Email Send Test 8888",
Text: []byte("Text Body is, of course, supported!"),
HTML: []byte("<h1>This a test email</h1>"),
Headers: textproto.MIMEHeader{},
}
//QQ邮箱465端口用授权码,587端口用密码
err := e.Send("smtp.exmail.qq.com:587", smtp.PlainAuth("", "8888@qq.com", "8888", "smtp.exmail.qq.com"))
if err !=nil {
panic(err)
}
print("邮件发送成功")
}
package main
import (
"gopkg.in/gomail.v2"
"strconv"
"fmt"
)
func SendMail(mailTo []string,subject string, body string ) error {
//定义邮箱服务器连接信息,如果是阿里邮箱 pass填密码,qq邮箱填授权码
mailConn := map[string]string {
"user": "8888@qq.com",
"pass": "8888",
"host": "smtp.exmail.qq.com",
"port": "465",
}
port, _ := strconv.Atoi(mailConn["port"]) //转换端口类型为int
m := gomail.NewMessage()
m.SetHeader("From","XD Game" + "<" + mailConn["user"] + ">") //这种方式可以添加别名,即“XD Game”, 也可以直接用<code>m.SetHeader("From",mailConn["user"])</code> 读者可以自行实验下效果
m.SetHeader("To", mailTo...) //发送给多个用户
m.SetHeader("Subject", subject) //设置邮件主题
m.SetBody("text/html", body) //设置邮件正文
d := gomail.NewDialer(mailConn["host"], port, mailConn["user"], mailConn["pass"])
err := d.DialAndSend(m)
return err
}
func main() {
//定义收件人
mailTo := []string {
"8888@qq.com",
}
//邮件主题为"Hello"
subject := "Hello"
// 邮件正文
body := "Good"
err := SendMail(mailTo, subject, body)
if err != nil {
fmt.Println(err.Error())
}
}