smtp.SendMail无法将邮件发送给多个收件人golang

huangapple go评论143阅读模式
英文:

smtp.SendMail cannot send mail to multiple recipent golang

问题

你在Go语言中发送邮件给多个收件人,使用的是你的雅虎邮箱,但是只有我自己收到了邮件。

代码:

err := smtp.SendMail(
    "smtp.mail.yahoo.com:25",
    auth,
    "testmail1@yahoo.com",
    []string{"testmail1@yahoo.com, testmail2@yahoo.com"},
    []byte("test")

邮件内容:

From: "testMail1" <testmail1@yahoo.com>
To: testMail1 <testmail1@yahoo.com>, testMail2 <testmail2@yahoo.com>,
Subject: "mail"
MIME-Version: 1.0
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: base64

输出结果:

2015/05/18 20:22:26 501 Syntax error in arguments

你做错了什么?

英文:

I want to send mail to multiple recipients in Go, with my yahoo mail, but I from all recipients only I get mail.

Code:

err := smtp.SendMail(
    &quot;smtp.mail.yahoo.com:25&quot;,
    auth,
    &quot;testmail1@yahoo.com&quot;,
    []string{&quot;testmail1@yahoo.com, testmail2@yahoo.com&quot;},
    []byte(&quot;test&quot;)

message:

From: &quot;testMail1&quot; &lt;testmail1@yahoo.com&gt;
To: testMail1 &lt;testmail1@yahoo.com&gt;, testMail2 &lt;testmail2@yahoo.com&gt;,
Subject: &quot;mail&quot;
MIME-Version: 1.0
Content-Type: text/html; charset=&quot;utf-8&quot;
Content-Transfer-Encoding: base64

This is output:

2015/05/18 20:22:26 501 Syntax error in arguments

What have I done wrong?

答案1

得分: 2

你的代码片段不完整,与你的电子邮件不匹配。你能发送更多的代码吗?

无论如何,你可以尝试替换:

[]string{"testmail1@yahoo.com, testmail2@yahoo.com"},

为:

[]string{"testmail1@yahoo.com", "testmail2@yahoo.com"},

此外,你可以尝试使用我的包Gomail来轻松发送电子邮件:

package main

import (
	"gopkg.in/gomail.v2"
)

func main() {
	m := gomail.NewMessage()
	m.SetAddressHeader("From", "testmail1@yahoo.com", "testMail1")
	m.SetHeader("To",
	    m.FormatAddress("testmail1@yahoo.com", "testMail1"),
	    m.FormatAddress("testmail2@yahoo.com", "testMail2"),
	)
	m.SetHeader("Subject", "mail")
	m.SetBody("text/plain", "Hello!")

	d := gomail.NewPlainDialer("smtp.mail.yahoo.com", 25, "login", "password")
	if err := d.DialAndSend(m); err != nil {
		panic(err)
	}
}
英文:

Your code snippet is incomplete and does not match your email. Could you send more code?

Anyway you can try replacing:

[]string{&quot;testmail1@yahoo.com, testmail2@yahoo.com&quot;},

By:

[]string{&quot;testmail1@yahoo.com&quot;, &quot;testmail2@yahoo.com&quot;},

Also you can try my package Gomail to easily send emails:

package main

import (
	&quot;gopkg.in/gomail.v2&quot;
)

func main() {
	m := gomail.NewMessage()
	m.SetAddressHeader(&quot;From&quot;, &quot;testmail1@yahoo.com&quot;, &quot;testMail1&quot;)
	m.SetHeader(&quot;To&quot;,
	    m.FormatAddress(&quot;testmail1@yahoo.com&quot;, &quot;testMail1&quot;),
	    m.FormatAddress(&quot;testmail2@yahoo.com&quot;, &quot;testMail2&quot;),
	)
	m.SetHeader(&quot;Subject&quot;, &quot;mail&quot;)
	m.SetBody(&quot;text/plain&quot;, &quot;Hello!&quot;)

	d := gomail.NewPlainDialer(&quot;smtp.mail.yahoo.com&quot;, 25, &quot;login&quot;, &quot;password&quot;)
	if err := d.DialAndSend(m); err != nil {
		panic(err)
	}
}

huangapple
  • 本文由 发表于 2015年5月19日 02:41:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/30310646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定