2015-09-04 88 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服務器也是正確的嗎? –