2012-03-08 108 views
0

對於如此多的代碼,我很抱歉,但是我遇到了此表單的問題。我已經嘗試過每種方式來配置它,但我根本無法發送它。它所需要做的就是發送一封包含郵件正文的電子郵件,但每次點擊提交時都會提示錯誤(來自catch塊)。嘗試提交基本表單時出現SMTP問題

這是在C#ASP.NET中。

我是新來的發展,並希望任何幫助。提前致謝!

protected void submitButton_Click(object sender, EventArgs e) 
{ 
    if (Page.IsValid) 
    { 
     string messageBody = "//numerous textboxes, dropdown lists, and checkboxes"; 

     SmtpClient smtp = new SmtpClient(); 
     MailMessage message = new MailMessage(); 
     message.IsBodyHtml = false; 

     try 
     { 
      // Prepare to and from e-mail addresses 
      MailAddress fromAddress = new MailAddress("[email protected]");     
      MailAddress toAddress = new MailAddress(emailTextBox.Text); 

      message.From = fromAddress; 
      message.To.Add(toAddress); 
      message.Subject = "Contact Form Email"; 
      message.Body = messageBody; 

      // Server details 
      smtp.Host = "email.goidp.com"; 
      // Credentials 
      smtp.UseDefaultCredentials = true; 
      // Send the email 
      smtp.Send(message);     

      // Inform the user 
      noticeLabel.Visible = true; 
      noticeLabel.Attributes.CssStyle.Add("display", "block"); 
      //noticeLabel.Text = "E-mail sent"; 
      noticeLabel.Text = "Thank you, we have received your information and will be in touch soon."; 

      // and clear the form 
      fullNameTextBox.Text = String.Empty; 
      companyTextBox.Text = String.Empty; 
       //many others, this is just to give the idea; 

     } 
     catch (Exception ex) 
     { 
      errorLabel.Text = "Error sending e-mail:<br/>\n" + ex.Message; 
      Response.Redirect("estimate-error.html?error=1"); 
      errorLabel.Visible = true; 
      errorLabel.Attributes.CssStyle.Add("display", "block"); 
      errorLabel.Text = "Error sending e-mail:<br/>\n" + ex.Message; 
     } 
    } 
    else 
    { 
     //errorLabel.Text = "Error sending e-mail. Check required fields<br/>"; 
     //Response.Redirect("estimate-error.html?error=2"); 
     errorLabel.Visible = true; 
     errorLabel.Attributes.CssStyle.Add("display", "block"); 
     errorLabel.Text = "Error sending e-mail. Check required fields<br/>"; 
    } 
} 

我是這個網站的新手,所以如果我需要發佈更多的信息,我會的。謝謝!

+2

你得到了什麼錯誤? – alf 2012-03-08 19:48:34

+0

這只是來自catch塊的通用錯誤。它只是說:「發送電子郵件時出錯:發送請求失敗」 – Peter 2012-03-08 19:54:53

+2

您可以在catch塊的第一行設置斷點並檢查異常。它應該包含詳細信息,或者可能是一個內部異常 – alf 2012-03-08 19:57:32

回答

0

更可能的是,你將需要設置一些憑據SMTP對象。

smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "PasswordForEmail"); 

希望有所幫助!祝你好運!