go-gomail / gomail

The best way to send emails in Go.
MIT License
4.3k stars 570 forks source link

Username and Password not accepted #133

Open Dontmindmes opened 5 years ago

Dontmindmes commented 5 years ago
m := gomail.NewMessage()
m.SetHeader("From", "from@example.com")
m.SetHeader("To", "to@example.com")
m.SetHeader("Subject", "Hello!")
m.Embed("passlist.rxt")
m.SetBody("text/html", ``)

d := gomail.NewPlainDialer("smtp.gmail.com", 587, "ss@gmail.com", "ss")
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}

if err := d.DialAndSend(m); err != nil {
    panic(err)
}

`panic: 535 5.7.8 Username and Password not accepted. Learn more at 5.7.8 https://support.google.com/mail/?p=BadCredentials 195sm9531505pfc.50 - gsmtp

goroutine 1 [running]: main.SendEmail()`

shcabin commented 5 years ago

Seconded. I have seeing the same issue too. follow https://support.google.com/mail/?p=BadCredentials, turn on access for less secure apps, but it didn't work.

pedromorgan commented 5 years ago

see #108

Alireza-cman commented 4 years ago

Hi, I have this problem too!! does anyone solve this issue? I didn't find anything in #108 as @pedromorgan mentioned in the above comment.

the-fanan commented 3 years ago

@Alireza-cman did you finally find a solution? I am also experiencing this issue.

mudphilo commented 3 years ago

I had a similar problem connecting to office365. Office 365 needs LoginAuth. I achieved this by creating a custom method to implement this. Create a file and paste the below code

import (
    "net/smtp"
    "errors"
)

type loginAuth struct {
    username, password string
}

func LoginAuth(username, password string) smtp.Auth {
    return &loginAuth{username, password}
}

func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
    return "LOGIN", []byte{}, nil
}

func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
    if more {
        switch string(fromServer) {
        case "Username:":
            return []byte(a.username), nil
        case "Password:":
            return []byte(a.password), nil
        default:
            return nil, errors.New("Unkown fromServer")
        }
    }
    return nil, nil
}

Generate LoginAuth below

...

auth := LoginAuth(username, password)

...

Then add the generated auth to gomail

...
d := gomail.NewDialer(server, port, username, pass)
d.Auth = auth // add LoginAuth generated above

// Send the email 
if err := d.DialAndSend(m); err != nil {
        log.Printf(" cant send email got error %s ", err.Error())
        return false
}
...
goeroeku commented 2 years ago

@mudphilo thanks, it worked