2011-12-13 57 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