<%
@ Page Language
=
"
C#
"
%>
<% @ Import Namespace = " System.Web.Mail " %>
< Script Language = " C# " Runat = " Server " >
public void Mail_Send(Object src,EventArgs e)
{
// 创建MailMessage对象
MailMessage MyMsg = new MailMessage();
MyMsg.From = tbFrom.Text;
MyMsg.To = tbTo.Text;
MyMsg.Subject = tbSubject.Text;
MyMsg.Priority = (MailPriority)ddlPriority.SelectedIndex;
MyMsg.BodyFormat = (MailFormat)ddlBodyFormat.SelectedIndex;
MyMsg.Body = tbBody.Text;
// 如果有附件则上传
HttpPostedFile hpfFile = AttachFile.PostedFile;
if (hpfFile.FileName != "" )
{
// 有附件,则上传到Temp目录中
// 取得文件名(不含路径)
char [] de = { ' / ' };
string [] AFilename = hpfFile.FileName.Split(de);
string strFilename = AFilename[AFilename.Length - 1 ];
string strPath = Server.MapPath( " . " ) + " /Temp/ " + strFilename;
hpfFile.SaveAs(strPath);
// 添加附件
MyMsg.Attachments.Add( new MailAttachment(strPath));
}
try
{
// 发送
SmtpMail.Send(MyMsg);
lblShowMsg.Text = " 发送成功 " ;
tbTo.Text = "" ;
tbSubject.Text = "" ;
tbBody.Text = "" ;
ddlPriority.SelectedIndex = 1 ;
ddlBodyFormat.SelectedIndex = 0 ;
}
catch (Exception ee)
{
lblShowMsg.Text = " 发送失败: " + ee.ToString();
}
}
</ script >
< html >
< head >
< title ></ title >
</ head >
< body >
< form Enctype = " multipart/form-data " runat = " server " >
< b > 演示发送邮件附件 </ b >
< table border = 1 >
< tr >< td colspan = " 2 " >< b > 功能强大的邮件发送 </ b ></ td ></ tr >
< tr >< td colspan = " 2 " >< asp:Label id = " lblShowMsg " ForeColor = " red " runat = " server " /></ td ></ tr >
< tr >< td > 收件人: </ td >< td >< asp:TextBox id = " tbTo " runat = " server " /></ td ></ tr >
< tr >< td > 发件人: </ td >< td >< asp:TextBox id = " tbFrom " runat = " server " /></ td ></ tr >
< tr >< td > 邮件主题: </ td >< td >< asp:TextBox id = " tbSubject " runat = " server " /></ td ></ tr >
< tr >
< td > 优先级: < asp:DropDownList id = " ddlPriority " runat = " server " >
< asp:ListItem Value = " High " > 高 </ asp:ListItem >
< asp:ListItem Value = " Normal " Selected > 普通 </ asp:ListItem >
< asp:ListItem Value = " Low " > 低 </ asp:ListItem >
</ asp:DropDownList >
</ td >
< td > 邮件格式: < asp:DropDownList id = " ddlBodyFormat " runat = " server " >
< asp:ListItem Value = " Text " > 文本格式 </ asp:ListItem >
< asp:ListItem Value = " Html " > HTML格式 </ asp:ListItem >
</ asp:DropDownList >
</ td >
</ tr >
< tr >< td colspan = " 2 " > 邮件内容: </ td ></ tr >
< tr >< td colspan = " 2 " >< asp:TextBox TextMode = " MultiLine " Rows = " 5 " Columns = " 50 " id = " tbBody " runat = " server " /></ td ></ tr >
< tr >< td > 邮件附件: </ td >< td >< input type = " file " id = " AttachFile " runat = " server " /></ td ></ tr >
< tr >< td >< asp:Button id = " btnSend " Text = " 发送 " OnClick = " Mail_Send " runat = " server " /></ td ></ tr >
</ table >
</ form >
</ body >
</ html >
<% @ Import Namespace = " System.Web.Mail " %>
< Script Language = " C# " Runat = " Server " >
public void Mail_Send(Object src,EventArgs e)
{
// 创建MailMessage对象
MailMessage MyMsg = new MailMessage();
MyMsg.From = tbFrom.Text;
MyMsg.To = tbTo.Text;
MyMsg.Subject = tbSubject.Text;
MyMsg.Priority = (MailPriority)ddlPriority.SelectedIndex;
MyMsg.BodyFormat = (MailFormat)ddlBodyFormat.SelectedIndex;
MyMsg.Body = tbBody.Text;
// 如果有附件则上传
HttpPostedFile hpfFile = AttachFile.PostedFile;
if (hpfFile.FileName != "" )
{
// 有附件,则上传到Temp目录中
// 取得文件名(不含路径)
char [] de = { ' / ' };
string [] AFilename = hpfFile.FileName.Split(de);
string strFilename = AFilename[AFilename.Length - 1 ];
string strPath = Server.MapPath( " . " ) + " /Temp/ " + strFilename;
hpfFile.SaveAs(strPath);
// 添加附件
MyMsg.Attachments.Add( new MailAttachment(strPath));
}
try
{
// 发送
SmtpMail.Send(MyMsg);
lblShowMsg.Text = " 发送成功 " ;
tbTo.Text = "" ;
tbSubject.Text = "" ;
tbBody.Text = "" ;
ddlPriority.SelectedIndex = 1 ;
ddlBodyFormat.SelectedIndex = 0 ;
}
catch (Exception ee)
{
lblShowMsg.Text = " 发送失败: " + ee.ToString();
}
}
</ script >
< html >
< head >
< title ></ title >
</ head >
< body >
< form Enctype = " multipart/form-data " runat = " server " >
< b > 演示发送邮件附件 </ b >
< table border = 1 >
< tr >< td colspan = " 2 " >< b > 功能强大的邮件发送 </ b ></ td ></ tr >
< tr >< td colspan = " 2 " >< asp:Label id = " lblShowMsg " ForeColor = " red " runat = " server " /></ td ></ tr >
< tr >< td > 收件人: </ td >< td >< asp:TextBox id = " tbTo " runat = " server " /></ td ></ tr >
< tr >< td > 发件人: </ td >< td >< asp:TextBox id = " tbFrom " runat = " server " /></ td ></ tr >
< tr >< td > 邮件主题: </ td >< td >< asp:TextBox id = " tbSubject " runat = " server " /></ td ></ tr >
< tr >
< td > 优先级: < asp:DropDownList id = " ddlPriority " runat = " server " >
< asp:ListItem Value = " High " > 高 </ asp:ListItem >
< asp:ListItem Value = " Normal " Selected > 普通 </ asp:ListItem >
< asp:ListItem Value = " Low " > 低 </ asp:ListItem >
</ asp:DropDownList >
</ td >
< td > 邮件格式: < asp:DropDownList id = " ddlBodyFormat " runat = " server " >
< asp:ListItem Value = " Text " > 文本格式 </ asp:ListItem >
< asp:ListItem Value = " Html " > HTML格式 </ asp:ListItem >
</ asp:DropDownList >
</ td >
</ tr >
< tr >< td colspan = " 2 " > 邮件内容: </ td ></ tr >
< tr >< td colspan = " 2 " >< asp:TextBox TextMode = " MultiLine " Rows = " 5 " Columns = " 50 " id = " tbBody " runat = " server " /></ td ></ tr >
< tr >< td > 邮件附件: </ td >< td >< input type = " file " id = " AttachFile " runat = " server " /></ td ></ tr >
< tr >< td >< asp:Button id = " btnSend " Text = " 发送 " OnClick = " Mail_Send " runat = " server " /></ td ></ tr >
</ table >
</ form >
</ body >
</ html >