运行该示例需要注意的地方:
1.如果您的机器上装有瑞星等杀毒软件,请关闭其“邮件发送监控,及邮件接受监控”功能。
2.正确设置邮件服务器。
sentMail.aspx
< %@ Page language="c#" Codebehind="sentMail.aspx.cs" AutoEventWireup="false" Inherits="CommonFunction.test.sentMail" %>
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML>
< HEAD>
< title>sentMail< /title>
< meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
< meta name="CODE_LANGUAGE" Content="C#">
< meta name="vs_defaultClientScript" content="JavaScript">
< meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
< /HEAD>
< body MS_POSITIONING="GridLayout">
< TABLE id="Table1" border="1">
< TR>
< TD colSpan="2">< B>邮件发送< /B>< /TD>
< /TR>
< TR>
< TD colSpan="2">
< asp:Label id="lblMsg" runat="server" ForeColor="red" Font-Bold="True">< /asp:Label>< /TD>
< /TR>
< TR>
< TD style="WIDTH: 121px">收件人:< /TD>
< TD>
< asp:TextBox id="txtTo" runat="server">IninNet@163.com< /asp:TextBox>< /TD>
< /TR>
< TR>
< TD style="WIDTH: 121px">发件人:< /TD>
< TD>
< asp:TextBox id="txtFrom" runat="server">Fandasy@163.com< /asp:TextBox>< /TD>
< /TR>
< TR>
< TD style="WIDTH: 121px">邮件主题:
< TD>
< asp:TextBox id="txtSubject" runat="server">< /asp:TextBox>< /TD>
< /TR>
< TR>
< TD colSpan="2">邮件内容:< /TD>
< /TR>
< TR>
< TD colSpan="2">
< asp:TextBox id="txtMsg" runat="server" TextMode="MultiLine" Rows="10" Columns="50">< /asp:TextBox>< /TD>
< /TR>
< TR>
< TD style="WIDTH: 121px">< FONT face="宋体">
< /asp:Button>< /FONT>< /TD>
< /TR>
< /TABLE>
< /form>
< /body>
< /HTML>
sentMail.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
namespace CommonFunction.test
{
///
/// sentMail 的摘要说明。
///
public class sentMail : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected System.Web.UI.WebControls.Label lblMsg;
protected System.Web.UI.WebControls.TextBox txtTo;
protected System.Web.UI.WebControls.TextBox txtFrom;
protected System.Web.UI.WebControls.TextBox txtSubject;
protected System.Web.UI.WebControls.TextBox txtMsg;
protected System.Web.UI.WebControls.Button btnSend;
private void Page_Load(object sender, System.EventArgs e)
{
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnSend_Click(object sender, System.EventArgs e)
{
//创建MailMessage对象
MailMessage mailMsg = new MailMessage();
//设置收件人的邮件地址
mailMsg.To = txtTo.Text;
//设置发送者的邮件地址
mailMsg.From = txtFrom.Text;
//设置邮件主题
mailMsg.Subject = txtSubject.Text;
//设置邮件内容
mailMsg.Body = txtMsg.Text;
//设置支持服务器验证
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//设置用户名
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername","
songxiuduo_999@163.com");
//设置用户密码
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword","sxd999");
//try
//{
//设置发送邮件服务器
SmtpMail.SmtpServer = "smtp.163.com";
//发送邮件
SmtpMail.Send(mailMsg);
lblMsg.Text = "发送成功";
//}
//catch
//{
//lblMsg.Text = "发送失败";
//}
}
}
}
|