smtp异常"发送邮件失败"

Aja*_*mar 6 c# email smtp

我正在用C#.Net制作一个SMTP邮件应用程序.它适用于Gmail设置,但我必须将其用于连接到VSNL.我得到一个例外:"发送邮件失败"

我的设置似乎很完美.问题是什么?为什么我得到例外?

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);
Run Code Online (Sandbox Code Playgroud)

我得到一个例外......

System.IO.IOException:无法从传输连接读取数据:net_io_connectionclosed.

在System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(字节[]缓冲液,的Int32偏移的Int32读,布尔的readLine)
在System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader呼叫者,布尔ONELINE)
在System.Net.Mail.SmtpReplyReaderFactory .ReadLine(SmtpReplyReader呼叫者)
在System.Net.Mail.SmtpConnection.GetConnection(字符串主机的Int32端口)在System.Net.Mail.SmtpTransport.GetConnection(字符串主机的Int32端口)
在System.Net.Mail.SmtpClient.GetConnection ()
System.Net.Mail.SmtpClient.Send(MailMessage消息)

fej*_*oco 17

检查异常的InnerException,应该告诉你失败的原因.


Grh*_*rhm 5

尝试Send在try catch块中包装调用以帮助识别潜在问题.例如

 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);
     }
 }
Run Code Online (Sandbox Code Playgroud)

这些信息将有助于确定问题所在......