2013-03-28 180 views
-2

我正在嘗試創建一個電子郵件頁面部分,並且我在下面的代碼中找到了一篇文章和其中一個答案,但仍然說發送郵件失敗,我幾乎找不到原因?。我需要幫助才能找到補救措施,以解釋爲什麼我的代碼無法發送電子郵件。在這裏我的代碼供您參考。請指教......謝謝。發送郵件失敗

protected void Button1_Click(object sender, EventArgs e) 
{ 

SmtpClient client = new SmtpClient(); 

     client.Host = "smtp.live.com"; //Im not sure about this,I just copy it from the article 
     client.Port = 4548; //This is my ASP.Net Development Server Port,Im not sure also if this is correct. 

     client.Credentials = new System.Net.NetworkCredential(
      @"email_account", 
      @"email_password"); //Im not sure about this code if this correct, I just copy it 


     client.EnableSsl = true; 

     // create message 
     MailMessage message = new MailMessage(); 
     message.From = new MailAddress(TextBox4.Text, "Mackmellow"); //Textbox4 is my email address 
     message.To.Add(new MailAddress(TextBox1.Text, "Mackmellow")); // Textbox1 is the email add I want to send 
     message.Subject = TextBox2.Text; //Textbox2 is my subject 
     message.Body = TextBox3.Text; // Textbox3 is my message 
     message.BodyEncoding = System.Text.Encoding.UTF8; 
     message.IsBodyHtml = true; 

     message.SubjectEncoding = System.Text.Encoding.UTF8; 


     // send message 
     try 
     { 
      client.Send(message); 


     } 
     catch (SmtpException ex) 
     { 

      Response.Write(ex.Message); 
     } 
     finally 
     { 
      // Clean up. 
      message.Dispose(); 
     } 

請糾正我的代碼並告訴我我失蹤了什麼?在此先感謝...

+2

的就是你得到實際的錯誤信息(和/或內部異常)? 「發送郵件失敗」就像告訴汽車技工「汽車不會運行」一般。更多細節總是一件好事:) – Tim

+0

失敗是否有錯誤? –

+0

您必須確保您的電子郵件(from_email)屬於電子郵件提供商(在您的情況下爲「smtp.live.com」),否則它將失敗,並且您的端口不正確,應該是live.com接受的任何內容smtp on – Alex

回答

2

對於發送郵件,您不能使用您的開發服務器端口。

您必須使用郵件服務器的smtp服務器端口。

爲smtp.live.com你應該使用端口25或587

對於下面這段代碼,指定有效的登錄信息(電子郵件/密碼)

client.Credentials = new System.Net.NetworkCredential(
      @"email_account", 
      @"email_password"); 
+0

我試圖分別使用端口25和587,但仍顯示一條錯誤消息,指出「郵箱不可用」。服務器響應是:5.7.3請求的操作中止;用戶未經過身份驗證' – timmack

+0

您必須在網絡憑證中指定有效的用戶名/密碼組合...修改您複製的值:) – scartag

+0

是我的電子郵件地址或我想發送給的電子郵件地址嗎? – timmack

-2

試試這個。

SmtpClient SmtpServer = new SmtpClient("smtp.live.com"); 
    var mail = new MailMessage(); 
    mail.From = new MailAddress("[email protected]"); 
    mail.To.Add("ToGmail.com"); 
    mail.Subject = "Your Sub"; 
    mail.IsBodyHtml = true; 
    string htmlBody; 
    htmlBody = "HTML code"; 
    mail.Body = htmlBody; 
    SmtpServer.Port = 587; 
    SmtpServer.UseDefaultCredentials = false; 
    SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "YourPassword"); 
    SmtpServer.EnableSsl = true; 
    SmtpServer.Send(mail); 
+0

謝謝,我試圖使用你的代碼,但仍然給我一個運行時異常。這是錯誤消息:'/ WebSite1'應用程序中的服務器錯誤。 郵箱不可用。服務器響應是:5.7.3請求的操作中止;用戶未通過身份驗證 說明:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。 異常詳細信息:System.Net.Mail.SmtpException:郵箱不可用。服務器響應是:5.7.3請求的操作中止;用戶未經過身份驗證 – timmack

0

只提供G-mail帳號標識然後檢查它。我認爲SmtpClient不接受任何其他域名郵件地址。所以你可以只輸入gmail地址然後檢查它。

編輯

查看本示例和其他答案。

Error sending mail from Asp.Net/C#

how to send email in asp.net to any destination eg yahoo,gmail,hotmail etc c# code

+1

是Ramesh,其實在From部分我用我的電子郵件地址是@ gmail.com,並且我在代碼中也驗證了正確的密碼,但仍然給出了相同的錯誤。 – timmack

+0

但我發送的地址是@ yahoo.com – timmack

+0

Hai檢查此示例 http://stackoverflow.com/questions/6659210/unable-to-send-email-to-other-domains-using-system-net -mail-smtpclient –