Smtp异常,无法发送邮件
本文关键字:异常 Smtp | 更新日期: 2023-09-27 18:11:12
System.Net.Mail.SmtpException:
Failure sending mail. --->
System.Net.WebException:
Unable to connect to the remote server --->
System.Net.Sockets.SocketException:
No connection could be made because the target machine actively refused it xx.x.xx.xxx:25
我的SMTP主机是以上的IP地址,我无法收到邮件,它抛出上述异常
下面是我的代码
mail.From = new MailAddress(email.From.EmailAddress,email.From.FullName);
mail.Subject = email.Subject;
mail.Body = email.Body;
if (email.Body.Contains("<html>"))
{
mail.IsBodyHtml = true;
}
if (!email.ExcludeAttachment && !string.IsNullOrEmpty(email.AttachmentPath))
mail.Attachments.Add(new Attachment(email.AttachmentPath));
client = new SmtpClient();
client.Host = ConfigurationSettings.AppSettings["SmtpServer"].ToString();
client.Send(mail);
出现这种情况的最可能原因是需要对服务器进行身份验证。你可能也需要发送身份验证,看看我的工作代码,看看你是否可以调整它以适应你的需要。
也值得抓住Telrik Fiddler看看来自smtp服务器的响应是什么
var client = new SmtpClient
{
Host = txtOutGoingServer.Text,
Port = Convert.ToInt16(txtServerPort.Text),
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(txtUserName.Text, txtPassword.Password),
Timeout = 20000
};
try
{
m.To.Add(new MailAddress(dr[0].ToString().Trim()));
m.From = new MailAddress(txtUserName.Text);
foreach (var attachment in Attactments)
{
m.Attachments.Add(new Attachment(attachment));
}
client.Send(m);
m.To.Clear();
m.Attachments.Clear();
Success.Add(dr[0].ToString());
}
catch (SmtpException esException)
{
Errors.Add("Error sending to " + dr[0].ToString() + " " + esException.Message);
}
catch (Exception ex)
{
Errors.Add("Error sending to " + dr[0].ToString() + " " + ex.Message);
}
的方法,我采取这个工作代码是从循环通过电子邮件地址和联系人姓名的数据表,所以只是替换dr[].ToString()…