2015-04-06 11 views
11

Viene visualizzato un messaggio di errore quando si tenta di inviare un'e-mail tramite il mio servizio web. Ho provato ad abilitare l'accesso ad app meno sicure disabilitando la verifica in due passaggi e accedendo all'account tramite un browser web. Nessuna delle soluzioni su SO ha funzionato per me. Sto ancora ottenendo:Come inviare un'e-mail con C# tramite Gmail

Error: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

Cosa posso fare per risolvere questo problema?

namespace EmailService 
{ 
    public class Service1 : IService1 
    {  
     public string SendEmail(string inputEmail, string subject, string body) 
     { 
      string returnString = ""; 
      try 
      { 
       MailMessage email = new MailMessage(); 
       SmtpClient smtp = new SmtpClient(); 
       smtp.Host = "smtp.gmail.com"; 

       // set up the Gmail server 
       smtp.EnableSsl = true; 
       smtp.Port = 587; 
       smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "mypassword"); 
       smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
       smtp.UseDefaultCredentials = false; 

       // draft the email 
       MailAddress fromAddress = new MailAddress("[email protected]"); 
       email.From = fromAddress; 
       email.To.Add(inputEmail); 
       email.Subject = body; 
       email.Body = body; 

       smtp.Send(email); 

       returnString = "Success! Please check your e-mail."; 
      } 
      catch(Exception ex) 
      { 
       returnString = "Error: " + ex.ToString(); 
      } 
      return returnString; 
     } 
    } 
} 
+0

Vedere se questo aiuta: http://stackoverflow.com/questions/18503333/the-smtp-server-requires-a-secure-connection-o r-the-client-was-not-authenticated –

+1

Hai provato a eseguire il ping del server, in caso affermativo la soluzione menzionata da Eghbal Sohrabi è ok – Shubhojit

+0

@Shubhojit esegue il ping del server di Google? – Johnny

risposta

29

Just Go qui: Less secure apps, accedere utilizzando la tua email e la password che utilizzano per l'invio di posta elettronica nel codice C# e scegliere Turn On.

Inoltre si prega di andare a questo link e fare clic su Continua Allow access to your Google account

anche io modificarlo po ':

public string sendit(string ReciverMail) 
{ 
    MailMessage msg = new MailMessage(); 

    msg.From = new MailAddress("[email protected]"); 
    msg.To.Add(ReciverMail); 
    msg.Subject = "Hello world! " + DateTime.Now.ToString(); 
    msg.Body = "hi to you ... :)"; 
    SmtpClient client = new SmtpClient(); 
    client.UseDefaultCredentials = true; 
    client.Host = "smtp.gmail.com"; 
    client.Port = 587; 
    client.EnableSsl = true; 
    client.DeliveryMethod = SmtpDeliveryMethod.Network; 
    client.Credentials = new NetworkCredential("[email protected]", "YourPassword"); 
    client.Timeout = 20000; 
    try 
    { 
     client.Send(msg); 
     return "Mail has been successfully sent!"; 
    } 
    catch (Exception ex) 
    { 
     return "Fail Has error" + ex.Message; 
    } 
    finally 
    { 
     msg.Dispose(); 
    } 
} 

Se il codice di cui sopra non funzionano, provare a cambiarlo come il codice seguente:

SmtpClient client = new SmtpClient(); 
    client.Host = "smtp.gmail.com"; 
    client.Port = 587; 
    client.EnableSsl = true; 
    client.DeliveryMethod = SmtpDeliveryMethod.Network; 
    client.UseDefaultCredentials = false; 
    client.Credentials = new NetworkCredential("[email protected]", "YourPassword"); 
+0

Ho già provato entrambe le cose e ancora non funziona. – Johnny

+5

Si scopre che 'smtp.UseDefaultCredentials = false;' deve precedere 'smtp.Credentials = new System.Net.NetworkCredential (" [email protected] "," mypassword ");' Ho semplicemente scambiato quelle righe e tutto funziona correttamente bene. – Johnny

+0

significa cambiare 'smtp.UseDefaultCredentials = true;' a 'smtp.UseDefaultCredentials = false;' e posizionarlo su 'smtp.Credentials = new System.Net.NetworkCredential (" [email protected] "," mypassword ") ; se sì, mi dica di modificare la mia risposta. –

Problemi correlati