Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

smtp exception "failure sending mail"

Tags:

c#

email

smtp

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("[email protected]");
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("[email protected]", "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)

like image 772
Ajay_Kumar Avatar asked Dec 23 '10 08:12

Ajay_Kumar


Video Answer


2 Answers

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

like image 200
fejesjoco Avatar answered Sep 22 '22 19:09

fejesjoco


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...

like image 40
Grhm Avatar answered Sep 19 '22 19:09

Grhm