package main
import ( "net/smtp" )
func main() { // SMTP 服务器配置 smtpHost := "smtp.example.com" smtpPort := "587" username := "your-email@example.com" password := "your-password"
// 收件人和发件人
from := "your-email@example.com"
to := []string{"recipient@example.com"}
subject := "测试邮件"
body := "这是一封使用 Go 发送的测试邮件!"
// 构造邮件内容
message := []byte("To: " + to[0] + "\r\n" +
"Subject: " + subject + "\r\n" +
"\r\n" +
body + "\r\n")
// 认证
auth := smtp.PlainAuth("", username, password, smtpHost)
// 发送邮件
err := smtp.SendMail(smtpHost+":"+smtpPort, auth, from, to, message)
if err != nil {
fmt.Println("发送失败:", err)
return
}
fmt.Println("邮件发送成功!")
}