2013-04-09 85 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);