ASP.NET中常用功能代码总结(7)——利用Jmail发送和接收邮件
ASP.NET中常用功能代码总结(7)——利用Jmail发送和接收邮件
整理:Terrylee
一.利用Jmail发送邮件
1/// <summary>
2/// 利用Jmail发送邮件
3/// </summary>
4private void SendMailByJmail()
5{
6 ///建立发邮件类
7 jmail.MessageClass oJmailMessage = new jmail.MessageClass();
8
9 /// 字符集
10 oJmailMessage.Charset = "GB2312";
11
12 ///附件的编码格式
13 oJmailMessage.Encoding = "BASE64";
14 oJmailMessage.ContentType = "text/html";
15
16 ///是否将信头编码成iso-8859-1字符集
17 oJmailMessage.ISOEncodeHeaders = false;
18
19 /// 优先级
20 oJmailMessage.Priority = Convert.ToByte(1);
21
22 ///发送人邮件地址
23 oJmailMessage.From = TxtEmail.Text.Trim();
24
25 ///发送人姓名
26 oJmailMessage.FromName = TxtName.Text.Trim();
27
28 /// 邮件主题
29 oJmailMessage.Subject = txtSubject.Text.Trim();
30
31 ///身份验证的用户名
32 oJmailMessage.MailServerUserName = TxtSmtpUser.Text.Trim();
33
34 ///用户密码
35 oJmailMessage.MailServerPassWord = TxtSmtpPwd.Text.Trim();
36
37 ///添加一个收件人,抄送人和密送人的添加和该方法是一样的,只是分别使用AddRecipientCC和RecipientBCC两个属性
38 ///要是需要添加多个收件人,则重复下面的语句即可。添加多个抄送和密送人的方法一样
39 oJmailMessage.AddRecipient(txtReciver.Text.Trim(),"","");
40 if("" != upFile.PostedFile.FileName)
41 {
42 string attpath = upFile.PostedFile.FileName;
43 oJmailMessage.AddAttachment(@attpath,true,attpath.Substring(attpath.LastIndexOf(".")+1,3));//添加附件
44 }
45
46 ///邮件内容
47 oJmailMessage.Body = txtContent.Text.Trim();
48
49 if(oJmailMessage.Send(TxtSmtServer.Text.Trim(),false))
50 {
51 Response.Write("发送成功!");
52 }
53 else
54 {
55 Response.Write("发送失败,请检查邮件服务器的设置!");
56 }
57
58 oJmailMessage = null;
59}
60
2/// 利用Jmail发送邮件
3/// </summary>
4private void SendMailByJmail()
5{
6 ///建立发邮件类
7 jmail.MessageClass oJmailMessage = new jmail.MessageClass();
8
9 /// 字符集
10 oJmailMessage.Charset = "GB2312";
11
12 ///附件的编码格式
13 oJmailMessage.Encoding = "BASE64";
14 oJmailMessage.ContentType = "text/html";
15
16 ///是否将信头编码成iso-8859-1字符集
17 oJmailMessage.ISOEncodeHeaders = false;
18
19 /// 优先级
20 oJmailMessage.Priority = Convert.ToByte(1);
21
22 ///发送人邮件地址
23 oJmailMessage.From = TxtEmail.Text.Trim();
24
25 ///发送人姓名
26 oJmailMessage.FromName = TxtName.Text.Trim();
27
28 /// 邮件主题
29 oJmailMessage.Subject = txtSubject.Text.Trim();
30
31 ///身份验证的用户名
32 oJmailMessage.MailServerUserName = TxtSmtpUser.Text.Trim();
33
34 ///用户密码
35 oJmailMessage.MailServerPassWord = TxtSmtpPwd.Text.Trim();
36
37 ///添加一个收件人,抄送人和密送人的添加和该方法是一样的,只是分别使用AddRecipientCC和RecipientBCC两个属性
38 ///要是需要添加多个收件人,则重复下面的语句即可。添加多个抄送和密送人的方法一样
39 oJmailMessage.AddRecipient(txtReciver.Text.Trim(),"","");
40 if("" != upFile.PostedFile.FileName)
41 {
42 string attpath = upFile.PostedFile.FileName;
43 oJmailMessage.AddAttachment(@attpath,true,attpath.Substring(attpath.LastIndexOf(".")+1,3));//添加附件
44 }
45
46 ///邮件内容
47 oJmailMessage.Body = txtContent.Text.Trim();
48
49 if(oJmailMessage.Send(TxtSmtServer.Text.Trim(),false))
50 {
51 Response.Write("发送成功!");
52 }
53 else
54 {
55 Response.Write("发送失败,请检查邮件服务器的设置!");
56 }
57
58 oJmailMessage = null;
59}
60
二.利用Jmail接收邮件
1/// <summary>
2/// 利用Jmail接收邮件
3/// </summary>
4private void ReciveByJmail()
5{
6 ///建立收邮件对象
7 jmail.POP3Class popMail = new POP3Class();
8
9 ///建立邮件信息接口
10 jmail.Message mailMessage;
11
12 ///建立附件集接口
13 jmail.Attachments atts;
14
15 ///建立附件接口
16 jmail.Attachment att;
17
18 try
19 {
20 popMail.Connect(TxtPopUser.Text.Trim(),TxtPopPwd.Text.Trim(),TxtPopServer.Text.Trim(),Convert.ToInt32(TxtPopPort.Text.Trim()));
21
22 ///如果收到邮件
23 if(0 < popMail.Count)
24 {
25 ///根据取到的邮件数量依次取得每封邮件
26 for(int i=1;i <= popMail.Count;i++)
27 {
28 ///取得一条邮件信息
29 mailMessage = popMail.Messages[i];
30
31 ///取得该邮件的附件集合
32 atts = mailMessage.Attachments;
33
34 ///设置邮件的编码方式
35 mailMessage.Charset = "GB2312";
36
37 ///设置邮件的附件编码方式
38 mailMessage.Encoding = "Base64";
39
40 ///是否将信头编码成iso-8859-1字符集
41 mailMessage.ISOEncodeHeaders = false;
42
43 ///邮件的优先级
44 txtpriority.Text = mailMessage.Priority.ToString();
45
46 ///邮件的发送人的信箱地址
47 txtSendMail.Text = mailMessage.From;
48
49 ///邮件的发送人
50 txtSender.Text = mailMessage.FromName;
51
52 ///邮件主题
53 txtSubject.Text = mailMessage.Subject;
54
55 ///邮件内容
56 txtBody.Text = mailMessage.Body;
57
58 ///邮件大小
59 txtSize.Text = mailMessage.Size.ToString();
60
61 for(int j=0;j<atts.Count;j++)
62 {
63 ///取得附件
64 att = atts[j];
65
66 ///附件名称
67 string attname = att.Name;
68
69 ///上传到服务器
70 att.SaveToFile("e:\\attFile\\"+attname);
71
72 }
73
74 }
75 panMailInfo.Visible = true;
76 att = null;
77 atts = null;
78 }
79 else
80 {
81 Response.Write("没有新邮件!");
82 }
83
84 popMail.DeleteMessages();
85 popMail.Disconnect();
86 popMail = null;
87 }
88 catch
89 {
90 Response.Write("Warning!请检查邮件服务器的设置是否正确!");
91 }
92}
2/// 利用Jmail接收邮件
3/// </summary>
4private void ReciveByJmail()
5{
6 ///建立收邮件对象
7 jmail.POP3Class popMail = new POP3Class();
8
9 ///建立邮件信息接口
10 jmail.Message mailMessage;
11
12 ///建立附件集接口
13 jmail.Attachments atts;
14
15 ///建立附件接口
16 jmail.Attachment att;
17
18 try
19 {
20 popMail.Connect(TxtPopUser.Text.Trim(),TxtPopPwd.Text.Trim(),TxtPopServer.Text.Trim(),Convert.ToInt32(TxtPopPort.Text.Trim()));
21
22 ///如果收到邮件
23 if(0 < popMail.Count)
24 {
25 ///根据取到的邮件数量依次取得每封邮件
26 for(int i=1;i <= popMail.Count;i++)
27 {
28 ///取得一条邮件信息
29 mailMessage = popMail.Messages[i];
30
31 ///取得该邮件的附件集合
32 atts = mailMessage.Attachments;
33
34 ///设置邮件的编码方式
35 mailMessage.Charset = "GB2312";
36
37 ///设置邮件的附件编码方式
38 mailMessage.Encoding = "Base64";
39
40 ///是否将信头编码成iso-8859-1字符集
41 mailMessage.ISOEncodeHeaders = false;
42
43 ///邮件的优先级
44 txtpriority.Text = mailMessage.Priority.ToString();
45
46 ///邮件的发送人的信箱地址
47 txtSendMail.Text = mailMessage.From;
48
49 ///邮件的发送人
50 txtSender.Text = mailMessage.FromName;
51
52 ///邮件主题
53 txtSubject.Text = mailMessage.Subject;
54
55 ///邮件内容
56 txtBody.Text = mailMessage.Body;
57
58 ///邮件大小
59 txtSize.Text = mailMessage.Size.ToString();
60
61 for(int j=0;j<atts.Count;j++)
62 {
63 ///取得附件
64 att = atts[j];
65
66 ///附件名称
67 string attname = att.Name;
68
69 ///上传到服务器
70 att.SaveToFile("e:\\attFile\\"+attname);
71
72 }
73
74 }
75 panMailInfo.Visible = true;
76 att = null;
77 atts = null;
78 }
79 else
80 {
81 Response.Write("没有新邮件!");
82 }
83
84 popMail.DeleteMessages();
85 popMail.Disconnect();
86 popMail = null;
87 }
88 catch
89 {
90 Response.Write("Warning!请检查邮件服务器的设置是否正确!");
91 }
92}
支持TerryLee的创业产品Worktile
Worktile,新一代简单好用、体验极致的团队协同、项目管理工具,让你和你的团队随时随地一起工作。完全免费,现在就去了解一下吧。
https://worktile.com
Worktile,新一代简单好用、体验极致的团队协同、项目管理工具,让你和你的团队随时随地一起工作。完全免费,现在就去了解一下吧。
https://worktile.com