donglie7268 2019-09-04 07:54 采纳率: 100%
浏览 2628
已采纳

如何使用Outlook的SMTP服务器发送电子邮件?

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 := "foo@outlook.com"
    from := user
    pass := "foobar"
    dest := "bar@gmail.com"

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

    to := []string{dest}

    msg := []byte("From: " + from + "
" +
        "To: " + dest + "
" +
        "Subject: Test outlook
" +
        "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.

  • 写回答

2条回答 默认 最新

  • doushai7225 2019-09-04 09:28
    关注
    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵