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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • 关注

    要使用 Outlook 的 SMTP 服务器发送电子邮件,请按照以下步骤进行操作:

    打开 Outlook,然后选择“文件”选项卡。

    在“信息”下选择“账户设置”,然后选择“服务器设置”。

    在“服务器设置”窗口中,选择“电子邮件”选项卡,然后选择您要使用的电子邮件帐户。

    单击“更改”按钮以修改您的电子邮件设置,确保以下设置正确:

    用户名和密码:输入您的电子邮件地址和密码。
    服务器信息:选择要使用的电子邮件服务器和端口,SMTP 服务器为“smtp.office365.com”,端口号为“587”。
    加密方法:选择“TLS”进行加密。
    身份验证要求:选择“需要身份验证”。
    在完成设置后,单击“测试帐户设置”,以确保您的设置是正确的,然后单击“关闭”,关闭窗口。

    现在您可以使用 Outlook 的 SMTP 服务器发送电子邮件了。要发送邮件,请在“新邮件”窗口中添加收件人、主题和内容,然后单击“发送”。

    希望这些步骤可以帮助您使用 Outlook 的 SMTP 服务器发送电子邮件。如果您遇到任何问题,请联系您的电子邮件服务提供商或技术支持。
    另外,如果您使用的是 Outlook.com,您也可以使用以下 SMTP 服务器发送电子邮件:

    SMTP 服务器地址:smtp.office365.com
    端口:587
    加密方法:TLS
    身份验证:是,需要用户名和密码
    如果您使用的是 Gmail,以下是使用 Outlook 的 SMTP 服务器发送电子邮件所需的设置:

    SMTP 服务器地址:smtp.gmail.com
    端口:587
    加密方法:TLS 或 SSL
    身份验证:是,需要用户名和密码
    请注意,某些电子邮件服务提供商可能需要使用不同的端口或加密参数。如果您无法使用上述步骤成功发送电子邮件,请参考您的电子邮件服务提供商的文档或联系他们的技术支持,获取更多帮助和信息。

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 欧姆龙PLC 电机控制 限位
  • ¥30 如何处理shell命令接收到的视频流并实时播放出来
  • ¥15 虚心请教C#的代码优化问题
  • ¥15 有偿求做台风过境全过程模拟仿真
  • ¥50 求!AutomationDesk 如何自动导入Variant数据
  • ¥20 Next.JS 静态导出路由
  • ¥15 我做一个对中文文本情感分析的项目 我用了CNN,和keras框架 报的错误我一直处理不好
  • ¥15 unity使用bakery打光烘焙所遇到的问题。
  • ¥99 二维有限元方法求解,泊松方程
  • ¥35 我需要在PC端 开两个抖店工作台客户端.(语言-java)