Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

<system.net>
    <mailSettings>
        <smtp from="[email protected]">
            <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("[email protected]");
msg.To.Add(new MailAddress("[email protected]"));
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("[email protected]");
msg.To.Add(new MailAddress("[email protected]"));
msg.Body = "test";
tmp2.Send(msg);
like image 537
Matt Murrell Avatar asked Jan 13 '11 21:01

Matt Murrell


People also ask

What is SmtpClient C#?

SMTP Client in C# and VB.NETThe Simple Mail Transfer Protocol (SMTP) is the only standard protocol for sending mail messages over the Internet. GemBox. Email enables you to work with the SMTP protocol in C# and VB.NET using an SmtpClient class.


3 Answers

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:<[email protected]>
SENT: 250 OK
RECEIVED: RCPT TO:<[email protected]>
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="[email protected]">
        <network host="my.mailserv.er" port="25" userName="[email protected]" 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("[email protected]"));
msg.Body = "I HAS A BODY";
mailer.Send(msg);
like image 188
arcain Avatar answered Nov 09 '22 11:11

arcain


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>
like image 44
rene Avatar answered Nov 09 '22 10:11

rene


Try setting the deliveryMethod attribute to force the configuration to use the network-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>
like image 1
Pauli Østerø Avatar answered Nov 09 '22 11:11

Pauli Østerø