2013-04-09 631 views
6

我在Exchange Online服务上有一个邮件帐户。现在,我想测试,如果我能够通过C#应用程序发送邮件给客户(在varoius域和微软的Office 365)在.net中发送测试Microsoft Office 365的SMTP电子邮件

我试图执行下面的代码,但我得到的错误

“根据验证 过程,远程证书无效。”

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

string[] strToList = "[email protected]"    
foreach (string strID in strToList) 
{ 
    if (strID != null) 
    { 
     mail.To.Add(new MailAddress(strID)); 
    } 
} 

mail.From = "[email protected]"; 
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("[email protected]", "mypassword"); 
client.Credentials = cred; 
client.Send(mail); 

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

+0

[发送使用Smtp.mail.microsoftonline.com电子邮件]的可能重复(http://stackoverflow.com/questions/6656039/sending-email-using-smtp-mail-microsoftonline-com) – bubbassauro 2013-08-05 16:16:35

+0

HTTP ://www.softdeveloperszone.com/2013/04/send-email-through-office-365-outlook.html。在这一个有一个裂缝。为我工作 – chamara 2016-11-13 09:04:55

回答

2

尝试使用:

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

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

+0

当我尝试上述我得到另一个异常“SMTP服务器需要安全连接或客户端未验证。服务器响应是:5.7.1客户端未验证” – user166013 2013-04-09 10:28:13

+0

你看:http: //support.microsoft.com/kb/2600912/en? – 2013-04-09 12:57:27

+0

@Garath链接已损坏:-( – johnnycardy 2014-08-13 13:42:08

1

的错误

SMTP服务器要求安全连接或客户端是不是 认证。服务器响应是:5.7.1客户端不是 认证

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

+0

我设置了一个专门用于通过SMTP登录的新用户。我得到了这个错误,因为在第一次登录时,用户需要更改密码。让我困惑了一会儿! – johnnycardy 2014-08-13 14:16:20

10

这对我的作品(从source编辑)


ThreadPool.QueueUserWorkItem(t => 
      { 
       SmtpClient client = new SmtpClient("smtp.office365.com",587); 
       client.EnableSsl = true; 
       client.Credentials = new System.Net.NetworkCredential("[email protected]", "password"); 
       MailAddress from = new MailAddress("[email protected]", String.Empty, System.Text.Encoding.UTF8); 
       MailAddress to = new MailAddress("[email protected]"); 
       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(); 
     } 
+0

有没有人知道如果可以做到这一点,而不需要输入明文密码? – TizzyFoe 2015-10-15 08:00:05

+0

我得到的回调没有错误,但它不发送邮件?你能帮我吗? – Peter 2015-11-06 13:17:57

+0

你有e.Error = null? e.Cancelled =真/假?尝试client.EnableSsl = false; – Zakos 2015-11-07 17:34:58

2

尝试smtp.office365.com代替smtp.outlook.office365.com

1

在某些情况下,TLS认证在c#中使用smtp.office365.com作为SMTP可能会导致问题。 尝试发送(MSG)语句(覆盖.TargetName)之前以下行:

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

这一个对我的作品

0

这也是发送邮件最好的办法。我在我的项目中尝试过,工作正常。

SmtpClient client = new SmtpClient("smtp.office365.com", 587); 
     client.EnableSsl = true; 
     client.Credentials = new System.Net.NetworkCredential("[email protected]", "[email protected]"); 
     MailAddress from = new MailAddress("From Address Ex [email protected]", String.Empty, System.Text.Encoding.UTF8); 
     MailAddress to = new MailAddress("From Address Ex [email protected]"); 
     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);