2013-05-05 69 views
0

發送郵件預期 但現在我的要求是從Windows Mail中server.whenever發送郵件我嘗試發送郵件它顯示給我錯誤發送郵件失敗。如何在asp.net從我試圖從Gmail和其他共享發送郵件託管郵件服務器和其作品的Windows Mail服務器

SmtpClient client = new SmtpClient(); 
client.DeliveryMethod = SmtpDeliveryMethod.Network; 

client.Host = "us2.webmail.mailhostbox.com"; 
string fromaddress = "[email protected]"; 
client.Port = 25; 
//client.Host = "mail.csisite.com"; 
// setup Smtp authentication 
System.Net.NetworkCredential credentials = 
    new System.Net.NetworkCredential("[email protected]", "password"); 
//client.UseDefaultCredentials = true; 
client.Credentials = credentials; 
client.EnableSsl = false; 
try 
{ 

//mail For Customer 
MailMessage msgcustomer = new MailMessage(); 
msgcustomer.From = new MailAddress(fromaddress); 
msgcustomer.To.Add(new MailAddress(EmailID)); 

msgcustomer.Subject = "Thank you for writing to us" ; 
msgcustomer.IsBodyHtml = true; 
msgcustomer.Body = "Test Mail"; 
client.Send(msgcustomer) 

回答

0

使用System.Net.Mail

MailMessage msgMail = new MailMessage(); 

    MailMessage myMessage = new MailMessage(); 
    myMessage.From = new MailAddress("sender's email","sender`s name and surname"); 
    myMessage.To.Add("recipient's email"); 
    myMessage.Subject = "Subject"; 
    myMessage.IsBodyHtml = true; 

    myMessage.Body = "Message Body"; 


    SmtpClient mySmtpClient = new SmtpClient(); 
    System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential("email", "password"); 
    mySmtpClient.Host = "your smtp host address"; 
    mySmtpClient.UseDefaultCredentials = false; 
    mySmtpClient.Credentials = myCredential; 
    mySmtpClient.ServicePoint.MaxIdleTime = 1; 

    mySmtpClient.Send(myMessage); 
    myMessage.Dispose(); 

更新的用戶越來越以下錯誤

{ 「A插座操作嘗試無法訪問網絡208.91.199.230:25」}和錯誤代碼是10051

的錯誤10051是當目標網絡或主機服務器無法到達出現一個套接字錯誤。錯誤可能是由於路由器的錯誤配置,Internet斷開連接或防火牆阻止操作導致的。大多在試圖生成Internet控制消息協議(ICMP),並與錯誤配置簡單郵件傳輸協議(SMTP)連接時發生此錯誤。這個錯誤也表明,使用Windows的軟件配置爲傳送到路由器,因爲10065錯誤,如果Windows沒有設置鏈接到一個路由器,而不是出現。

你應該確保網絡路徑正確,計算機沒有運行兩個軟件防火牆和目標計算機未關閉。

+0

想讓我知道在我有wriiten代碼wats錯誤 – user2106954 2013-05-05 16:12:39

+0

什麼是你正在越來越內在的例外..使用默認的身份驗證爲您的服務器我越來越關注exeption {「試圖訪問套接字的方式被其訪問權限208.91.199.232:25「}禁止。檢查你的內部異常,看看是什麼 – 2013-05-05 16:21:59

+0

內部例外是{「一個套接字操作嘗試到一個無法訪問的網絡208.91.199.230:25」} 和錯誤代碼是10051 – user2106954 2013-05-05 16:36:39

0

發送郵件:

var smtp = new System.Net.Mail.SmtpClient(); 
{ 
    smtp.Host = "smtp.gmail.com"; 
    smtp.Port = 587; 
    smtp.EnableSsl = true; 
    smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
    smtp.Credentials = new NetworkCredential(fromAddress, fromPassword); 
    smtp.Timeout = 20000; 
} 
// Passing values to smtp object 
smtp.Send(fromAddress, toAddress, subject, body); 
+0

我想使用Windows Server而不是Gmail的SMTP發送郵件。 – user2106954 2013-05-05 16:18:07

0

使用此代碼發送郵件,這是經過測試的代碼。

public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body) 
    { 
     // Instantiate a new instance of MailMessage 
     MailMessage mMailMessage = new MailMessage(); 

     // Set the sender address of the mail message 
     mMailMessage.From = new MailAddress(from); 
     // Set the recepient address of the mail message 
     mMailMessage.To.Add(new MailAddress(to)); 

     // Check if the bcc value is null or an empty string 
     if ((bcc != null) && (bcc != string.Empty)) 
     { 
      // Set the Bcc address of the mail message 
      mMailMessage.Bcc.Add(new MailAddress(bcc)); 
     }  // Check if the cc value is null or an empty value 
     if ((cc != null) && (cc != string.Empty)) 
     { 
      // Set the CC address of the mail message 
      mMailMessage.CC.Add(new MailAddress(cc)); 
     }  // Set the subject of the mail message 
     mMailMessage.Subject = subject; 
     // Set the body of the mail message 
     mMailMessage.Body = body; 

     // Set the format of the mail message body as HTML 
     mMailMessage.IsBodyHtml = true; 
     // Set the priority of the mail message to normal 
     mMailMessage.Priority = MailPriority.Normal; 

     // Instantiate a new instance of SmtpClient 
     SmtpClient mSmtpClient = new SmtpClient(); 
     // Send the mail message 
     mSmtpClient.Host = "smtp.gmail.com"; //Or Your SMTP Server Address 
     mSmtpClient.Credentials = new System.Net.NetworkCredential 
      ("[email protected]", "Password"); 
     //Or your Smtp Email ID and Password 
     mSmtpClient.EnableSsl = true; 

     mSmtpClient.Send(mMailMessage); 
    } 
0

注:我一直在試圖通過我的ISP的郵件服務器,但沒有成功發送電子郵件 - 以下所有我能找到這些論壇上的建議。我剛剛發現,如果我不嘗試更改默認憑據設置,它將正常工作。如果我包括:client.UseDefaultCredentials = true;或client.UseDefaultCredentials = false;在我的代碼中,然後我會從服務器獲取登錄失敗消息。通過不包括,它運作良好。

相關問題