准备
-
谷歌邮箱一个
-
进入谷歌邮箱,搜索【应用专用密码】
-
生成密码并复制
编写代码
使用go语言库:github.com/jordan-wright/email
import (
"fmt"
"github.com/jordan-wright/email"
"net/smtp"
"net/textproto"
)
func sendEmail(from string, to []string) {
e := &email.Email{
To: to,
From: fmt.Sprintf("测试邮箱 <%s>", from),
Subject: "测试邮件 2 - 请忽略",
Text: []byte("这是一封简单的测试邮件,请忽略。"),
HTML: []byte("<h1>这是一封简单的测试邮件,请忽略。</h1>"),
Headers: textproto.MIMEHeader{},
}
err := e.Send("smtp.gmail.com:587", smtp.PlainAuth("", "l397608301@gmail.com", "刚刚生成复制的【应用专用密码】", "smtp.gmail.com"))
if err != nil {
fmt.Println(err)
return
}
fmt.Println("发送成功")
}
测试
func TestSend(t *testing.T) {
from := "l397608301@gmail.com"
to := []string{"397608301@qq.com"}
sendEmail(from, to)
}
评论区