2013-04-09 15 views
6

Ho un account di posta sul servizio Exchange Online. Ora sto cercando di verificare se sono in grado di inviare mail ai clienti (su domini varoius e su Microsoft Office 365) attraverso il C#Invia prova e-mail SMTP Microsoft Office 365 in .net

ho provato l'attuazione del codice qui sotto, ma sto ottenendo l'errore

"Il certificato remoto non è valido in base alla procedura di convalida ."

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

Si prega di consulenza, se sto facendo qualcosa di sbagliato. Grazie mille in anticipo.

+0

possibile duplicato del [Invio di posta elettronica utilizzando 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. Fai uno schifo a questo. Ho lavorato per me – chamara

risposta

2

tenta di utilizzare:

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

Questo codice vi permetterà di accettare certificati non validi.

+0

quando ho provato quanto sopra sto ricevendo un'altra eccezione "Il server SMTP richiede una connessione sicura o il client non è stato autenticato.La risposta del server è stata: 5.7.1 Il client non è stato autenticato" – user166013

+0

Hai visto: http: //support.microsoft.com/kb/2600912/en? –

+0

@Garath link is broken :-( – johnnycardy

1

L'errore

il server SMTP richiede una connessione sicura o il client non era autenticato. Risposta del server: 5.7.1 Il client non è stato autenticato

spesso accade quando account utente associato password è scaduta o account è bloccato. Provare a impostare "Non scadere mai la password utente" in Active Directory, se non viola la politica della password aziendale :) Ciò è accaduto durante il test con o365 Exchange Online A/c.

+0

Ho configurato un nuovo utente appositamente per accedere tramite SMTP. Ho ricevuto questo errore perché durante il primo accesso, l'utente è stato richiesto di cambiare la password. Mi sono confuso per un po '! – johnnycardy

10

questo funziona per me (a cura di 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

Qualcuno sa se è possibile farlo senza inserire una password in testo normale? – TizzyFoe

+0

Non ricevo il callback senza errori, ma non invia una mail? Puoi aiutarmi? – Peter

+0

hai e.Error = null? e.Cancelled = true/false? prova client.EnableSsl = falso; – Zakos

2

Prova smtp.office365.com invece di smtp.outlook.office365.com

1

In alcuni casi l'autenticazione TLS potrebbe causare problemi nell'utilizzo di smtp.office365.com come SMTP da C#. Provare la seguente riga prima dell'istruzione Send (msg) (sovrascrivendo .TargetName):

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

Questo funziona per me

0

Questo è anche il modo migliore per inviare la posta. L'ho provato nel mio progetto e sto lavorando bene.

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