donglie7268
2019-09-04 07:54
浏览 2.0k
已采纳

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

图片转代码服务由CSDN问答提供 功能建议

我想使用Outlook服务器发送电子邮件,但出现错误 504 5.7.4 无法识别的身份验证类型

这是代码段。

  func sendEmail(){
 server:=“ smtp-mail  .outlook.com 
端口:= 587 
用户:=“ foo@outlook.com” 
来自:=用户
传递:=“ foobar” 
目标:=“ bar@gmail.com” 
  
 auth:= smtp.PlainAuth(“”,用户,密码,服务器)
 
到:= [] string {dest} 
 
 msg:= [] byte(“ From:” + from +“  
“ + 
”收件人:“ +目标+” 
“ + 
”主题:测试外观
“ + 
”确定“)
 
端点:=服务器+”:“ +端口\  n err:= smtp.SendMail(endpoint,auth,from,to,msg)
 if err!= nil {
 log.Fatal(err)
} 
} 
    
 
 

如果我不是使用Outlook发送电子邮件,而是使用gmail,它可以正常工作。

在Python中,我可以使用以下代码使用Outlook发送电子邮件:

 服务器= smtplib.SMTP(服务器,端口)
  server.starttls()
 server.login(用户,密码)
 server.sendmail(从,到,味精)
 server.quit()
   
 
 

所以我想我的Go代码中缺少某些内容。 根据 doc SendMail 切换为 TLS ,所以这不应该成为问题。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 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.

    点赞 评论

相关推荐 更多相似问题