<%
@ Page language
=
"
c#
"
Codebehind
=
"
SendEmail.aspx.cs
"
AutoEventWireup
=
"
false
"
Inherits
=
"
WebApplication1.SendEmail
"
%>
<! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.0 Transitional//EN " >
< HTML >
< HEAD >
< title > SendEmail </ 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 " >
< form id = " Form1 " method = " post " runat = " server " >
< FONT face = " 宋体 " >
< DIV >
< TABLE id = " Table1 " style = " Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px " cellSpacing = " 0 "
cellPadding = " 0 " width = " 776 " align = " center " border = " 0 " >
< TR >
< TD >
< TABLE id = " Table2 " cellSpacing = " 1 " cellPadding = " 4 " width = " 600 " align = " center " bgColor = " #cccccc "
border = " 0 " >
< TR >
< TD align = " center " bgColor = " #f0f0f0 " colSpan = " 2 " > 电子邮件发送程 序 </ TD >
</ TR >
< TR >
< TD align = " right " width = " 150 " bgColor = " #f0f0f0 " > 发送人: </ TD >
< TD align = " left " bgColor = " #ffffff " >
< asp:TextBox id = " fromMail " runat = " server " Width = " 300 " ></ asp:TextBox ></ TD >
</ TR >
< TR >
< TD align = " right " bgColor = " #f0f0f0 " > 收件人: </ TD >
< TD align = " left " bgColor = " #ffffff " >
< asp:TextBox id = " toMail " runat = " server " Width = " 300 " ></ asp:TextBox ></ TD >
</ TR >
< TR >
< TD align = " right " bgColor = " #f0f0f0 " > 抄送人: </ TD >
< TD align = " left " bgColor = " #ffffff " >
< asp:TextBox id = " ccMail " runat = " server " Width = " 300 " ></ asp:TextBox ></ TD >
</ TR >
< TR >
< TD align = " right " bgColor = " #f0f0f0 " > 暗送人: </ TD >
< TD align = " left " bgColor = " #ffffff " >
< asp:TextBox id = " bccMail " runat = " server " Width = " 300 " ></ asp:TextBox ></ TD >
</ TR >
< TR >
< TD align = " right " bgColor = " #f0f0f0 " > 主 & nbsp; & nbsp; & nbsp; & nbsp;题 : </ TD >
< TD align = " left " bgColor = " #ffffff " >
< asp:TextBox id = " subject " runat = " server " Width = " 300 " ></ asp:TextBox ></ TD >
</ TR >
< TR >
< TD align = " right " bgColor = " #f0f0f0 " > 附 & nbsp; & nbsp; & nbsp; & nbsp;件 : </ TD >
< TD align = " left " bgColor = " #ffffff " >< INPUT id = " upfile " type = " file " name = " upfile " runat = " server " ></ TD >
</ TR >
< TR >
< TD align = " right " bgColor = " #f0f0f0 " > 内 & nbsp; & nbsp; & nbsp; & nbsp;容 : </ TD >
< TD align = " left " bgColor = " #ffffff " >
< asp:TextBox id = " body " runat = " server " Width = " 300 " TextMode = " multiLine " Height = " 200 " ></ asp:TextBox ></ TD >
</ TR >
< TR >
< TD align = " right " bgColor = " #f0f0f0 " > 格 & nbsp; & nbsp; & nbsp; & nbsp;式 : </ TD >
< TD align = " left " bgColor = " #ffffff " >
< asp:RadioButtonList id = " format " runat = " server " ></ asp:RadioButtonList ></ TD >
</ TR >
< TR >
< TD align = " center " bgColor = " #f0f0f0 " colSpan = " 2 " >
< asp:Button id = " send " runat = " server " Text = " 发送 " ></ asp:Button >& nbsp; & nbsp;
< asp:Button id = " reset " runat = " server " Text = " 重置 " ></ asp:Button ></ TD >
</ TR >
</ TABLE >
</ TD >
</ TR >
</ TABLE >
</ DIV >
</ FONT >
</ form >
</ body >
</ HTML >
<! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.0 Transitional//EN " >
< HTML >
< HEAD >
< title > SendEmail </ 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 " >
< form id = " Form1 " method = " post " runat = " server " >
< FONT face = " 宋体 " >
< DIV >
< TABLE id = " Table1 " style = " Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px " cellSpacing = " 0 "
cellPadding = " 0 " width = " 776 " align = " center " border = " 0 " >
< TR >
< TD >
< TABLE id = " Table2 " cellSpacing = " 1 " cellPadding = " 4 " width = " 600 " align = " center " bgColor = " #cccccc "
border = " 0 " >
< TR >
< TD align = " center " bgColor = " #f0f0f0 " colSpan = " 2 " > 电子邮件发送程 序 </ TD >
</ TR >
< TR >
< TD align = " right " width = " 150 " bgColor = " #f0f0f0 " > 发送人: </ TD >
< TD align = " left " bgColor = " #ffffff " >
< asp:TextBox id = " fromMail " runat = " server " Width = " 300 " ></ asp:TextBox ></ TD >
</ TR >
< TR >
< TD align = " right " bgColor = " #f0f0f0 " > 收件人: </ TD >
< TD align = " left " bgColor = " #ffffff " >
< asp:TextBox id = " toMail " runat = " server " Width = " 300 " ></ asp:TextBox ></ TD >
</ TR >
< TR >
< TD align = " right " bgColor = " #f0f0f0 " > 抄送人: </ TD >
< TD align = " left " bgColor = " #ffffff " >
< asp:TextBox id = " ccMail " runat = " server " Width = " 300 " ></ asp:TextBox ></ TD >
</ TR >
< TR >
< TD align = " right " bgColor = " #f0f0f0 " > 暗送人: </ TD >
< TD align = " left " bgColor = " #ffffff " >
< asp:TextBox id = " bccMail " runat = " server " Width = " 300 " ></ asp:TextBox ></ TD >
</ TR >
< TR >
< TD align = " right " bgColor = " #f0f0f0 " > 主 & nbsp; & nbsp; & nbsp; & nbsp;题 : </ TD >
< TD align = " left " bgColor = " #ffffff " >
< asp:TextBox id = " subject " runat = " server " Width = " 300 " ></ asp:TextBox ></ TD >
</ TR >
< TR >
< TD align = " right " bgColor = " #f0f0f0 " > 附 & nbsp; & nbsp; & nbsp; & nbsp;件 : </ TD >
< TD align = " left " bgColor = " #ffffff " >< INPUT id = " upfile " type = " file " name = " upfile " runat = " server " ></ TD >
</ TR >
< TR >
< TD align = " right " bgColor = " #f0f0f0 " > 内 & nbsp; & nbsp; & nbsp; & nbsp;容 : </ TD >
< TD align = " left " bgColor = " #ffffff " >
< asp:TextBox id = " body " runat = " server " Width = " 300 " TextMode = " multiLine " Height = " 200 " ></ asp:TextBox ></ TD >
</ TR >
< TR >
< TD align = " right " bgColor = " #f0f0f0 " > 格 & nbsp; & nbsp; & nbsp; & nbsp;式 : </ TD >
< TD align = " left " bgColor = " #ffffff " >
< asp:RadioButtonList id = " format " runat = " server " ></ asp:RadioButtonList ></ TD >
</ TR >
< TR >
< TD align = " center " bgColor = " #f0f0f0 " colSpan = " 2 " >
< asp:Button id = " send " runat = " server " Text = " 发送 " ></ asp:Button >& nbsp; & nbsp;
< asp:Button id = " reset " runat = " server " Text = " 重置 " ></ asp:Button ></ TD >
</ TR >
</ TABLE >
</ TD >
</ TR >
</ TABLE >
</ DIV >
</ FONT >
</ form >
</ body >
</ HTML >
using
System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Util;
using System.Web.Mail;
namespace WebApplication1
{
/// <summary>
/// SendEmail 的摘要说明。
/// </summary>
public class SendEmail : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button reset;
protected System.Web.UI.WebControls.Button send;
protected System.Web.UI.WebControls.RadioButtonList format;
protected System.Web.UI.WebControls.TextBox body;
protected System.Web.UI.WebControls.TextBox subject;
protected System.Web.UI.WebControls.TextBox bccMail;
protected System.Web.UI.WebControls.TextBox ccMail;
protected System.Web.UI.WebControls.TextBox toMail;
protected System.Web.UI.WebControls.TextBox fromMail;
protected System.Web.UI.HtmlControls.HtmlInputFile upfile;
private void Page_Load( object sender, System.EventArgs e)
{
if ( ! IsPostBack)
{
format.Items.Add( new ListItem( " 文本 " , " 0 " )); // 类型
format.Items.Add( new ListItem( " HTML " , " 1 " ));
format.Items[ 0 ].Selected = true ;
fromMail.Text = " lqscoke08@126.com " ; // 发送方邮件
fromMail.Enabled = false ;
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base .OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this .send.Click += new System.EventHandler( this .send_Click);
this .Load += new System.EventHandler( this .Page_Load);
}
#endregion
private void send_Click( object sender, System.EventArgs e)
{
bool flag = Sendmail(fromMail.Text,toMail.Text,ccMail.Text,bccMail.Text,subject.Text,body.Text,format.SelectedValue);
if (flag == true )
{
Response.Write( " <script>alert('发送成功!');</script> " );
}
else
{
Response.Write( " <script>alert('发送失败!');</script> " );
}
}
private bool Sendmail( string fromMail, string toMail, string ccMail, string bccMail, string subject, string body, string sendMode)
{
try
{
MailMessage myMail = new MailMessage();
myMail.From = fromMail; // 发件人邮箱
myMail.To = toMail; // 收件人Email
myMail.Cc = ccMail; // 邮件列表
myMail.Bcc = bccMail;
myMail.Subject = subject; // 主题
myMail.Body = body; // 正文
myMail.BodyFormat = sendMode == " 0 " ? MailFormat.Text : MailFormat.Html; // 邮件类型
// 附件
string ServerFileName = "" ;
if ( this .upfile.PostedFile.ContentLength != 0 )
{
string upFileName = this .upfile.PostedFile.FileName;
string [] strTemp = upFileName.Split( ' . ' );
string upFileExp = strTemp[strTemp.Length - 1 ].ToString();
ServerFileName = Server.MapPath(DateTime.Now.ToString( " yyyyMMddhhmmss " ) + " . " + upFileExp);
this .upfile.PostedFile.SaveAs(ServerFileName);
myMail.Attachments.Add( new MailAttachment(ServerFileName));
}
// 通过SMTP服务器验证
myMail.Fields.Add( " http://schemas.microsoft.com/cdo/configuration/smtpauthenticate " , 1 );
myMail.Fields.Add( " http://schemas.microsoft.com/cdo/configuration/sendusername " , " lqscoke08 " ); // 发送方邮件帐户
myMail.Fields.Add( " http://schemas.microsoft.com/cdo/configuration/sendpassword " , " 3626053705881 " ); // 发送方邮件密码
SmtpMail.SmtpServer = " smtp. " + fromMail.Substring(fromMail.IndexOf( " @ " ) + 1 );
SmtpMail.Send(myMail);
return true ;
}
catch
{
return false ;
}
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Util;
using System.Web.Mail;
namespace WebApplication1
{
/// <summary>
/// SendEmail 的摘要说明。
/// </summary>
public class SendEmail : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button reset;
protected System.Web.UI.WebControls.Button send;
protected System.Web.UI.WebControls.RadioButtonList format;
protected System.Web.UI.WebControls.TextBox body;
protected System.Web.UI.WebControls.TextBox subject;
protected System.Web.UI.WebControls.TextBox bccMail;
protected System.Web.UI.WebControls.TextBox ccMail;
protected System.Web.UI.WebControls.TextBox toMail;
protected System.Web.UI.WebControls.TextBox fromMail;
protected System.Web.UI.HtmlControls.HtmlInputFile upfile;
private void Page_Load( object sender, System.EventArgs e)
{
if ( ! IsPostBack)
{
format.Items.Add( new ListItem( " 文本 " , " 0 " )); // 类型
format.Items.Add( new ListItem( " HTML " , " 1 " ));
format.Items[ 0 ].Selected = true ;
fromMail.Text = " lqscoke08@126.com " ; // 发送方邮件
fromMail.Enabled = false ;
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base .OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this .send.Click += new System.EventHandler( this .send_Click);
this .Load += new System.EventHandler( this .Page_Load);
}
#endregion
private void send_Click( object sender, System.EventArgs e)
{
bool flag = Sendmail(fromMail.Text,toMail.Text,ccMail.Text,bccMail.Text,subject.Text,body.Text,format.SelectedValue);
if (flag == true )
{
Response.Write( " <script>alert('发送成功!');</script> " );
}
else
{
Response.Write( " <script>alert('发送失败!');</script> " );
}
}
private bool Sendmail( string fromMail, string toMail, string ccMail, string bccMail, string subject, string body, string sendMode)
{
try
{
MailMessage myMail = new MailMessage();
myMail.From = fromMail; // 发件人邮箱
myMail.To = toMail; // 收件人Email
myMail.Cc = ccMail; // 邮件列表
myMail.Bcc = bccMail;
myMail.Subject = subject; // 主题
myMail.Body = body; // 正文
myMail.BodyFormat = sendMode == " 0 " ? MailFormat.Text : MailFormat.Html; // 邮件类型
// 附件
string ServerFileName = "" ;
if ( this .upfile.PostedFile.ContentLength != 0 )
{
string upFileName = this .upfile.PostedFile.FileName;
string [] strTemp = upFileName.Split( ' . ' );
string upFileExp = strTemp[strTemp.Length - 1 ].ToString();
ServerFileName = Server.MapPath(DateTime.Now.ToString( " yyyyMMddhhmmss " ) + " . " + upFileExp);
this .upfile.PostedFile.SaveAs(ServerFileName);
myMail.Attachments.Add( new MailAttachment(ServerFileName));
}
// 通过SMTP服务器验证
myMail.Fields.Add( " http://schemas.microsoft.com/cdo/configuration/smtpauthenticate " , 1 );
myMail.Fields.Add( " http://schemas.microsoft.com/cdo/configuration/sendusername " , " lqscoke08 " ); // 发送方邮件帐户
myMail.Fields.Add( " http://schemas.microsoft.com/cdo/configuration/sendpassword " , " 3626053705881 " ); // 发送方邮件密码
SmtpMail.SmtpServer = " smtp. " + fromMail.Substring(fromMail.IndexOf( " @ " ) + 1 );
SmtpMail.Send(myMail);
return true ;
}
catch
{
return false ;
}
}
}
}