Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29748

smtp auth settings for 0365 using Go net/smtp library | Error "Unrecognized authentication type" using PlainAuth

$
0
0

I'm struggling to set up the tls config settings for Office365 smtp. I understand that Microsoft may have recently stopped supporting PlainAuth. But I can't find any guidance on what I should be using, or how

I understand that tls is required and that with Go I need to be using the StartTLS method on the client

However now i'm getting the following error:

504 5.7.4 Unrecognized authentication type [LNXP123CA0016.GBRP123.PROD.OUTLOOK.COM]

This error is found on the lines when i'm using the Auth variable that I set:

if err = c.Auth(auth); err != nil {
    fmt.Println("c.Auth Error: ", err)
} 

Does anyone know how I should be setting up my auth variable for O365?

Full code example below

package main

import (
"fmt
"net"

emailer "acadiant/EMAILER"
mail "net/mail"
smtp "net/smtp"
)    

func main() {

from := mail.Address{"", "supersecretandhiddenfromwork@example.com"}
to := mail.Address{"", "supersecretandhiddenfromwork@example.com"}
subject := "Test subject"
body := "Test email body"

host := "smtp.office365.com"

headers := make(map[string]string)
headers["From"] = from.String()
headers["To"] = to.String()
headers["Subject"] = subject

message := ""
for k, v := range headers {
    message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + body

//servername := "smtp.office365.com:587"
//host, _, _ = net.SplitHostPort(servername)

auth := smtp.PlainAuth("", "supersecretandhiddenfromwork@example.com", "password", host)

tlsconfig := &tls.Config{
    ServerName: host,
}

conn, err := net.Dial("tcp", "smtp.office365.com:587")
if err != nil {
    fmt.Println("tls.Dial Error: ", err)
}

c, err := smtp.NewClient(conn, host)
if err != nil {
    fmt.Println("smtp.NewClient Error: ", err)
}

c.StartTLS(tlsconfig)

if err = c.Auth(auth); err != nil {
    fmt.Println("c.Auth Error: ", err)
}

if err = c.Mail(from.Address); err != nil {
    fmt.Println("c.Mail Error: ", err)
}

if err = c.Rcpt(to.Address); err != nil {
    fmt.Println("c.Rcpt Error: ", err)
}

w, err := c.Data()
if err != nil {
    fmt.Println("c.Data Error: ", err)
}

_, err = w.Write([]byte(message))
if err != nil {
    fmt.Println("Error: ", err)
}

err = w.Close()
if err != nil {
    fmt.Println("reader Error: ", err)
}

c.Quit()
}

Viewing all articles
Browse latest Browse all 29748

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>