在.net中发送测试Microsoft Office 365的SMTP电子邮件

本文关键字:Office SMTP 电子邮件 Microsoft 测试 net | 更新日期: 2023-09-27 18:08:26

我在Exchange Online服务上有一个邮件帐户。现在我正在尝试测试我是否能够通过c#应用程序向客户(在varoius域和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);

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

在.net中发送测试Microsoft Office 365的SMTP电子邮件

这对我有效(从源代码编辑(


   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();
        }

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

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

这个适用于我

这也是发送邮件的最佳方式。我在我的项目中尝试过,效果很好。

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

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

尝试使用:

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

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

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

错误

SMTP服务器需要安全连接,或者客户端没有经过身份验证。服务器响应为:5.7.1客户端不是经过验证的

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

尝试设置:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

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

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

可能有利于测试,但这是一个重大安全风险
不要与产品帐户或在产品环境中使用。

var Client = new SmtpClient("smtp.office365.com", 587);
Client.EnableSsl = true;
Client.Credentials = new System.Net.NetworkCredential("mail", "pass");
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

我成功地发送了代码为的邮件