简体   繁体   English

smtp异常“发送邮件失败”

[英]smtp exception “failure sending mail”

I am making an SMTP mail application with C#.Net. 我正在使用C#.Net制作SMTP邮件应用程序。 It is working ok for Gmail settings, but I have to work it for a connection to VSNL. Gmail设置可以正常使用,但是我必须使用它才能连接到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. System.IO.IOException:无法从传输连接中读取数据:net_io_connectionclosed。

at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine) 在System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte []缓冲区,Int32偏移量,Int32读取,布尔值readLine)处
at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) 在System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader调用者,布尔oneLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) 在System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader调用方)
at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) 在System.Net.Mail.SmtpConnection.GetConnection(字符串主机,Int32端口)处
at System.Net.Mail.SmtpClient.GetConnection() 在System.Net.Mail.SmtpClient.GetConnection()
at System.Net.Mail.SmtpClient.Send(MailMessage message) 在System.Net.Mail.SmtpClient.Send(MailMessage消息)

检查InnerException的异常,应告诉您它为什么失败。

Try wrapping the Send call in a try catch block to help identify the underlying problem. 尝试将Send调用包装在try catch块中,以帮助识别潜在的问题。 eg 例如

 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... 这些信息将有助于识别问题所在。

Make sure your Anti Virus is blocking sending mails. 确保您的防病毒软件阻止发送邮件。 In my case McAfee Access protection Rules were blocking sending mails, untick blocks and reports. 以我为例,McAfee Access Protection Rules阻止发送邮件,取消阻止和报告。

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; 但是不能将其更改为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; 所有功能都必须被try catch包围;

Without seeing your code, it is difficult to find the reason for exception. 没有看到您的代码,很难找到异常原因。 Following are assumptions: 以下是假设:

The serverHost of VSNL is smtp.vsnl.net VSNL的serverHostsmtp.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 . 通常,仅当用户名或密码不匹配时才会发生此异常。

Check to see if the machine is being referred to by an IPv6 address. 检查是否通过IPv6地址引用了计算机。 In my case using machine name gave me the same error. 在我的情况下,使用计算机名给了我同样的错误。 Using the ip4 address it did work (ie 10.0.0.4). 使用ip4地址可以正常工作(即10.0.0.4)。 I got rid of ipv6 and it started to work. 我摆脱了ipv6,它开始起作用。

Not the solution i was looking for but given my limited understanding of ipv6 I did not know of other choices. 不是我要找的解决方案,但由于我对ipv6的了解有限,因此我不知道其他选择。

Try by removing smtpClient.EnableSsl = true; 尝试删除smtpClient.EnableSsl = true; I am not sure whether vsnl supports SSL and the port number you are using 我不确定vsnl是否支持SSL和您使用的端口号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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