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

如何使用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条)

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作