2011-12-13 54 views
0

我正在嘗試創建反饋表單。 在此之前,我試圖做一個電子郵件應用程序,但我不知道該怎麼做。如何用c#發送帶有asp.net的電子郵件(反饋表)

我在網上找到了一些代碼。 我瞭解代碼,但無法解決問題。

我Default.aspx頁包含下面的代碼

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title>Send Email by .Net 2.0</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <h2 style="background-color:Brown; color:Wheat; font-family:Verdana; font-size:14px" align=center>Please enter the following requested 
       information below to send us your comments.</h2> 
      <table align=center> 
       <tr> 
        <td style="height: 26px"><span style="font-family:Verdana; font-size:12px; font-weight:bold;color:Brown;">Your Name:</span></td> 
        <td style="height: 26px"><asp:textbox id="txtName" Width="241" Runat="server"></asp:textbox></td> 
       </tr> 
       <tr> 
        <td><span style="font-family:Verdana; font-size:12px; font-weight:bold;color:Brown;">Your Email Address:</span></td> 
        <td><asp:textbox id="txtEmail" Width="241" Runat="server"></asp:textbox></td> 
       </tr> 
       <tr> 
        <td colSpan="2" ><span style="font-family:Verdana; font-size:12px; font-weight:bold; color:Brown;">Your Comment:</span></td> 
       </tr> 
       <tr> 
        <td align="center" colSpan="2" width=100%><asp:textbox id="txtMessage" Width="100%" Runat="server" Height="99" TextMode="MultiLine" MaxLength="400"></asp:textbox></td> 
       </tr> 
       <tr> 
        <td colSpan="2">&nbsp;</td> 
       </tr> 
       <tr> 
        <td align=center><asp:button id="btnSendmail" Runat="server" Text="Send Mail" OnClick="btnSendmail_Click"></asp:button></td> 
        <td align=center><asp:button id="btnReset" Runat="server" Text="Reset" OnClick="btnReset_Click"></asp:button></td> 
       </tr> 
       <tr> 
        <td colSpan="2"><asp:label id="lblStatus" Runat="server" EnableViewState="False"></asp:label></td> 
       </tr> 
      </table> 
    </div> 
    </form> 
</body> 
</html> 

我的代碼隱藏的Default.aspx

using System; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 
using System.Net.Mail; 

public partial class _Default : System.Web.UI.Page 
{ 
    #region "Send email" 
    protected void btnSendmail_Click(object sender, EventArgs e) 
    { 
     // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0 
     // System.Net.Mail.SmtpClient is the alternate class for this in 2.0 
     SmtpClient smtpClient = new SmtpClient(); 
     MailMessage message = new MailMessage(); 

     try 
     { 
      MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text); 

      // You can specify the host name or ipaddress of your server 
      // Default in IIS will be localhost 
      smtpClient.Host = "localhost"; 

      //Default port will be 25 
      smtpClient.Port = 1159; 

      //From address will be given as a MailAddress Object 
      message.From = fromAddress; 

      // To address collection of MailAddress 
      message.To.Add("[email protected]"); 
      message.Subject = "Feedback"; 

      // CC and BCC optional 
      // MailAddressCollection class is used to send the email to various users 
      // You can specify Address as new MailAddress("[email protected]") 
      message.CC.Add("[email protected]"); 
      message.CC.Add("[email protected]"); 

      // You can specify Address directly as string 
      message.Bcc.Add(new MailAddress("[email protected]")); 
      message.Bcc.Add(new MailAddress("[email protected]")); 

      //Body can be Html or text format 
      //Specify true if it is html message 
      message.IsBodyHtml = false; 

      // Message body content 
      message.Body = txtMessage.Text; 

      // Send SMTP mail 
      smtpClient.Send(message); 

      lblStatus.Text = "Email successfully sent."; 
     } 
     catch (Exception ex) 
     { 
      lblStatus.Text = "Send Email Failed.<br>" + ex.Message; 
     } 
    } 
    #endregion 

    #region "Reset" 
    protected void btnReset_Click(object sender, EventArgs e) 
    { 
     txtName.Text = ""; 
     txtMessage.Text = ""; 
     txtEmail.Text = ""; 
    } 
    #endregion 
} 

即時得到這裏的錯誤

// Send SMTP mail 
smtpClient.Send(message); 

我不知道如何糾正它。 我的港口號碼是3168 我曾嘗試更換它。 但無法解決

捕獲部分越來越「失敗發送郵件」 我發送錯誤的細節/快照。

error message

enter image description here

error message

+1

你得到的ex.Message是什麼? – Pleun

+0

請僅發佈_relevant_代碼和標記。你發佈了太多內容,很難看到問題。同時發佈實際的錯誤消息。 – Oded

+0

@Raja你在'發送SMTP郵件smtpClient.Send(message)'''有什麼例外? – Neha

回答

1

馬上蝙蝠,我會說,你的問題是你已經設置localhost作爲你的郵件服務器的事實。你甚至有本地安裝的郵件服務器?如果您可以訪問實時站點上的真實郵件服務器,請嘗試使用適當的密碼和用戶名信息將郵件服務器設置爲該地址。然後看看他們的郵件是否出去。

+0

我的想法一樣,以解決error.can請你告訴我如何安裝郵件服務器。 – Raja