📄 mysendmail.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;
using System.Text;
namespace Example_11_4
{
/// <summary>
/// Summary description for MySendMail.
/// </summary>
public class MySendMail : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox ToEmail;
protected System.Web.UI.WebControls.RequiredFieldValidator rt;
protected System.Web.UI.WebControls.RegularExpressionValidator ret;
protected System.Web.UI.WebControls.TextBox Title;
protected System.Web.UI.WebControls.RequiredFieldValidator rtt;
protected System.Web.UI.WebControls.TextBox FromEmail;
protected System.Web.UI.WebControls.RequiredFieldValidator rg;
protected System.Web.UI.WebControls.RegularExpressionValidator reg;
protected System.Web.UI.WebControls.TextBox Body;
protected System.Web.UI.WebControls.Button SendMailBtn;
protected System.Web.UI.WebControls.Button CancelBtn;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
private void SendMailBtn_Click(object sender, System.EventArgs e)
{
try
{
SendMail(FromEmail.Text,ToEmail.Text,Title.Text,Body.Text,new ArrayList());
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
private void SendMail(String FromEmail,String ToEmail,String Title,
String Body,ArrayList aFileName)
{
MailMessage myMail = new MailMessage();
StringBuilder MailBody = new StringBuilder();
myMail.From = "<" + FromEmail + ">" + FromEmail;
myMail.To = ToEmail;
myMail.Subject = Title;
myMail.BodyFormat = MailFormat.Html;
MailBody.Append("<style>A:visited { TEXT-DECORATION: none }");
MailBody.Append("A:active { TEXT-DECORATION: none }");
MailBody.Append("A:hover { TEXT-DECORATION: underline }");
MailBody.Append("A:link { text-decoration: none;}");
MailBody.Append("A:visited { text-decoration: none;}");
MailBody.Append("BODY { FONT-FAMILY: Verdana,宋体; FONT-SIZE: 9pt;}");
MailBody.Append("TD { FONT-FAMILY: Verdana,宋体; FONT-SIZE: 9pt }</style>");
MailBody.Append("<TABLE border=0 width='95%' align=center><TBODY><TR>");
MailBody.Append("<TD valign=middle align=top>");
MailBody.Append("<p>" + Body + "<p>");
MailBody.Append("</TD></TR>");
MailBody.Append("</TBODY></TABLE><br>");
myMail.Body = MailBody.ToString();
for(int i = 0; i < aFileName.Count; i++)
{
MailAttachment mailA = new MailAttachment(aFileName[i].ToString());
myMail.Attachments.Add(mailA);
}
SmtpMail.Send(myMail);
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SendMailBtn.Click += new System.EventHandler(this.SendMailBtn_Click);
this.CancelBtn.Click += new System.EventHandler(this.CancelBtn_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void CancelBtn_Click(object sender, System.EventArgs e)
{
///清空邮件的信息
FromEmail.Text = "";
ToEmail.Text = "";
Title.Text = "";
Body.Text = "";
///显示取消信息
Response.Write("你取消发送该邮件!!!");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -