《C# code》
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
MailAddress from = new MailAddress(TextBox4.Text,"ZL");
MailAddress to = new MailAddress(TextBox1.Text,"zl");
MailMessage oMail = new MailMessage(from, to);
oMail.Subject = TextBox2.Text;
oMail.Body = TextBox3.Text;
oMail.IsBodyHtml = true;
oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
oMail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
client.Host = "smtp.163.com";
client.Credentials = new NetworkCredential(TextBox4.Text, TextBox5.Text);
try
{
client.Send(oMail);
Label1.Text = "恭喜你!邮件发送成功。";
}
catch
{
Label1.Text = "邮件发送失败,检查网络及信箱是否可用。";
}
oMail.Dispose();
TABLE1.Visible = false;
Table2.Visible = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect(Request.Url.ToString());
TABLE1.Visible = true;
Table2.Visible = false;
}
}
《邮件发送前台页面》
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ValidateRequest="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="TABLE1" runat="server" border="0" cellpadding="0" cellspacing="0">
<tr>
<td >
收信:<asp:TextBox ID="TextBox1" runat="server" Width="150px"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="发信人"></asp:Label>
<asp:TextBox ID="TextBox4" runat="server" Width="134px"></asp:TextBox><br />
主题:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text="密码"></asp:Label>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br />
内容:<asp:TextBox ID="TextBox3" runat="server" Height="154px" TextMode="MultiLine"
Width="336px"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="发送" onClick="Button1_Click" /></td>
</tr>
</table>
</div>
<table id="Table2" runat="server" border="0" cellpadding="0" cellspacing="0" visible="false">
<tr>
<td align="center" >
<asp:Label ID="Label1" runat="server" ForeColor="Red" Text="恭喜您,发表成功!"></asp:Label><br />
<asp:Button ID="Button2" runat="server" Text="返回" onClick="Button2_Click" /></td>
</tr>
</table>
</form>
</body>
</html>