smtp exception “failure sending mail”

2019-01-24 14:16发布

I am making an SMTP mail application with C#.Net. It is working ok for Gmail settings, but I have to work it for a connection to VSNL. I am getting an exception: "Failure sending mail"

My settings seem perfect. What is the problem? Why am I getting the exception?

MailMessage mailMsg = new MailMessage();
MailAddress mailAddress = new MailAddress("mail@vsnl.net");
mailMsg.To.Add(textboxsecondry.Text);
mailMsg.From = mailAddress;

// Subject and Body
mailMsg.Subject = "Testing mail..";
mailMsg.Body = "connection testing..";

SmtpClient smtpClient = new SmtpClient("smtp.vsnl.net", 25);

var credentials = new System.Net.NetworkCredential("mail@vsnl.net", "password");

smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = credentials;
smtpClient.Send(mailMsg);

I am getting an exception following...

System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.

at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine)
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)

标签: c# email smtp
7条回答
Viruses.
2楼-- · 2019-01-24 14:45

I used some but i am checking only this if send mail fails:

default value is false;

but it can't be change to true;

 smtpClient.Port = smtpServerPort;
 smtpClient.UseDefaultCredentials = false;
 smtpClient.Timeout = 100000;
 smtpClient.Credentials = new System.Net.NetworkCredential(mailerEmailAddress, mailerPassword);
 smtpClient.EnableSsl = EnableSsl;

All function must be surround by a try catch;

查看更多
我想做一个坏孩纸
3楼-- · 2019-01-24 14:54

Check the InnerException of the exception, should tell you why it failed.

查看更多
在下西门庆
4楼-- · 2019-01-24 14:55

Try by removing smtpClient.EnableSsl = true; I am not sure whether vsnl supports SSL and the port number you are using

查看更多
地球回转人心会变
5楼-- · 2019-01-24 14:58

Check to see if the machine is being referred to by an IPv6 address. In my case using machine name gave me the same error. Using the ip4 address it did work (i.e. 10.0.0.4). I got rid of ipv6 and it started to work.

Not the solution i was looking for but given my limited understanding of ipv6 I did not know of other choices.

查看更多
Melony?
6楼-- · 2019-01-24 14:59

Without seeing your code, it is difficult to find the reason for exception. Following are assumptions:

The serverHost of VSNL is smtp.vsnl.net

Exception:

Unable to read data from the transport connection: net_io_connectionclosed

Usually this exception occurs only when there is mismatch in username or password.

查看更多
放荡不羁爱自由
7楼-- · 2019-01-24 15:02

Try wrapping the Send call in a try catch block to help identify the underlying problem. e.g.

 try
 {
     smtpClient.Send(mailMsg);
 }
 catch (Exception ex)
 {
     Console.WriteLine(ex);   //Should print stacktrace + details of inner exception

     if (ex.InnerException != null)
     {
         Console.WriteLine("InnerException is: {0}",ex.InnerException);
     }
 }

This information will help identify what the problem is...

查看更多
登录 后发表回答