2013-04-09 41 views
6

tôi có tài khoản thư trên dịch vụ Exchange Online. Bây giờ tôi đang cố gắng để kiểm tra xem tôi có thể gửi mail cho khách hàng (trên lĩnh vực varoius và trên Microsoft Office 365) thông qua C# ứng dụngGửi kiểm tra email SMTP Microsoft Office 365 in .net

tôi đã cố gắng thực hiện các mã dưới đây nhưng tôi đang nhận được lỗi

"Chứng chỉ từ xa không hợp lệ theo quy trình xác thực ".

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

Xin tư vấn nếu tôi đang làm điều gì sai trái. Cảm ơn rất nhiều trước.

+0

thể trùng lặp của [Gửi email sử dụng Smtp.mail.microsoftonline.com] (http://stackoverflow.com/questions/6656039/sending-email-using-smtp-mail-microsoftonline-com) – bubbassauro

+0

http : //www.softdeveloperszone.com/2013/04/send-email-through-office-365-outlook.html. Có một vết nứt ở cái này. Làm việc cho tôi – chamara

Trả lời

2

Cố gắng sử dụng:

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

Mã này sẽ cho phép bạn chấp nhận chứng chỉ không hợp lệ.

+0

khi tôi đã thử ở trên tôi nhận được ngoại lệ khác "Máy chủ SMTP yêu cầu kết nối an toàn hoặc ứng dụng khách chưa được xác thực. Phản hồi của máy chủ là: 5.7.1 Khách hàng không được xác thực" – user166013

+0

Bạn đã xem: http: //support.microsoft.com/kb/2600912/en? –

+0

@Garath liên kết bị hỏng :-( – johnnycardy

1

Các Lỗi

Máy chủ SMTP đòi hỏi một kết nối an toàn hoặc các khách hàng đã không chứng thực. Phản hồi của máy chủ là: 5.7.1 Khách hàng không phải là đã xác thực

thường xảy ra khi mật khẩu tài khoản người dùng được kết thúc hoặc tài khoản bị khóa. Hãy thử đặt "Không bao giờ hết hạn mật khẩu người dùng" trong Active Directory, nếu nó không vi phạm chính sách mật khẩu công ty của bạn :) Điều này xảy ra với tôi trong khi thử nghiệm với o365 Exchange Online A/c.

+0

Tôi thiết lập một người dùng mới cụ thể để đăng nhập qua SMTP. Tôi gặp lỗi này vì trong lần đăng nhập đầu tiên, người dùng đã được yêu cầu thay đổi mật khẩu của họ. Đã làm tôi bối rối trong một thời gian! – johnnycardy

10

này làm việc cho tôi (thay đổi nội dung từ 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

Có ai biết nếu có thể thực hiện việc này mà không cần nhập mật khẩu thuần văn bản không? – TizzyFoe

+0

Tôi nhận được trong gọi lại không có lỗi, nhưng nó không gửi một thư? Bạn có thể giúp tôi không? – Peter

+0

u có e.Error = null? e.Cancelled = true/false? thử client.EnableSsl = false; – Zakos

2

Hãy thử smtp.office365.com thay vì smtp.outlook.office365.com

1

Trong một số trường hợp xác thực TLS có thể gây ra sự cố khi sử dụng smtp.office365.com dưới dạng SMTP từ C#. Hãy thử dòng sau đây trước Send (msg) statement (trọng .TargetName):

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

một này làm việc cho tôi

0

Đây cũng là cách tốt nhất để gửi Mail. Tôi đã thử nó trong dự án của tôi và làm việc tốt.

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