2015-09-04 101 views
0
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地址上面我无法得到它的邮件是抛出上述异常。下面的smtp例外无法发送邮件

是我的代码

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); 
+0

显示您的代码。 – Arash

+0

mail.From = new MailAddress(email.From.EmailAddress,email.From.FullName); mail.Subject = email.Subject; mail.Body = email.Body; if(email.Body.Contains(“”)) { mail.IsBodyHtml = true; (email.AttachmentPath)) } 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); –

+0

这是我的代码。 –

回答

0

您得到这个最可能的原因是因为你需要对服务器进行身份验证。您可能还需要发送验证码,看看我的工作代码,看看您是否可以调整以适应您的需求。

这也可能是值得抓住Telrik提琴手查看响应就是从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); 

     } 

,我采取这一工作的代码的方法,从通过电子邮件地址和联系人姓名的一个DataTable循环,所以只需将dr [] .Tostring()...替换为任何值即可。

+0

正在让DNS超时。 –

+0

使用此代码?如果是这样,请尝试或超时,或增加值 –

+0

你确定smtp服务器也是正确的吗? –