答案:使用net/smtp包配置SMTP服务器信息并构造邮件内容,通过smtp.PlainAuth实现认证,设置邮件头的Content-Type为text/html发送HTML邮件,利用mime/multipart包构建正文和附件的多部分消息以发送带附件邮件,同时通过错误类型判断处理连接超时、认证失败等异常。

Golang发送邮件,核心在于
net/smtp
首先,需要配置SMTP服务器的连接信息,包括服务器地址、端口、用户名和密码。然后,构造邮件内容,包括发件人、收件人、主题和正文。最后,使用
smtp.SendMail
SMTP服务器认证是发送邮件的关键一步。很多服务器都需要验证你的身份,防止垃圾邮件。
smtp.SendMail
smtp.Auth
smtp
smtp.PlainAuth
例如:
立即学习“go语言免费学习笔记(深入)”;
package main
import (
"fmt"
"net/smtp"
"log"
)
func main() {
// SMTP服务器配置
smtpServer := "smtp.example.com"
smtpPort := 587
smtpUser := "your_email@example.com"
smtpPass := "your_password"
from := "your_email@example.com"
to := []string{"recipient@example.com"}
subject := "Golang SMTP Email"
body := "This is a test email from Golang using the smtp package."
// 构造邮件内容
message := []byte("To: recipient@example.com\r\n" +
"Subject: " + subject + "\r\n" +
"\r\n" + body + "\r\n")
// 认证信息
auth := smtp.PlainAuth("", smtpUser, smtpPass, smtpServer)
// 连接SMTP服务器并发送邮件
err := smtp.SendMail(fmt.Sprintf("%s:%d", smtpServer, smtpPort), auth, from, to, message)
if err != nil {
log.Fatal(err)
}
fmt.Println("Email sent successfully!")
}这段代码展示了如何使用
smtp.PlainAuth
如果你的SMTP服务器使用其他认证方式,例如CRAM-MD5或LOGIN,你需要使用相应的
smtp.Auth
发送HTML邮件需要在邮件头中指定
Content-Type
修改上面的例子:
phpList提供开源电子邮件营销服务,包括分析、列表分割、内容个性化和退信处理。丰富的技术功能和安全稳定的代码基础是17年持续开发的结果。在95个国家使用,在20多种语言中可用,并用于去年发送了250亿封电子邮件活动。您可以使用自己的SMTP服务器部署它,或在http://phplist.com上获得免费的托管帐户。
14
package main
import (
"fmt"
"net/smtp"
"log"
)
func main() {
// SMTP服务器配置
smtpServer := "smtp.example.com"
smtpPort := 587
smtpUser := "your_email@example.com"
smtpPass := "your_password"
from := "your_email@example.com"
to := []string{"recipient@example.com"}
subject := "Golang SMTP HTML Email"
body := `<!DOCTYPE html>
<html>
<head>
<title>HTML Email</title>
</head>
<body>
<h1>This is an HTML email from Golang!</h1>
<p>Using the <strong>smtp</strong> package.</p>
</body>
</html>`
// 构造邮件内容
message := []byte("To: recipient@example.com\r\n" +
"Subject: " + subject + "\r\n" +
"MIME-version: 1.0\r\n" +
"Content-Type: text/html; charset=\"UTF-8\"\r\n" +
"\r\n" + body + "\r\n")
// 认证信息
auth := smtp.PlainAuth("", smtpUser, smtpPass, smtpServer)
// 连接SMTP服务器并发送邮件
err := smtp.SendMail(fmt.Sprintf("%s:%d", smtpServer, smtpPort), auth, from, to, message)
if err != nil {
log.Fatal(err)
}
fmt.Println("Email sent successfully!")
}注意
Content-Type: text/html; charset="UTF-8"
发送带附件的邮件稍微复杂一些,需要使用
mime/multipart
multipart.Part
multipart.Writer
Content-Type
Content-Disposition
这部分代码比较长,这里只给出思路:
multipart.Writer
Content-Type: text/plain; charset="UTF-8"
Content-Type: text/html; charset="UTF-8"
Content-Type
application/pdf
Content-Disposition: attachment; filename="your_file.pdf"
multipart.Writer
bytes.Buffer
Content-Type
multipart/mixed; boundary="your_boundary"
your_boundary
multipart.Writer
smtp.SendMail
实际操作中,可以使用现成的库,例如
github.com/jordan-wright/email
发送邮件时可能会遇到各种错误,例如连接超时、认证失败、服务器拒绝等。需要仔细处理这些错误,并提供有用的错误信息。
例如,可以检查
smtp.SendMail
err := smtp.SendMail(fmt.Sprintf("%s:%d", smtpServer, smtpPort), auth, from, to, message)
if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
log.Println("Timeout error:", err)
} else {
log.Println("SendMail error:", err)
}
// 可以根据错误类型进行不同的处理
}此外,还可以使用
log
以上就是Golang发送电子邮件 smtp包配置与发送的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号