简体   繁体   中英

Why can't I send e-mail from a C# app when I can send e-mail from a mail client just fine?

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.

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

Try using:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM