[英]smtp exception “failure sending mail”
我正在使用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);
我在以下之后遇到了异常情况...
System.IO.IOException:无法从传输连接中读取数据:net_io_connectionclosed。
在System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte []缓冲区,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.SmtpClient.GetConnection()
在System.Net.Mail.SmtpClient.Send(MailMessage消息)
检查InnerException的异常,应告诉您它为什么失败。
尝试将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);
}
}
这些信息将有助于识别问题所在。
确保您的防病毒软件阻止发送邮件。 以我为例,McAfee Access Protection Rules阻止发送邮件,取消阻止和报告。
我用了一些,但是如果发送邮件失败,我只检查这个:
default value is false;
但是不能将其更改为true;
smtpClient.Port = smtpServerPort;
smtpClient.UseDefaultCredentials = false;
smtpClient.Timeout = 100000;
smtpClient.Credentials = new System.Net.NetworkCredential(mailerEmailAddress, mailerPassword);
smtpClient.EnableSsl = EnableSsl;
所有功能都必须被try catch包围;
没有看到您的代码,很难找到异常原因。 以下是假设:
VSNL的serverHost是smtp.vsnl.net
例外:
Unable to read data from the transport connection: net_io_connectionclosed
通常,仅当用户名或密码不匹配时才会发生此异常。
检查是否通过IPv6地址引用了计算机。 在我的情况下,使用计算机名给了我同样的错误。 使用ip4地址可以正常工作(即10.0.0.4)。 我摆脱了ipv6,它开始起作用。
不是我要找的解决方案,但由于我对ipv6的了解有限,因此我不知道其他选择。
尝试删除smtpClient.EnableSsl = true; 我不确定vsnl是否支持SSL和您使用的端口号
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.