开发者

SmtpClient won't authenticate when inflated from web.config

When using the system.net/mail web.config settings to configure my SmtpClient, it fails to deliver emails, with an "protocol error" described best by Base64 encoding and authentication problems:

Example:

With the follow开发者_如何学运维ing Config

<system.net>
    <mailSettings>
        <smtp from="email@server.com">
            <network host="servermail.outsourced.com"
                     port="2525" 
                     defaultCredentials="false" 
                     userName="username" 
                     password="password"/>
        </smtp>
    </mailSettings>
</system.net>

And the Code:

var tmp = new SmtpClient();

MailMessage msg = new MailMessage();
msg.Subject = "test";
msg.From = new MailAddress("me@server.com");
msg.To.Add(new MailAddress("me@server.com"));
msg.Body = "test";
tmp.Send(msg);

Produces the error message:

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)

However, in the following code where I manually set all the properties, the code runs without exception and the email is delivered.

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("me@server.com");
msg.To.Add(new MailAddress("me@server.com"));
msg.Body = "test";
tmp2.Send(msg);


I tried your config settings from within LINQPad against my hMailServer mail server, and they worked great. So, my guess is that the mail server you are communicating with is handshaking with the client in an unexpected fashion. When I was testing, I captured the SMTP log from my server, and here's what it looked like (sanitized, of course):

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:<mailtest@mailserv.er>
SENT: 250 OK
RECEIVED: RCPT TO:<happyuser@mailserv.er>
SENT: 250 OK
RECEIVED: DATA
SENT: 354 OK, send.

My server requires SMTP AUTH, and you can see that after my client sends the AUTH command, the server responds with status code 334, and the base-64 encoded representation of Password:. So, I'd recommend turning on the trace functionality for the SmtpClient so you can see what is occurring during both scenarios.

I was running LINQPad 4.31, and my linqpad.config file contained:

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

The LINQPad query was as follows:

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("happyuser@mailserv.er"));
msg.Body = "I HAS A BODY";
mailer.Send(msg);


Add this for logging

Please comment what AuthenticationModules are used (you'll find them stated in the network.log). On my box SmtpLoginAuthenticationModule# is constant used but there are others possible.

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


Try setting the deliveryMethod attribute to force the configuration to use the network-config

<system.net>
    <mailSettings>
        <smtp deliveryMethod="network" from="email@server.com">
            <network host="servermail.outsourced.com"
                     port="2525" 
                     defaultCredentials="false" 
                     userName="username" 
                     password="password"/>
        </smtp>
    </mailSettings>
</system.net>


Guys this code is ok i try using devsmtp for local machine and it work fine

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

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

End Sub

please download the devsmtp for local machine from

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜