<%@ 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();
// 设置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;
// 可选: 对邮件进行加密
// mailObj.BodyEncoding = MailFormat.Base64;
// 可选: 设置邮件的优先级别为高
mailObj.Priority = MailPriority.High;
// 可选: 附件
if (inputMailAttachment.PostedFile.ContentLength > 0) {
// 注意这里我们创建了一个MailAttachment对象来附加一个文件到email。
mailObj.Attachments.Add(new MailAttachment(inputMailAttachment.PostedFile.FileName));
}
// 使用SmtpMail对象来发送邮件。
SmtpMail.Send(mailObj);
labelSendMailResult.Text = "邮件发送成功 From: " + inputMailFrom.Value + "; To: " + inputMailTo.Value;
if (inputMailAttachment.PostedFile.ContentLength > 0) {
labelSendMailResult.Text += "
该邮件包含附件: " + inputMailAttachment.PostedFile.FileName + ", 附件大小为: " + (inputMailAttachment.PostedFile.ContentLength / 1024).ToString() + " K Byte(s)";
}
}
}
发送邮件 ASP.NET