ASP.NET中常用功能代码总结(7)——利用Jmail发送和接收邮件

ASP.NET中常用功能代码总结(7——利用Jmail发送和接收邮件<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

整理:Terrylee

一.利用Jmail发送邮件

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
 2InBlock.gif/// 利用Jmail发送邮件
 3ExpandedBlockEnd.gif/// </summary>

 4 None.gif private   void  SendMailByJmail()
 5 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    /**////建立发邮件类
 7InBlock.gif    jmail.MessageClass oJmailMessage = new jmail.MessageClass();
 8InBlock.gif    
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// 字符集
10InBlock.gif    oJmailMessage.Charset = "GB2312";
11InBlock.gif
12ExpandedSubBlockStart.gifContractedSubBlock.gif    /**////附件的编码格式
13InBlock.gif    oJmailMessage.Encoding = "BASE64";
14InBlock.gif    oJmailMessage.ContentType = "text/html";
15InBlock.gif
16ExpandedSubBlockStart.gifContractedSubBlock.gif    /**////是否将信头编码成iso-8859-1字符集
17InBlock.gif    oJmailMessage.ISOEncodeHeaders = false;
18InBlock.gif
19ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// 优先级
20InBlock.gif    oJmailMessage.Priority = Convert.ToByte(1);
21InBlock.gif    
22ExpandedSubBlockStart.gifContractedSubBlock.gif    /**////发送人邮件地址
23InBlock.gif    oJmailMessage.From = TxtEmail.Text.Trim();
24InBlock.gif    
25ExpandedSubBlockStart.gifContractedSubBlock.gif    /**////发送人姓名
26InBlock.gif    oJmailMessage.FromName    = TxtName.Text.Trim();
27InBlock.gif
28ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// 邮件主题
29InBlock.gif    oJmailMessage.Subject    = txtSubject.Text.Trim();
30InBlock.gif
31ExpandedSubBlockStart.gifContractedSubBlock.gif    /**////身份验证的用户名
32InBlock.gif    oJmailMessage.MailServerUserName = TxtSmtpUser.Text.Trim();
33InBlock.gif
34ExpandedSubBlockStart.gifContractedSubBlock.gif    /**////用户密码
35InBlock.gif    oJmailMessage.MailServerPassWord = TxtSmtpPwd.Text.Trim();
36InBlock.gif
37ExpandedSubBlockStart.gifContractedSubBlock.gif    /**////添加一个收件人,抄送人和密送人的添加和该方法是一样的,只是分别使用AddRecipientCC和RecipientBCC两个属性
38ExpandedSubBlockEnd.gif    ///要是需要添加多个收件人,则重复下面的语句即可。添加多个抄送和密送人的方法一样

39InBlock.gif    oJmailMessage.AddRecipient(txtReciver.Text.Trim(),"","");
40InBlock.gif    if("" != upFile.PostedFile.FileName)
41ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
42InBlock.gif        string attpath = upFile.PostedFile.FileName;
43InBlock.gif        oJmailMessage.AddAttachment(@attpath,true,attpath.Substring(attpath.LastIndexOf(".")+1,3));//添加附件
44ExpandedSubBlockEnd.gif    }

45InBlock.gif
46ExpandedSubBlockStart.gifContractedSubBlock.gif    /**////邮件内容
47InBlock.gif    oJmailMessage.Body = txtContent.Text.Trim();
48InBlock.gif
49InBlock.gif    if(oJmailMessage.Send(TxtSmtServer.Text.Trim(),false))
50ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
51InBlock.gif        Response.Write("发送成功!");
52ExpandedSubBlockEnd.gif    }

53InBlock.gif    else
54ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
55InBlock.gif        Response.Write("发送失败,请检查邮件服务器的设置!");
56ExpandedSubBlockEnd.gif    }

57InBlock.gif
58InBlock.gif    oJmailMessage = null;
59ExpandedBlockEnd.gif}

60 None.gif

二.利用Jmail接收邮件

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
 2InBlock.gif/// 利用Jmail接收邮件
 3ExpandedBlockEnd.gif/// </summary>
 4 None.gif private   void  ReciveByJmail()
 5 ExpandedBlockStart.gifContractedBlock.gif dot.gif {    
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    /**////建立收邮件对象
 7InBlock.gif    jmail.POP3Class popMail = new POP3Class();
 8InBlock.gif    
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    /**////建立邮件信息接口
10InBlock.gif    jmail.Message mailMessage; 
11InBlock.gif
12ExpandedSubBlockStart.gifContractedSubBlock.gif    /**////建立附件集接口
13InBlock.gif    jmail.Attachments atts;
14InBlock.gif
15ExpandedSubBlockStart.gifContractedSubBlock.gif    /**////建立附件接口
16InBlock.gif    jmail.Attachment att;
17InBlock.gif
18InBlock.gif    try
19ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
20InBlock.gif        popMail.Connect(TxtPopUser.Text.Trim(),TxtPopPwd.Text.Trim(),TxtPopServer.Text.Trim(),Convert.ToInt32(TxtPopPort.Text.Trim()));
21InBlock.gif        
22ExpandedSubBlockStart.gifContractedSubBlock.gif        /**////如果收到邮件
23InBlock.gif        if(0 < popMail.Count)                                                                          
24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{    
25ExpandedSubBlockStart.gifContractedSubBlock.gif            /**////根据取到的邮件数量依次取得每封邮件
26InBlock.gif            for(int i=1;i <= popMail.Count;i++)                                                       
27ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
28ExpandedSubBlockStart.gifContractedSubBlock.gif                /**////取得一条邮件信息
29InBlock.gif                mailMessage = popMail.Messages[i];                                                        
30InBlock.gif
31ExpandedSubBlockStart.gifContractedSubBlock.gif                /**////取得该邮件的附件集合
32InBlock.gif                atts = mailMessage.Attachments; 
33InBlock.gif                
34ExpandedSubBlockStart.gifContractedSubBlock.gif                /**////设置邮件的编码方式                          
35InBlock.gif                mailMessage.Charset = "GB2312"
36InBlock.gif                
37ExpandedSubBlockStart.gifContractedSubBlock.gif                /**////设置邮件的附件编码方式                         
38InBlock.gif                mailMessage.Encoding = "Base64"
39InBlock.gif                
40ExpandedSubBlockStart.gifContractedSubBlock.gif                /**////是否将信头编码成iso-8859-1字符集                        
41InBlock.gif                mailMessage.ISOEncodeHeaders = false
42InBlock.gif                
43ExpandedSubBlockStart.gifContractedSubBlock.gif                /**////邮件的优先级                     
44InBlock.gif                txtpriority.Text = mailMessage.Priority.ToString(); 
45InBlock.gif                
46ExpandedSubBlockStart.gifContractedSubBlock.gif                /**////邮件的发送人的信箱地址                      
47InBlock.gif                txtSendMail.Text = mailMessage.From; 
48InBlock.gif                
49ExpandedSubBlockStart.gifContractedSubBlock.gif                /**////邮件的发送人                     
50InBlock.gif                txtSender.Text = mailMessage.FromName; 
51InBlock.gif                
52ExpandedSubBlockStart.gifContractedSubBlock.gif                /**////邮件主题                   
53InBlock.gif                txtSubject.Text = mailMessage.Subject; 
54InBlock.gif                
55ExpandedSubBlockStart.gifContractedSubBlock.gif                /**////邮件内容                   
56InBlock.gif                txtBody.Text = mailMessage.Body; 
57InBlock.gif                
58ExpandedSubBlockStart.gifContractedSubBlock.gif                /**////邮件大小                        
59InBlock.gif                txtSize.Text = mailMessage.Size.ToString();                                                          
60InBlock.gif                
61InBlock.gif                for(int j=0;j<atts.Count;j++)
62ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
63ExpandedSubBlockStart.gifContractedSubBlock.gif                    /**////取得附件
64InBlock.gif                    att = atts[j];  
65InBlock.gif                    
66ExpandedSubBlockStart.gifContractedSubBlock.gif                    /**////附件名称                              
67InBlock.gif                    string attname = att.Name;                                                            
68InBlock.gif                    
69ExpandedSubBlockStart.gifContractedSubBlock.gif                    /**////上传到服务器
70InBlock.gif                    att.SaveToFile("e:\\attFile\\"+attname);                                             
71InBlock.gif                    
72ExpandedSubBlockEnd.gif                }

73InBlock.gif                
74ExpandedSubBlockEnd.gif            }

75InBlock.gif            panMailInfo.Visible = true;
76InBlock.gif            att = null;
77InBlock.gif            atts = null;
78ExpandedSubBlockEnd.gif        }

79InBlock.gif        else
80ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
81InBlock.gif            Response.Write("没有新邮件!");
82ExpandedSubBlockEnd.gif        }

83InBlock.gif
84InBlock.gif        popMail.DeleteMessages();
85InBlock.gif        popMail.Disconnect();
86InBlock.gif        popMail = null;
87ExpandedSubBlockEnd.gif    }

88InBlock.gif    catch
89ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
90InBlock.gif        Response.Write("Warning!请检查邮件服务器的设置是否正确!");
91ExpandedSubBlockEnd.gif    }

92ExpandedBlockEnd.gif}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值