如何使用auth发送邮件? [英] How to send a mail with using auth?

查看:116
本文介绍了如何使用auth发送邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using smtp package of golang to send the mail from localhost to the given mail address. But there is a problem I'm providing my email and password for it but it will show me the error of

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

他们希望我必须允许安全性较低的应用程序使用我的帐户,但是我不想让我尝试一小段代码.

they want that I have to allow less secure app to use my account But I don't want to allow that I tried a small piece of code for it.

尝试示例1:-

Tried Example1:-

// 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邮件,但是在这种情况下,它会给我端口连接的错误,请参见给定的代码:-

I also tried a gopkg.in/gomail.v2 package for doing NoAuth mail but in this it will give me the error of port connection see in given code:-

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,它将不会给出仅显示为请求的任何响应.

I also change the port to 8080 after doing 8080 it will not give any response it was showing only requesting.

任何人都可以告诉我,如何在没有身份验证的情况下将邮件从本地主机发送到给定的邮件地址?

Can anyone tell me that how will I send mail from localhost to the given mail address without auth?

推荐答案

在第一个示例中尝试使用端口587.它应该可以正常工作.

Try to use port 587 on first example. It should be working.

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

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

If you use smtp.gmail.com then the correct port is either 587 (TLS) or 465 (SSL), with the less secure app must be allowed.

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

这篇关于如何使用auth发送邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆