2013-06-24 196 views
1

我特別製作本教程爲基礎:http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp發送電子郵件

我成功地調試錯誤,但隨後它似乎並沒有工作...幫助,請...

前瞻:

my contact us form

後面的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Net; 

公共部分類默認設置2:System.Web.UI.Page { 保護無效的Page_Load(對象發件人,EventArgs的){

} protected void SendMail() { // Gmail Address from where you send the mail var fromAddress = "[email protected]"; // any address where the email will be sending var toAddress = YourEmail.Text.ToString(); //Password of your gmail address const string fromPassword = "Password"; // Passing the values and make a email formate to display string subject = YourSubject.Text.ToString(); string body = "From: " + YourName.Text + "\n"; body += "Email: " + YourEmail.Text + "\n"; body += "Subject: " + YourSubject.Text + "\n"; body += "Question: \n" + Comments.Text + "\n"; // smtp settings 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); } protected void Button1_Click(object sender, EventArgs e) { try { //here on button click what will done SendMail(); DisplayMessage.Text = "Message sent!"; DisplayMessage.Visible = true; YourSubject.Text = ""; YourEmail.Text = ""; YourName.Text = ""; Comments.Text = ""; } catch (Exception) { } }}

+0

您需要提供什麼行不通更多的細節,什麼錯誤您遇到 –

+0

其發生錯誤,在那裏.. –

+0

沒有郵件被髮送... – mitche027

回答

0
+0

我使用587成功,但它是Google ... var client = new SmtpClient(「smtp.gmail.com」,587) { Credentials = new NetworkCredential(「acct @ gma il.com「,」pw「), EnableSsl = true }; – smoore4

+0

嘗試....仍然不起作用 – mitche027

+0

登錄到您的帳戶並選中已發送文件夾。它'smtp.Send'方法不會拋出異常,這意味着連接是正確的。 –

0

有您是否嘗試評論該文章/聯繫作者?

我想有兩件事情在這裏:

答:您還沒有這樣做的catch塊東西。你應該考慮以下在catch塊

catch(Exception ex){ 
DisplayMessage.Text = ex.Message; 
DisplayMessage.Visible = true; 
} 

B.檢查您正在使用的Gmail帳戶是否已啓用pop3/imap smtp。

0

試試這個

protected void SendMail() 
    { 
     // any address where the email will be sending 
     var toAddress = YourEmail.Text.ToString(); 
     //Password of your gmail address 
     const string fromPassword = "Password"; 
     // Passing the values and make a email formate to display 
     string subject = YourSubject.Text.ToString(); 
     string body = "From: " + YourName.Text + "\n"; 
     body += "Email: " + YourEmail.Text + "\n"; 
     body += "Subject: " + YourSubject.Text + "\n"; 
     body += "Question: \n" + Comments.Text + "\n"; 

     SmtpClient smtpClient = new SmtpClient(); 
     MailMessage message = new MailMessage(); 
     MailAddress fromAddress = new MailAddress("[email protected]"); 
     smtpClient.Host = "smtp.gmail.com"; 
     smtpClient.Port = 587; 
     smtpClient.UseDefaultCredentials = false; 
     smtpClient.EnableSsl = true; 
     smtpClient.Credentials = new System.Net.NetworkCredential("[email protected]", fromPassword); 
     message.From = fromAddress; 
     message.To.Add(toAddress); 
     message.Subject = subject; 
     message.Priority = MailPriority.High; 
     message.Body = body; 
     message.IsBodyHtml = true; 
     smtpClient.Send(message); 
    }