0

I have a task to troubleshoot why a C# app is failing to send automated e-mail messages. I carefully checked the source code, and could find absolutely nothing wrong.

Therefore, I tried to send e-mail from Thunderbird, the e-mail client I normally use. I specified the same SMTP relay, the same UID and password, and everything worked fine.

Trying to isolate the problem, I tried writing a very short C# console app to see what might be going wrong. I wrote the following:

using System.Net;
using System.Net.Mail;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = new SmtpClient("my.server.with.ssl", 465);
            client.EnableSsl = true;
            client.Credentials = new NetworkCredential("uid", "pwd");
            var message = new MailMessage("me@example.com", "myemail@mydomain", "Test Message Subject", "Test Message Body");
            client.Send(message);    
        }
    }
}

I entered the same credentials that I used in Thunderbird. When I run the above, I get the following error:

Unhandled Exception: System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.

How can it be that e-mail is sent just fine from Thunderbird, but the simple programming above fails to work when I try to send an e-mail from a basic C# app?

Edit I tried changing the port number to 587 as suggested in the Question that @stuartd linked to, and in that case, I get an error that says The operation timed out.

Edit I've tried using other e-mail servers and adjusting the settings, but nothing works so far. I've tried connecting to the same SMTP server that I use for my personal e-mail and it shows an error that the connection times out.

Edit I can't say why, but everything seems to be working now in spite of the fact that I didn't change any code. It seems as if something odd happened with my connection.

Vivian River
  • 31,198
  • 62
  • 198
  • 313
  • https://stackoverflow.com/questions/20228644/smtpexception-unable-to-read-data-from-the-transport-connection-net-io-connect#20252948 – stuartd Jul 12 '18 at 16:11
  • Possible duplicate of [SmtpException: Unable to read data from the transport connection: net_io_connectionclosed](https://stackoverflow.com/questions/20228644/smtpexception-unable-to-read-data-from-the-transport-connection-net-io-connect) – stuartd Jul 12 '18 at 16:13

2 Answers2

0

try using the default constructor and specify the host only. Also some servers require that the client be authenticated before the server sends e-mail on its behalf. Try changing the value of UseDefaultCredentials

var mail = new MailMessage();
mail.From = new MailAddress("xxx@gmx.net");
mail.To.Add("yyy@gmail.com");
mail.IsBodyHtml = true;
mail.Subject = "subject";
mail.Body = "content";

var client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Host = "mail.gmx.net";
client.EnableSsl = true;
client.Credentials = new NetworkCredential("user", "pass");
client.Send(mail);

if that doesn't help than tell us the server name if its the public one

VladL
  • 12,769
  • 10
  • 63
  • 83
  • smtp.sendgrid.net – Vivian River Jul 12 '18 at 16:59
  • It times out with a message: Failure sending mail. ---> Unable to connect to the remote server ---> A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. This is the same error I get if I tell it to connect to a host that is running no smtp relay at all. – Vivian River Jul 12 '18 at 17:07
  • Any chance your application uses proxy? Or maybe you are running it in the debugger and something like fiddler catches the request? – VladL Jul 13 '18 at 12:20
0

Try using:

SmtpClient smtpClient = new SmtpClient("mail.mymailhost.com");

smtpClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

Frank Ball
  • 1,039
  • 8
  • 15