一.利用Jmail发送邮件
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接收邮件
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}
1.安装jmail4.3
2.找到jmail.dll 引用到项目中。
using jmail;
....................
jmail.Message Jmail = new jmail.Message();
DateTime t = DateTime.Now;
String Subject = "haohao 学习 天天向上";
String body = "你好 《》<br><b>科学</b>12:15";
String FromEmail = "escdns@gmail.com";
String ToEmail = "woolern@sohu.com";
//Silent属性:如果设置为true,JMail不会抛出例外错误. JMail. Send( () 会根据操作结果返回true或false
Jmail.Silent = true;
//Jmail创建的日志,前提loging属性设置为true
Jmail.Logging = true;
//字符集,缺省为"US-ASCII"
Jmail.Charset = "GB2312";
//信件的contentype. 缺省是"text/plain") : 字符串如果你以HTML格式发送邮件, 改为"text/html"即可。
//Jmail.ContentType = "Multipart/Mixed";
//添加收件人(若几个收件人就添加几行下面的代码)
Jmail.AddRecipient(ToEmail, "", "");
//Jmail.AddRecipientCC,Jmail.AddRecipientBCC (抄送,密送,用法同Jmail.AddRecipient)
Jmail.From = FromEmail;
//发件人邮件用户名
Jmail.MailServerUserName = "woolern@sohu.com";
//发件人邮件密码
Jmail.MailServerPassWord = "********";
//设置邮件标题
Jmail.Subject = Subject;
//邮件添加附件,(多附件的话,可以再加一条Jmail.AddAttachment( "c:\test.jpg",true,null);)就可以搞定了。
//[注]:加了附件,讲把上面的Jmail.ContentType="text/html";删掉。否则会在邮件里出现乱码。
Jmail.AddAttachment("c:\img200610311000250.jpg", true, null);
//邮件内容,(若为纯文本就改为Jmail.Body )
Jmail.HTMLBody = body;
//Jmail发送的方法
Jmail.Send("smtp.sina.net", false);
Jmail.Close();