【问题标题】:Sending email from office 365 mailbox through C# using SMTP使用 SMTP 通过 C# 从 Office 365 邮箱发送电子邮件
【发布时间】:2019-12-22 06:41:20
【问题描述】:

我有一个控制台应用程序,我需要通过它发送电子邮件通知。我可以在浏览器中使用相同的凭据登录邮箱。

从抛出的异常来看,我似乎无法通过 C# 代码打开与 Office OWA 的连接并收到错误消息:“发送邮件失败” - 内部异常 - “无法连接到远程服务器”。

我注意到,当我从浏览器登录邮箱时,它首先让我根据电子邮件地址域登陆公司特定页面,然后要求输入密码登录邮箱。

此外,传递给打开 smtpClient 连接的网络凭据需要在 Office 365 中具有一些管理员权限或提升的权限,或者它可以是具有邮箱的普通 Office 365 用户。

参考博客:

https://weblogs.asp.net/sreejukg/send-email-using-office-365-account-and-c?__r=8d721ba733714a1

https://docs.microsoft.com/en-us/Exchange/mail-flow-best-practices/how-to-set-up-a-multifunction-device-or-application-to-send-email-using-office-3?redirectSourcePath=%252farticle%252fHow-to-set-up-a-multifunction-device-or-application-to-send-email-using-Office-365-69f58e99-c550-4274-ad18-c805d654b4c4#HowtoconfigSMTPCS

try
            {
                String userName = "SenderEmailAddress@myCompanyDomain.com";
                String password = "passwordgoeshere";
                MailMessage msg = new MailMessage();
                msg.To.Add(new MailAddress("john.smith@myCompanyDomain.com"));
                msg.From = new MailAddress(userName);
                msg.Subject = "Test Office 365 Account";
                msg.Body = "Testing email using Office 365 account.";
                msg.IsBodyHtml = true;

                using (SmtpClient client = new SmtpClient
                {
                    Host = "smtp.office365.com",
                    Credentials = new System.Net.NetworkCredential(userName, password),
                    Port = 587,
                    EnableSsl = true,
                })
                {
                    client.Send(msg);
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                Console.ReadKey();
            }

目标:

  1. 如果我使用共享邮箱凭据打开连接,我应该能够从共享邮箱发送电子邮件。

【问题讨论】:

  • 你的代码看起来不错(我这周写了类似的东西,它工作正常)考虑到你得到的错误我怀疑你的防火墙阻止了端口 587。阻止端口 25 和 587 是常见的事情让军团阻止特洛伊木马(或您来自黑名单 IP)。尝试使用不同的 Internet 连接等运行您的代码
  • 我是否需要为特定 IP(或 IP 范围)打开这些端口,或者它是一种可以打开或关闭的开/关设置。我知道我公司内的许多其他团队都在发送电子邮件,所以一定有办法让我也能做到这一点。我注意到,从浏览器登录邮箱时,它首先让我根据电子邮件地址域登陆公司特定页面,然后要求输入密码才能登录邮箱。
  • 您只需要为源IP打开它,它很容易测试您应该能够远程登录(腻子等)到端口并得到响应docs.microsoft.com/en-us/exchange/mail-flow/…

标签: c# email office365 exchange-server exchangewebservices


【解决方案1】:

请添加 UseDefaultCredentials = false,否则会选择系统或认证用户凭证发送邮件

            using (SmtpClient client = new SmtpClient
            {
                Host = "smtp.office365.com",
                Credentials = new System.Net.NetworkCredential(userName, password),
                Port = 587,
                EnableSsl = true,
                UseDefaultCredentials = false
            })
            {
                client.Send(msg);
            }

或者推荐这样设置

        using (SmtpClient client = new SmtpClient
        {
            Host = "smtp.office365.com",
            Credentials = new System.Net.NetworkCredential(userName, password),
            Port = 587,
            EnableSsl = true
        })
        {
            client.UseDefaultCredentials = false;
            client.Send(msg);
        }

【讨论】:

  • 这不起作用。我仍然遇到同样的错误。
猜你喜欢
  • 2011-09-08
  • 2015-06-11
  • 2016-04-09
  • 1970-01-01
  • 1970-01-01
  • 2021-06-11
  • 2020-03-20
  • 2016-06-15
相关资源
最近更新 更多