【发布时间】:2019-06-01 22:34:01
【问题描述】:
我正在使用 golang 的 smtp 包将邮件从 localhost 发送到给定的邮件地址。但是有一个问题,我提供了我的电子邮件和密码,但它会显示错误
535 5.7.8 Username and Password not accepted. Learn more at
5.7.8 https://support.google.com/mail/?p=BadCredentials p24sm107930499pfk.155 - gsmtp
他们希望我必须允许不太安全的应用程序使用我的帐户,但我不想允许我尝试了一小段代码。
试过的例子1:-
// Set up authentication information.
auth := smtp.PlainAuth(
"",
"email",
"password",
"smtp.gmail.com",
)
// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.
err := smtp.SendMail(
"smtp.gmail.com:25",
auth,
"emailFrom",
[]string{EmailToooo},
[]byte("This is the email body."),
)
if err != nil {
log.Fatal(err)
}
*尝试过的示例 2:- *
m := gomail.NewMessage()
m.SetHeader("From", "SenderEmail@gmail.com")
m.SetHeader("To", "Email_Tooo@gmail.com")
m.SetHeader("Subject", "Hello!")
m.SetBody("text/html", "Hello <b>Bob</b> and <i>Cora</i>!")
d := gomail.NewDialer("smtp.gmail.com", 587, "email", "password")
// Send the email to Bob, Cora and Dan.
if err := d.DialAndSend(m); err != nil {
fmt.Println(err)
}
我还尝试了一个 gopkg.in/gomail.v2 包来执行 NoAuth 邮件,但在此它会给我端口连接错误,请参见给定代码:-
m := gomail.NewMessage()
m.SetHeader("From", "from@example.com")
m.SetHeader("To", "to@example.com")
m.SetHeader("Subject", "Hello!")
m.SetBody("text/plain", "Hello!")
d := gomail.Dialer{Host: "localhost", Port: 587}
if err := d.DialAndSend(m); err != nil {
panic(err)
}
在执行 8080 后,我还将端口更改为 8080,它不会给出任何响应,它只显示请求。
谁能告诉我如何在没有身份验证的情况下将邮件从本地主机发送到给定的邮件地址?
【问题讨论】:
-
您尝试建立安全连接了吗?
-
这里的安全连接是什么意思?
标签: email go smtp mail-sender