0

我正在使用 golang 的 smtp 包将邮件从本地主机发送到给定的邮件地址。但是有一个问题我提供了我的电子邮件和密码,但它会向我显示错误

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,它不会给出任何响应,它只显示请求。

谁能告诉我如何在没有身份验证的情况下将邮件从本地主机发送到给定的邮件地址?

4

1 回答 1

2

尝试587在第一个示例中使用端口。它应该工作。

err := smtp.SendMail(
    "smtp.gmail.com:587",
    auth,
    "emailFrom",
    []string{EmailToooo},
    []byte("This is the email body."),
)

如果您使用smtp.gmail.com,则正确的端口是 587 (TLS) 或 465 (SSL),必须允许安全性较低的应用程序。

更多信息:https ://support.google.com/a/answer/176600?hl=en

于 2019-01-07T09:24:28.403 回答