Quando si utilizzano le impostazioni web.config di system.net/mail per configurare il mio SmtpClient, non riesce a recapitare e-mail, con un "errore di protocollo" descritto meglio dalla codifica e dall'autenticazione Base64 problemi:SmtpClient non si autentica quando gonfiato da web.config
Esempio:
con la configurazione seguente
<system.net>
<mailSettings>
<smtp from="[email protected]">
<network host="servermail.outsourced.com"
port="2525"
defaultCredentials="false"
userName="username"
password="password"/>
</smtp>
</mailSettings>
</system.net>
e al codice:
var tmp = new SmtpClient();
MailMessage msg = new MailMessage();
msg.Subject = "test";
msg.From = new MailAddress("[email protected]");
msg.To.Add(new MailAddress("[email protected]"));
msg.Body = "test";
tmp.Send(msg);
produce il messaggio di errore:
System.Net.Mail.SmtpException: The server committed a protocol violation The server response was: UGFzc3dvcmQ6
at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException
& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
Tuttavia, nel seguente codice in cui ho impostato manualmente tutte le proprietà, il codice viene eseguito, senza eccezione e l'e-mail viene consegnato.
var tmp2 = new SmtpClient("servermail.outsourced.com", 2525);
tmp2.Credentials = new NetworkCredential("username", "password");
tmp2.UseDefaultCredentials = false;
MailMessage msg = new MailMessage();
msg.Subject = "test";
msg.From = new MailAddress("[email protected]");
msg.To.Add(new MailAddress("[email protected]"));
msg.Body = "test";
tmp2.Send(msg);
Se decodifichi UGFzc3dvcmQ6 ottieni "Password:" quindi c'è un problema con la tua autenticazione apparentemente. L'ho testato io stesso senza problemi.Possibile duplicato: http://stackoverflow.com/questions/2380531/problem-in-sending-mail-with-smtpclient – thomasvdb
Questo era il mio sospetto iniziale, tranne che le stesse credenziali producono un errore quando impostate tramite la configurazione web, e funzionano quando impostato utilizzando il codice. –
Qual è il server di posta in uso? – Hawxby