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

时间:2013-04-09 10:13:26

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

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

如果我做错了,请告知。 非常感谢。

8 个答案:

答案 0 :(得分:13)

这对我有用(从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();
        }

答案 1 :(得分:4)

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

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

这个适用于我

答案 2 :(得分:4)

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

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

答案 3 :(得分:2)

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

答案 4 :(得分:1)

错误

  

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

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

答案 5 :(得分:1)

尝试设置:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

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

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

对于测试目的可能很好-但这是主要的安全风险
请勿在产品帐户或产品环境中使用。

答案 6 :(得分:0)

尝试使用:

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

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

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

答案 7 :(得分:0)

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

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