0

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.

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)
} 

*Tried Example 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)
}    

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)
}   

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?

catter
  • 151
  • 2
  • 16

1 Answers1

2

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."),
)

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.

Further information: https://support.google.com/a/answer/176600?hl=en

novalagung
  • 10,905
  • 4
  • 58
  • 82
  • is this for NoAuth? – catter Jan 07 '19 at 09:25
  • no. authentication is still required. you cannot send an email without authentication. – novalagung Jan 07 '19 at 09:27
  • okay I will send the authorization but while I'm changing the email address coming after the auth then it will not show in the email i'm receiving in my Inbox. It show the same email which I use in auth and it also want that I have to change my settings of the gmail and allow the less secure app – catter Jan 07 '19 at 09:32
  • why I have to allow the less secure app because I'm running my code on localhost? – catter Jan 07 '19 at 09:41
  • @catter it's requirements from google. it's not something we can control. – novalagung Jan 08 '19 at 01:08
  • not for only gmail can we use yahoo port number or hot mail? there is any common port for all the email servers? – catter Jan 08 '19 at 03:59
  • yes you can definitely use any other email provider like, hotmail or yahoo. regarding the configuration (smtp host, port, etc), they already have documentation for it. – novalagung Jan 08 '19 at 04:40