使用&#34; Name&#34;发送电子邮件来自Go的<email>

时间:2016-04-07 19:49:17

标签: email go smtp standard-library

我使用net/smtp发送电子邮件,似乎无法处理电子邮件中的联系人姓名。

c, _ := smtp.Dial("smtp.example.com:25")
c.Mail(`jdoe@example.com`)

而不是

c, _ := smtp.Dial("smtp.example.com:25")
c.Mail(`"John Q. Doe" <jdoe@example.com>`)

有没有一个好方法来处理这个问题?如果可以的话,我更喜欢封装和标准的东西,但如果可以做到的话,我愿意使用原始SMTP。

2 个答案:

答案 0 :(得分:0)

你可以试试没有双引号吗?即

c.Mail(`John Q. Doe <jdoe@example.com>`)

答案 1 :(得分:0)

smtpServer := "smtp.example.com"
auth := smtp.PlainAuth("", "from@example.com", "******", smtpServer)

from := mail.Address{"fromName", "from@example.com"}
to := mail.Address{"toName", "to@example.com"}
msg := []byte("From: " + from.String() + "\r\n" +
"To: " + to.String() + "\r\n" +
"Subject: Awesome Subject !\r\n" +
"This is the email body.\r\n")
err := smtp.SendMail(smtpServer + ":25", auth, from.Address,[]string{to.Address}, msg)
if err != nil {
    log.Fatal(err)
}
相关问题