Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send email using Outlook's SMTP servers?

Tags:

email

go

outlook

I want to send an email using Outlook servers, but I'm getting an error 504 5.7.4 Unrecognized authentication type

Here's the snippet.

func sendEmail() {
    server := "smtp-mail.outlook.com
    port := 587
    user := "[email protected]"
    from := user
    pass := "foobar"
    dest := "[email protected]"

    auth := smtp.PlainAuth("", user, pass, server)

    to := []string{dest}

    msg := []byte("From: " + from + "\n" +
        "To: " + dest + "\n" +
        "Subject: Test outlook\n" +
        "OK")

    endpoint := server + ":" + port
    err := smtp.SendMail(endpoint, auth, from, to, msg)
    if err != nil {
        log.Fatal(err)
    }
}

If instead of sending the email using outlook, I use gmail, it works fine.

In Python, I can send the email using outlook with the following code:

    server = smtplib.SMTP(server, port)
    server.starttls()
    server.login(user, password)
    server.sendmail(from, to, msg)
    server.quit()

So I guess I'm missing something in my Go code. According to the doc, SendMail switches to TLS, so that shouldn't be the issue.


1 Answers

auth := smtp.PlainAuth("", user, pass, server)

This is using the authentication method PLAIN. Unfortunately smtp-mail.outlook.com does not support this authentication method:

> EHLO example.com
< 250-AM0PR10CA0007.outlook.office365.com Hello ...
< 250- ...
< 250-AUTH LOGIN XOAUTH2

Thus, only LOGIN and XOAUTH2 are supported as authentication methods.

server.login(user, password)

Python supports LOGIN so it will succeed.

Golang smtp does not support LOGIN. But this gist seems to provide a working fix by adding this missing authentication method.

like image 199
Steffen Ullrich Avatar answered Oct 26 '25 15:10

Steffen Ullrich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!