<%@ page Language = "C#" debug = "true" Explicit="True" %>
<%@ Import Namespace = "System.Web.Util" %>
<%@ Import Namespace = "System.Web.Mail" %>
public void SendMail (Object Obj, EventArgs E) {
labelSendMailResult.Text = "";
if (Page.IsValid) {
MailMessage mailObj = new MailMessage();
// 設(shè)置email的'from'和'to'的地址
mailObj.From = inputMailFrom.Value;
mailObj.To = inputMailTo.Value;
mailObj.Subject = inputMailSubject.Value;
mailObj.Body = textBoxMailBody.Text;
// 可選: 使用html格式的Email
mailObj.BodyFormat = MailFormat.Html;
// 可選: 對(duì)郵件進(jìn)行加密
// mailObj.BodyEncoding = MailFormat.Base64;
// 可選: 設(shè)置郵件的優(yōu)先級(jí)別為高
mailObj.Priority = MailPriority.High;
// 可選: 附件
if (inputMailAttachment.PostedFile.ContentLength > 0) {
// 注意這里我們創(chuàng)建了一個(gè)MailAttachment對(duì)象來附加一個(gè)文件到email。
mailObj.Attachments.Add(new MailAttachment(inputMailAttachment.PostedFile.FileName));
}
// 使用SmtpMail對(duì)象來發(fā)送郵件。
SmtpMail.Send(mailObj);
labelSendMailResult.Text = "郵件發(fā)送成功 From: " + inputMailFrom.Value + "; To: " + inputMailTo.Value;
if (inputMailAttachment.PostedFile.ContentLength > 0) {
labelSendMailResult.Text += "
該郵件包含附件: " + inputMailAttachment.PostedFile.FileName + ", 附件大小為: " + (inputMailAttachment.PostedFile.ContentLength / 1024).ToString() + " K Byte(s)";
}
}
}
發(fā)送郵件 ASP.NET