准备工作
- 开启代理邮箱的pop3/smtp服务
- 获取指定邮箱的授权码
- import “gopkg.in/gomail.v2”
demo
package main
import (
"fmt"
"strconv"
"gopkg.in/gomail.v2"
)
const (
// 邮件服务器
Host = "xxxx.com"
// 邮件服务器开放端口
Port = "465"
// 发件人
SenderName = "Tester"
// 你代理发送的邮箱
User = "[email protected]"
// 授权码
Password = "xxxxxxx"
)
func main() {
err := SendMail(User, Password, Host, Port, "[email protected]", SenderName, "测试邮件发送", "这是我用gomail给你发送的一个测试邮件")
fmt.Println("发送完成:", err)
}
func SendMail(userName, authCode, host, portStr, mailTo, sendName string, subject, body string) error {
port, _ := strconv.Atoi(portStr)
m := gomail.NewMessage()
m.SetHeader("From", m.FormatAddress(userName, sendName))
m.SetHeader("To", mailTo)
m.SetHeader("Subject", subject)
m.SetBody("text/html", body)
d := gomail.NewDialer(host, port, userName, authCode)
err := d.DialAndSend(m)
return err
}
发送