【问题标题】:Send SMTP email testing Microsoft Office 365 in .net在 .net 中发送 SMTP 电子邮件测试 Microsoft Office 365
【发布时间】:2013-03-31 17:19:54
【问题描述】:

我在 Exchange Online 服务上有一个邮件帐户。现在我正在尝试测试我是否能够通过 c# 应用程序向客户(在各种域和 Microsoft Office 365 上)发送邮件

我尝试实现以下代码,但出现错误

"远程证书根据验证无效 程序。”

MailMessage mail = null;                
mail = new MailMessage();

string[] strToList = "abc@gmail.com"              
foreach (string strID in strToList)
{
    if (strID != null)
    {
        mail.To.Add(new MailAddress(strID));
    }
}

mail.From = "demo@onmicrosoft.com";
mail.Subject = "testing"
mail.IsBodyHtml = true;
mail.Body = "mail body";

SmtpClient client = new SmtpClient("smtp.outlook.office365.com");
client.Port = 587;
client.EnableSsl = true;
client.UseDefaultCredentials = false;
NetworkCredential cred = new System.Net.NetworkCredential("demo@onmicrosoft.com", "mypassword");
client.Credentials = cred;
client.Send(mail);

如果我做错了什么,请提出建议。 提前非常感谢。

【问题讨论】:

标签: c# exchange-server office365 system.net.mail


【解决方案1】:

这对我有用(编辑自 source


   ThreadPool.QueueUserWorkItem(t =>
            {
                SmtpClient client = new SmtpClient("smtp.office365.com",587);
                client.EnableSsl = true;
                client.Credentials = new System.Net.NetworkCredential("xxx@yyy.com", "password");
                MailAddress from = new MailAddress("xxx@yyy.com", String.Empty, System.Text.Encoding.UTF8);
                MailAddress to = new MailAddress("xxx@yyy.com");
                MailMessage message = new MailMessage(from, to);
                message.Body = "The message I want to send.";
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.Subject = "The subject of the email";
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                // Set the method that is called back when the send operation ends.
                client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
                // The userState can be any object that allows your callback 
                // method to identify this send operation.
                // For this example, I am passing the message itself
                client.SendAsync(message, message);
            });

        private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            // Get the message we sent
            MailMessage msg = (MailMessage)e.UserState;

            if (e.Cancelled)
            {
                // prompt user with "send cancelled" message 
            }
            if (e.Error != null)
            {
                // prompt user with error message 
            }
            else
            {
                // prompt user with message sent!
                // as we have the message object we can also display who the message
                // was sent to etc 
            }

            // finally dispose of the message
            if (msg != null)
                msg.Dispose();
        }

【讨论】:

  • 有谁知道是否可以在不输入纯文本密码的情况下执行此操作?
  • 我在回调中没有错误,但它没有发送邮件?你能帮我吗?
  • 你有 e.Error = null 吗? e. 取消 = 真/假?试试 client.EnableSsl = false;
【解决方案2】:

在某些情况下,TLS 身份验证可能会导致在使用 smtp.office365.com 作为 c# 中的 SMTP 时出现问题。 在 Send(msg) 语句之前尝试以下行(覆盖 .TargetName):

client.TargetName = "STARTTLS/smtp.office365.com";

这个适合我

【讨论】:

  • 这为我解决了这个问题 - 我已经在许多地方使用了这个问题的常规代码,但是在一个站点上,如果不添加这个 TargetName 值,我就无法让事情顺利进行。谢谢!!
【解决方案3】:

这也是发送邮件的最佳方式。我已经在我的项目中尝试过它并且工作正常。

 SmtpClient client = new SmtpClient("smtp.office365.com", 587);
        client.EnableSsl = true;
        client.Credentials = new System.Net.NetworkCredential("From@mail.com", "sdsd@12345");
        MailAddress from = new MailAddress("From Address Ex From@mail.com", String.Empty, System.Text.Encoding.UTF8);
        MailAddress to = new MailAddress("From Address Ex To@mail.com");
        MailMessage message = new MailMessage(from, to);
        message.Body = "This is your body message";
        message.BodyEncoding = System.Text.Encoding.UTF8;
        message.Subject = "Subject";
        message.SubjectEncoding = System.Text.Encoding.UTF8;

        client.Send(message);

【讨论】:

    【解决方案4】:

    尝试 smtp.office365.com 而不是 smtp.outlook.office365.com

    【讨论】:

      【解决方案5】:

      尝试使用:

      ServicePointManager.ServerCertificateValidationCallback = 
          (sender, certificate, chain, sslPolicyErrors) => true;
      

      此代码将允许您接受无效证书。

      正如 Ori Nachum 在评论中提到的那样:这是一种非常糟糕的做法,只能用于测试目的。这是一个安全风险!

      【讨论】:

      • 当我尝试上述方法时,我得到另一个异常“SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.7.1 客户端未通过身份验证”
      • @RowlandShaw 你检查过stackoverflow.com/questions/20906077/… 吗?
      • 应该注意这是一个不好的做法,应该只用于测试目的。这是一个重大的安全风险。
      【解决方案6】:

      错误

      SMTP 服务器需要安全连接,或者客户端没有 认证。服务器响应是:5.7.1 客户端没有 已认证

      通常在关联的用户帐户密码过期或帐户被锁定时发生。尝试在 Active Directory 中设置“用户密码永不过期”,如果它不违反您的公司密码政策 :) 这发生在我使用 o365 Exchange Online A/c 测试时。

      【讨论】:

      • 我专门设置了一个新用户通过 SMTP 登录。我收到此错误是因为在第一次登录时,用户需要更改密码。让我迷惑了一阵子!
      【解决方案7】:

      尝试设置:

      ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
      

      它将确保服务的 SecurityPoint 是 TLS。
      请注意此处提供的答案设置

      ServicePointManager.ServerCertificateValidationCallback = 
          (sender, certificate, chain, sslPolicyErrors) => true;
      

      可能有利于测试目的 - 但这是一个重大安全风险
      不要在 prod 帐户或 prod 环境中使用。

      【讨论】:

        【解决方案8】:
        var Client = new SmtpClient("smtp.office365.com", 587);
        Client.EnableSsl = true;
        Client.Credentials = new System.Net.NetworkCredential("mail", "pass");
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
        

        我成功发送带有代码的邮件

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-12-22
          • 1970-01-01
          • 2022-09-24
          • 2011-09-08
          • 2016-06-15
          • 1970-01-01
          相关资源
          最近更新 更多