17

我在 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);

如果我做错了什么,请指教。提前非常感谢。

4

8 回答 8

15

这对我有用(从源代码编辑)


   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();
        }
于 2013-12-23T11:46:51.780 回答
6

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

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

这个对我有用

于 2014-06-12T05:16:03.970 回答
6

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

 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);
于 2017-05-10T08:59:13.200 回答
3

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

于 2014-03-02T22:06:16.677 回答
2

尝试使用:

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

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

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

于 2013-04-09T10:16:01.103 回答
1

错误

SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.7.1 客户端未通过身份验证

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

于 2013-07-08T05:50:03.913 回答
1
var Client = new SmtpClient("smtp.office365.com", 587);
Client.EnableSsl = true;
Client.Credentials = new System.Net.NetworkCredential("mail", "pass");
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

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

于 2020-06-26T13:55:27.463 回答
1

尝试设置:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

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

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

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

于 2018-08-14T08:10:45.893 回答