views:

1628

answers:

2

I just noticed that some of the email operations in my code have been failing and am trying to fix it up, however I keep getting the following error:

System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
 ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
   at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
   at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
   at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
   at System.Net.Mail.SmtpClient.GetConnection()
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   --- End of inner exception stack trace ---
   at System.Net.Mail.SmtpClient.Send(MailMessage message)

My code is fairly straightforward:

MailMessage mail = new MailMessage();
mail.To.Add(email_to);
mail.From = new MailAddress(email_from);
mail.Subject = email_subject;
mail.Body = email_body;


var client = new SmtpClient(smtp_server_url);
client.Credentials = new NetworkCredential(username, password, domain);
client.Timeout = 30000;
client.Send(mail);

Does anyone have any ideas what could be going on? Our exchange server administrator says she can't see anything in the log.

Also, not that this is a surprise, but the same thing happens when I try to send a message from powershell.

+1  A: 

Usually when I see that it's a configuration on the Exchange server. In our environment, our Network Admins must explicitly allow machines to use the Exchange server as a relay. If the machine my code is running on is not explicitly added to the server, I get this message. Hopefully that helps.

David Stratton
Thanks, but thats not it, I can send from outlook just fine. Its only with sending from .NET code that I have a problem
George Mauer
I am also suspicious of configuration/auth issues. Does it work if email_from and email_to are set to your email address (same as you use for exchange)?
jsight
You may already know this, so forgive me if I'm assuming incorrectly, but I wouldn't dismiss the exchange server settings just because you can email through outlook, Sending mail from code on a machine uses the System.net.Mail.SmtpClient, which connects via SMTP. That's different to how Outlook connects. The admins have to specifically allow the machine to use the server as a relay for smtp mail. This cab be by the IP of the machine and the client credentials or both.
David Stratton
@David, that is perhaps true, but it was working from other machines until recently. Might have something to do with the user being placed on active directory, but I'm passing the right domain. email_from and email_to are both my email address for the test.
George Mauer
+2  A: 

Got it!

Essentially it seems that this error (ErrorCode 10054) happens when you connect to a server successfully but then ask it to do something that it is not equipped to do.

In my case here is what happened:

  • I used to send mail through our front-facing mail server mail.mycompany.com
  • That is not the address of the exchange server though, it is the address you point your browser to to access outlook online.
  • This worked fine previously since non-web requests for that address were routed to the exchange server. At some point this policy was changed - in our case because spam management was handed off to AT&T. Of course I was not informed of any of this.
  • Since there was indeed a server running at that site it would let me connect to it but then drop the connection immediately (or possibly even not allow it to open all the way)
  • Setting the correct host-name fixed the problem.
George Mauer