如何使用c#发送带有asp.net的电子邮件(FeedBack表格)

时间:2011-12-13 12:04:17

标签: 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("admin1@yoursite.com");
            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("admin1@yoursite.com")
            message.CC.Add("admin1@yoursite.com");
            message.CC.Add("admin2@yoursite.com");

            // You can specify Address directly as string
            message.Bcc.Add(new MailAddress("admin3@yoursite.com"));
            message.Bcc.Add(new MailAddress("admin4@yoursite.com"));

            //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 个答案:

答案 0 :(得分:1)

马上,我会说你的问题是你将localhost设置为你的邮件服务器。你甚至在本地安装了邮件服务器吗?如果您可以访问实际站点上的真实邮件服务器,请尝试使用适当的密码和用户名信息将邮件服务器设置为该地址。然后看看他们的邮件是否熄灭了。