jmail接收邮件

来源:互联网 发布:第三方软件检测机构 编辑:程序博客网 时间:2024/05/23 00:02
[c-sharp] view plaincopyprint?
  1. private void ReciveByJmail()  
  2.         {  
  3.             /**/  
  4.             ///建立收邮件对象      
  5.             jmail.POP3Class popMail = new POP3Class();  
  6.             /**/  
  7.             ///建立邮件信息接口      
  8.             jmail.Message mailMessage;  
  9.   
  10.             /**/  
  11.             ///建立附件集接口      
  12.             jmail.Attachments atts;  
  13.   
  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.                 popMail.Connect("邮箱名""密码""邮件服务器", 端口);  
  22.                 /**/  
  23.                 ///如果收到邮件      
  24.                 if (0 < popMail.Count)  
  25.                 {  
  26.                     /**/  
  27.                     ///根据取到的邮件数量依次取得每封邮件     
  28.                     for (int i = 1; i <= popMail.Count; i++)  
  29.                     {  
  30.                         /**/  
  31.                         ///取得一条邮件信息     
  32.                         mailMessage = popMail.Messages[i];  
  33.   
  34.                         /**/  
  35.                         ///取得该邮件的附件集合     
  36.                         atts = mailMessage.Attachments;  
  37.   
  38.                      //   mailMessage.Logging = true;//启用邮件日志  
  39.                      //   mailMessage.ContentType = "text/html";//邮件的格式为HTML格式  
  40.                     //    mailMessage.Silent = true; //屏蔽例外错误,返回FALSE跟TRUE两值  
  41.                           
  42.                         /**/  
  43.                         ///是否将信头编码成iso-8859-1字符集   
  44.                         mailMessage.ISOEncodeHeaders = true;  
  45.                         /**/  
  46.                         ///设置邮件的编码方式                               
  47.                         mailMessage.Charset = "UTF-8";  
  48.                        // mailMessage.Charset = "GB2312";  
  49.   
  50.                         /**/  
  51.                         ///设置邮件的附件编码方式                              
  52.                         mailMessage.Encoding = "Base64";  
  53.                        // mailMessage.ContentTransferEncoding = "base64";  
  54.                         /**/  
  55.                         ///邮件的优先级                          
  56.                         //txtpriority.Text = mailMessage.Priority.ToString();  
  57.   
  58.                         /**/  
  59.                         ///邮件的发送人的信箱地址                           
  60.                         txtSendMail.Text = mailMessage.From;  
  61.   
  62.                         /**/  
  63.                         ///邮件的发送人                          
  64.                         //txtSender.Text = mailMessage.FromName;  
  65.                         string formname = mailMessage.Headers.GetHeader("From");  
  66.                         txtSender.Text = DecodeStr(formname);  
  67.   
  68.                         /**/  
  69.                         ///邮件主题                        
  70.                         ///   
  71.                         //mailMessage.Headers.GetHeader("Subject");  
  72.                         string title1 = mailMessage.Headers.GetHeader("Subject");  
  73.                         txtSubject.Text = DecodeStr(title1);  
  74.   
  75.   
  76.                         /**/  
  77.                         ///邮件内容                        
  78.                         txtBody.Text = mailMessage.Body;  
  79.   
  80.                         /**/  
  81.                         ///邮件大小                             
  82.                         txtSize.Text = mailMessage.Size.ToString();  
  83.   
  84.                         for (int j = 0; j < atts.Count; j++)  
  85.                         {  
  86.                             /**/  
  87.                             ///取得附件     
  88.                             att = atts[j];  
  89.   
  90.                             /**/  
  91.                             ///附件名称                                   
  92.                             string attname = att.Name;  
  93.   
  94.                             /**/  
  95.                             ///上传到服务器     
  96.                             att.SaveToFile("e://attFile//" + attname);  
  97.   
  98.                         }  
  99.   
  100.                     }  
  101.                     //panMailInfo.Visible = true;  
  102.                     att = null;  
  103.                     atts = null;  
  104.                 }  
  105.                 else  
  106.                 {  
  107.                     Response.Write("没有新邮件!");  
  108.                 }  
  109.   
  110.                 // popMail.DeleteMessages();  
  111.                 popMail.Disconnect();  
  112.                 popMail = null;  
  113.             }  
  114.             catch(Exception e)  
  115.             {  
  116.                 //Response.Write("Warning!请检查邮件服务器的设置是否正确!");  
  117.                 Response.Write(e.ToString());  
  118.             }  


解决使用JMail接收邮件时的标题乱码



    #region UTF-8转ISO

        public string DecodeStr(string str)
        {
            string result = "";
            if (str != "" || str != null)
            {
                if (str.ToUpper().Contains("UTF-8"))
                {
                    String[] array = str.Split('?');
                    if (array.Length > 2)
                    {
                        string title = array[3];
                        byte[] bytes = Convert.FromBase64CharArray(title.ToCharArray(), 0, title.ToCharArray().Length);
                        Encoding en = Encoding.GetEncoding("utf-8");
                        result = en.GetString(bytes);
                    }

                }
            }
            return result;
        }
        #endregion