【问题标题】:Unable to send emails using C# code from GoDaddy configured Office 365 account无法使用 GoDaddy 配置的 Office 365 帐户中的 C# 代码发送电子邮件
【发布时间】:2020-10-20 12:12:32
【问题描述】:

我已从 GoDaddy 购买了“Office 365 Email Essentials”计划用于发送电子邮件。我可以在我的 Outlook 邮箱中发送和接收这些电子邮件,但不能通过 C# 代码以编程方式发送和接收这些电子邮件。 我不断收到错误“

SMTP 服务器需要安全连接,或者客户端没有 认证。服务器响应为:5.7.57 SMTP;客户不是 在 MAIL FROM 期间经过身份验证以发送匿名邮件 [BMXPR01CA0092.INDPRD01.PROD.OUTLOOK.COM]"

MailMessage msg = new MailMessage();
        msg.To.Add(new MailAddress("xxx@gmail.com", "xxx"));
        msg.From = new MailAddress("xxx@mydomain.com", "Admin");
        msg.Subject = "This is a Test Mail";
        msg.Body = "This is a test message using Exchange OnLine";
        msg.IsBodyHtml = true;

        SmtpClient client = new SmtpClient();
        client.UseDefaultCredentials = false;
        client.Credentials = new NetworkCredential("xxx@mydomain.com", "mypassword","mydomain.com");
        client.Port = 587;
        client.Host = "smtp.office365.com";
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.EnableSsl = true;
        //Send the msg
        try
        {
            client.Send(msg);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

这是配置文件中的邮件设置条目-

<mailSettings>
<smtp from="xxx@mydomain.com">
  <network host="smtp.office365.com" port="587" />
</smtp>
</mailSettings>

我在 Outlook 中看到的设置是 -

服务器名称:smtp.office365.com 港口:587 加密方式:STARTTLS

另外,我也尝试使用用户 ID 代替网络凭据中的实际电子邮件地址,但这也不起作用。

我也尝试在 GoDaddy 中使用 MX Txt 记录,即服务器“relay-hosting.secureserver.net”和端口 #25,但没有运气。

我还根据链接 http://edudotnet.blogspot.com/2014/02/smtp-microsoft-office-365-net-smtp.html 更改了“邮箱委托”中的设置,但这也没有帮助。

请帮助我在这些步骤中缺少什么。

【问题讨论】:

  • 我也试过禁用防火墙,添加入站规则以允许 TCP 端口 587 等。
  • 您的问题解决了吗?我目前也遇到同样的情况,如果您在此处发布您的解决方案将不胜感激

标签: c# email office365 smtpclient


【解决方案1】:

花了几个小时后,我发现 Godaddy 配置的 Office 365 邮件 ID 的 smtp 主机是“smtpout.secureserver.net”。

还将客户端编程为关闭连接,如下所示:

SmtpClient client = new SmtpClient {

   Host = "smtpout.secureserver.net",

   Credentials = new NetworkCredential("USERNAME@DOMAIN.COM", "YOURPWD"),

   Port = 587,

   EnableSsl = true,

   Timeout = 10000

};

这些更新后,它会工作!!!享受吧!

示例控制台应用程序代码!

static void Main(string[] args)
        {
            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                string userId = "username@domainname.com";
                string _pwd = "mypassword";
                var toAddress = "touseremailaddress";
                SmtpClient client = new SmtpClient
                {
                    Host = "smtpout.secureserver.net",
                    Credentials = new NetworkCredential(userId, _pwd),
                    Port = 587,
                    EnableSsl = true
                };
               
                var Msg = new MailMessage();
                Msg.From = new MailAddress(userId);
                Msg.To.Add(new MailAddress(toAddress.ToString()));
                Msg.Subject = "TEST EMAIL";
                Msg.Body = "Hello world";

                Console.WriteLine("start to send email over SSL...");
                client.Send(Msg);
                Console.WriteLine("email was sent successfully!");
                Console.ReadLine();
            }
            catch (Exception ep)
            {
                Console.WriteLine("failed to send email with the following error:");
                Console.WriteLine(ep.Message);
                Console.ReadLine();
            }
        }

【讨论】:

    【解决方案2】:

    尝试将 SMTP 服务器名称更改为 outlook.office365.com 而不是 smtp.office365.com

    【讨论】:

    • 我试过了,但它给了我一个错误“SMTP服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.7.57 SMTP;客户端未通过身份验证发送匿名邮件来自 [BMXPR01CA0002.INDPRD01.PROD.OUTLOOK.COM] 的邮件”
    猜你喜欢
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多