我正在嘗試創建反饋表單。 在此之前,我試圖做一個電子郵件應用程序,但我不知道該怎麼做。如何用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"> </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 我曾嘗試更換它。 但無法解決
捕獲部分越來越「失敗發送郵件」 我發送錯誤的細節/快照。
你得到的ex.Message是什麼? – Pleun
請僅發佈_relevant_代碼和標記。你發佈了太多內容,很難看到問題。同時發佈實際的錯誤消息。 – Oded
@Raja你在'發送SMTP郵件smtpClient.Send(message)'''有什麼例外? – Neha