简体   繁体   中英

System.Net.Mail.SmtpException: Failure sending mail. Unable to read data from the transport connection

This is the relevant code (C#)

                    MailMessage m;
                    SmtpClient client;   

                    client.Host = "smtp.mail.yahoo.com";
                    client.Port = 465;  
                    client.EnableSsl = true;
                    client.Credentials = new NetworkCredential("mymail@yahoo.com", "mypassword");

                    m = new MailMessage();
                    m.From = new MailAddress("mymail@yahoo.com");
                    m.To.Add(new MailAddress("anothermail@gmail.com"));
                    m.Subject = "My subject";
                    m.Body = "This is my body. ";

                    client.Send(m);

This is the full error trace

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(ServicePoint servicePoint)
   at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
   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)
   at EmailSender.EmailSenderForm.send() in c:\Users\Ankur\Documents\Visual Studio 2012\Projects\EmailSender\EmailSender\EmailSenderForm.cs:line 91

What am I doing wrong here?

As far as I know, Yahoo doesn't allow 3rd party mail clients to operate their accounts. That's why you need webmail extensions with Thunderbird for example. Yahoo offers their Yahoo Mail Plus for these kind of services. Can you try your application with another mail account in Gmail? There it should work fine.

I have this code configured for an outlook account for sending emails. I have it working two years now without any modifications. It should work out of the box (just changing the account names):

MailMessage mm = new MailMessage(@"example@hotmail.com",
                     dest, textBox1.Text, richTextBox1.Text);
SmtpClient client = new SmtpClient("smtp.live.com", 587);
client.Credentials = new NetworkCredential(@"examplehotmail.com", "password");
client.EnableSsl = true;
messageSent = false;
client.SendAsync(mm, null);
client.SendCompleted += SentMessageTrigger;

I hope it helps.

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