简体   繁体   English

如何使用C#(反馈表单)通过asp.net发送电子邮件

[英]how to send email with asp.net with c #(FeedBack Form)

I'm trying to create a feedback form. 我正在尝试创建一个反馈表。 Before that i was trying to make an email application but I could'nt get how to do that. 在此之前,我尝试制作电子邮件应用程序,但我无法做到这一点。

I found some code on the net. 我在网上找到了一些代码。 I understood the code but was not able to resolve the problem. 我了解代码,但无法解决问题。

my default.aspx page contain the following code 我的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>

my code-behind for default.aspx 我的代码为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
}

im getting the error here 我在这里得到错误

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

i dont know how to correct it. 我不知道如何纠正它。 my port no is 3168 i had tried to replace it. 我的端口号是3168,我曾尝试更换它。 but unable to resolve 但无法解决

the catch portion is getting "Failure sending Mail" im sending the detail/snapshort of error. 捕获部分正在“发送邮件失败”,即发送错误的详细信息/快照。

错误信息

在此处输入图片说明

错误信息

Right off the bat, I would say that your problem is the fact you have set localhost as your mail server. 马上,我想说您的问题是您已将localhost设置为邮件服务器。 Do you even have a mail server installed locally? 您甚至在本地安装了邮件服务器? If you have access to a real mail server on your live site, try setting the mail server to that address with appropriate password and username info. 如果您可以访问实时站点上的真实邮件服务器,请尝试使用适当的密码和用户名信息将邮件服务器设置为该地址。 Then see if them mail goes out. 然后查看他们的邮件是否发送出去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM