2011-01-13 5 views
7

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

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

+0

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. –

+0

Qual è il server di posta in uso? – Hawxby

risposta

3

ho provato le impostazioni di configurazione da dentro LINQPad contro il mio server di posta hMailServer, e hanno funzionato grande. Quindi, la mia ipotesi è che il server di posta con cui comunichi sia una stretta di mano con il cliente in modo inaspettato. Quando ero test, ho catturato il registro SMTP dal mio server, ed ecco ciò che sembrava (igienizzato, ovviamente):

SENT: 220 my.mailserv.er ESMTP 
RECEIVED: EHLO CLIENTAPP 
SENT: 250-my.mailserv.er[nl]250-SIZE 25600000[nl]250 AUTH LOGIN 
RECEIVED: AUTH login bWFpbHRlc3RAbXkubWFpbHNlcnYuZXI= 
SENT: 334 UGFzc3dvcmQ6 
RECEIVED: *** 
SENT: 235 authenticated. 
RECEIVED: MAIL FROM:<[email protected]> 
SENT: 250 OK 
RECEIVED: RCPT TO:<[email protected]> 
SENT: 250 OK 
RECEIVED: DATA 
SENT: 354 OK, send. 

Il server richiede autenticazione SMTP, e si può vedere che dopo la mia client invia il Comando AUTH, il server risponde con il codice di stato 334 e la rappresentazione codificata in base 64 di Password:. Quindi, raccomanderei lo turning on the trace functionality per SmtpClient in modo da poter vedere cosa sta accadendo in entrambi gli scenari.

stavo correndo LINQPad 4,31, e il mio file linqpad.config contenuta:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.net> 
    <mailSettings> 
     <smtp from="[email protected]"> 
     <network host="my.mailserv.er" port="25" userName="[email protected]" password="supersecure"/> 
     </smtp> 
    </mailSettings> 
    </system.net> 
</configuration> 

La query LINQPad è stato il seguente:

SmtpClient mailer = new SmtpClient(); 

Guid g = Guid.NewGuid(); 
g.Dump("Message GUID"); 

MailMessage msg = new MailMessage(); 
msg.Subject = "Test:" + g; 
msg.To.Add(new MailAddress("[email protected]")); 
msg.Body = "I HAS A BODY"; 
mailer.Send(msg); 
1

Prova a impostare l'attributo deliveryMethod per forzare la configurazione per utilizzare la rete-config

<system.net> 
    <mailSettings> 
     <smtp deliveryMethod="network" from="[email protected]"> 
      <network host="servermail.outsourced.com" 
        port="2525" 
        defaultCredentials="false" 
        userName="username" 
        password="password"/> 
     </smtp> 
    </mailSettings> 
</system.net> 
+0

Grazie per la raccomandazione. Non ha avuto effetto. –

+0

Hai provato a eseguire il debug e controllare le proprietà del server SMTP dopo che sono state compilate correttamente quando si utilizza web.config per memorizzare le credenziali? Forse c'è un valore errato in là. – Scott

2

Aggiungere questo per l'accesso

Si prega di commentare ciò che Vengono utilizzati AuthenticationModules (li troverai dichiarati nel network.log). Nella mia casella viene usato costante SmtpLoginAuthenticationModule #, ma ce ne sono altri possibili.

<system.diagnostics> 
    <sources> 
     <source name="System.Net" tracemode="includehex"> 
     <listeners> 
      <add name="System.Net"/> 
     </listeners> 
     </source> 
     <source name="System.Net.Sockets"> 
     <listeners> 
      <add name="System.Net"/> 
     </listeners> 
     </source> 
     <source name="System.Net.Cache"> 
     <listeners> 
      <add name="System.Net"/> 
     </listeners> 
     </source> 

    </sources> 
    <switches> 
     <add name="System.Net" value="Verbose"/> 
     <add name="System.Net.Sockets" value="Verbose"/> 
     <add name="System.Net.Cache" value="Verbose"/> 
    </switches> 
    <sharedListeners> 
     <add name="System.Net" 
     type="System.Diagnostics.TextWriterTraceListener" 
     initializeData="network.log" 
     /> 
    </sharedListeners> 
    <trace autoflush="true"/> 

    </system.diagnostics> 
+0

Dove è possibile visualizzare i registri? – brenjt

0

Ragazzi Questo codice è ok provo a utilizzare devsmtp per la macchina locale e funzionerà bene

secondario protettivo Button1_Click (ByVal sender As Object, ByVal e come EventArgs) Maniglie Button1.Click

Using tmp = New SmtpClient() 
     Dim msg As New MailMessage() With {.Subject = "test", .From = New MailAddress("[email protected]")} 
     msg.[To].Add(New MailAddress("[email protected]")) 
     msg.Body = "test" 
     tmp.Send(msg) 
    End Using 

End Sub 

scaricare il devsmtp per la macchina locale da

http://download-codeplex.sec.s-msft.com/Download/Release?ProjectName=smtp4dev&DownloadId=269147&FileTime=129575689693530000&Build=20393