温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:基于Ajax的邮件系统源码
当前文件路径:AjaxMail/Mail/SenderMail.aspx.cs

1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Collections; 5
using System.Web; 6
using System.Web.Security; 7
using System.Web.UI; 8
using System.Web.UI.WebControls; 9
using System.Web.UI.WebControls.WebParts; 10
using System.Web.UI.HtmlControls; 11
using System.Data.SqlClient; 12
using Web2ASPNET2.ASPNET2AjaxMail; 13
using Web2ASPNET2.CommonOperation; 14
using Web2ASPNET2.UserCommonOperation; 15
using System.Net.Mail; 16
using System.IO; 17
18
public partial class Mail_SenderMail : System.Web.UI.Page 19
{ 20
protected void Page_Load(object sender, EventArgs e) 21
{ ///判断用户是否登录 22
UserInfo info = (UserInfo)UserCommonOperation.GetUserInfo(Session); 23
if(info == null) 24
{ ///返回到上一个页面 25
Response.Write("<script>history.back()</script>"); 26
Response.Redirect("~/Portal/UserLogin.aspx"); ///跳转到登录页面 27
return; 28
} 29
} 30
protected void btnSender_Click(object sender,EventArgs e) 31
{ 32
int size = 100; ///定义保存邮件大小变量size 33
///构建新邮件 34
MailMessage mail = new MailMessage(); 35
///添加发件人地址 36
mail.From = new MailAddress("ASPNET2AjaxMail@mail.com"); 37
size += mail.From.Address.Length; 38
///添加收件人地址 39
string split = ";"; 40
string[] toList = tbTo.Text.Trim().Split(split.ToCharArray()); 41
foreach(string to in toList) 42
{ 43
mail.To.Add(to.Trim()); 44
} 45
size += tbTo.Text.Length; 46
///添加抄送地址; 47
string[] ccList = tbCC.Text.Trim().Split(split.ToCharArray()); 48
foreach(string cc in ccList) 49
{ 50
if(string.IsNullOrEmpty(cc.Trim()) == false) 51
{ 52
mail.CC.Add(cc.Trim()); 53
} 54
} 55
size += tbCC.Text.Length; 56
///添加主题 57
mail.Subject = tbName.Text.Trim(); 58
mail.SubjectEncoding = System.Text.Encoding.UTF8; 59
size += tbName.Text.Length; 60
///添加内容 61
mail.Body = tbBody.Text; 62
mail.BodyEncoding = System.Text.Encoding.UTF8; 63
mail.IsBodyHtml = cbHtmlFormt.Checked; 64
size += tbBody.Text.Length; 65
///添加附件 66
HttpFileCollection fileList = HttpContext.Current.Request.Files; 67
for(int i = 0; i < fileList.Count; i++) 68
{ ///添加单个附件 69
HttpPostedFile file = fileList[i]; 70
if(file.FileName.Length <= 0 || file.ContentLength <= 0) continue; 71
Attachment attachment = new Attachment(file.FileName); 72
mail.Attachments.Add(attachment); 73
size += file.ContentLength; 74
} 75
76
Mail ajaxMail = new Mail(); 77
if(ajaxMail.SenderMail(mail) > 0) 78
{ 79
int mailID = ajaxMail.SaveAsMail(mail.Subject,mail.Body,mail.From.Address, 80
tbTo.Text,tbCC.Text,cbHtmlFormt.Checked,size,mail.Attachments.Count > 0 ? true : false); 81
if(mailID > 0) return; 82
///保存发送邮件的附件 83
for(int i = 0; i < fileList.Count; i++) 84
{ ///添加单个附件 85
HttpPostedFile file = fileList[i]; 86
if(file.FileName.Length <= 0 || file.ContentLength <= 0) continue; 87
///保存附件到硬盘中 88
string fileName = Path.GetFileName(file.FileName); 89
file.SaveAs(MapPath("Attachments/" + fileName)); 90
///保存发送邮件的附件 91
ajaxMail.SaveAsMailAttachment(fileName,"Attachments/" + fileName, 92
file.ContentType,file.ContentLength,mailID); 93
} 94
} 95
Response.Redirect("~/Mail/ViewMail.aspx"); 96
} 97
protected void btnReturn_Click(object sender,EventArgs e) 98
{ 99
Response.Redirect("~/Mail/ViewMail.aspx"); 100
} 101
} 102





}
}