LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C#及JAVA之邮件服务器IMAP文件夹名称编码解码实现(支持中文名称)

admin
2017年10月25日 11:19 本文热度 6056
IMAP邮件客户端中读取和创建邮件夹时需要对邮件夹名称进行编码和解码。该方法是通过网络上流传的perl语言方法改写的,配合InterIMAP项目可以编写完整IMAP邮件客户端。
InterIMAP项目地址: http://interimap.codeplex.com/
编码和解码方法,具体含义可以参考国际标准的定义:http://www.faqs.org/rfcs/rfc3501.html
C#:
//编码
private string IMAPEncode(string folder)
{
    string rtn = "", base64;
    int index = 0;
    Regex regAsis = new Regex(@"\G(?:[\x20-\x25\x27-\x7e])+");
    Regex reg26 = new Regex(@"\G&");
    Regex regEncode = new Regex(@"\G(?:[^\x20-\x7e])+");
    Regex regEq = new Regex(@"=+$");
    Regex regSlash = new Regex(@"\/");
    while (index < folder.Length)
    {
        Match m;
        m = regAsis.Match(folder, index);
        if (m.Success)
        {
            index = index + m.Length;
            rtn = rtn + m.Value;
            continue;
        }
        m = reg26.Match(folder, index);
        if (m.Success)
        {
            index = index + m.Length;
            rtn = rtn + "&-";
            continue;
        }
        m = regEncode.Match(folder, index);
        if (m.Success)
        {
            index = index + m.Length;
            base64 = Convert.ToBase64String(Encoding.GetEncoding("UTF-16BE").GetBytes(m.Value));
            base64 = regEq.Replace(base64, "");
            base64 = regSlash.Replace(base64, ",");
            rtn = rtn + "&" + base64 + "-";
            continue;
        }
    }
    return rtn;
}
       
// 解码
private string IMAPDeconde(string folder)
{
    string rtn = "", base64;
    int index = 0;
    Regex regAsis = new Regex(@"\G([^&]+)");
    Regex reg26 = new Regex(@"\G\&-");
    Regex regDecode = new Regex(@"\G\&([A-Za-z0-9+,]+)-?");
    Regex regComma = new Regex(@",");
    while (index < folder.Length)
    {
        Match m;
        m = regAsis.Match(folder, index);
        if(m.Success)
        {
            index = index + m.Length;
            rtn = rtn + m.Value;
            continue;
        }
        m = reg26.Match(folder, index);
        if(m.Success)
        { index = index + m.Length;
            rtn = rtn + "&";
            continue;
        }
        m = regDecode.Match(folder, index);
        if(m.Success)
        {
            index = index + m.Length;
            base64 = m.Value.Substring(1, m.Value.Length - 2);
            base64 = regComma.Replace(base64, "/");
            int mod = base64.Length % 4;
            if(mod > 0 )
            {
                int count = 4 - mod;
                while (count > 0)
                {
                    base64 += "=";
                    count--;
                }
                //base64 = base64.PadRight(base64.Length + (4 - mod), "=");
            }
            base64 = Encoding.GetEncoding("UTF-16BE").GetString(Convert.FromBase64String(base64));
            rtn = rtn + base64;
            continue;
        }
    }
    return rtn;
}

JAVA:
  1. ImapFolderEncoder {  
  2.     public static String encode(String folder) {  
  3.     String rtn = "", base64;  
  4.     int index = 0;  
  5.     Pattern regAsis = Pattern.compile("\\G(?:[\\x20-\\x25\\x27-\\x7e])+");  
  6.     Pattern reg26 = Pattern.compile("\\G&");  
  7.     Pattern regEncode = Pattern.compile("\\G(?:[^\\x20-\\x7e])+");  
  8.     Pattern regEq = Pattern.compile("=+$");  
  9.     Pattern regSlash = Pattern.compile("\\/");  
  10.     while (index < folder.length()) {  
  11.         Matcher m;  
  12.         m = regAsis.matcher(folder);  
  13.         if (m.find(index)) {  
  14.         index = index + (m.end() - m.start());  
  15.         rtn = rtn + m.group();  
  16.         continue;  
  17.         }  
  18.         m = reg26.matcher(folder);  
  19.         if (m.find(index)) {  
  20.         index = index + (m.end() - m.start());  
  21.         rtn = rtn + "&-";  
  22.         continue;  
  23.         }  
  24.         m = regEncode.matcher(folder);  
  25.         if (m.find(index)) {  
  26.         index = index + (m.end() - m.start());  
  27.         base64 = SimpleUtil.encodeBase64Content(m.group(), "UTF-16BE");  
  28.         base64 = base64.replaceAll(regEq.pattern(), "");  
  29.         base64 = base64.replaceAll(regSlash.pattern(), ",");  
  30.         rtn = rtn + "&" + base64 + "-";  
  31.         continue;  
  32.         }  
  33.     }  
  34.     return rtn;  
  35.     }  
  36.       
  37. }  
  1. ImapFolderDecoder {  
  2.     public static String decode(String folder) {  
  3.     String rtn = "", base64;  
  4.     int index = 0;  
  5.     Pattern regAsis = Pattern.compile("\\G([^&]+)");  
  6.     Pattern reg26 = Pattern.compile("\\G\\&-");  
  7.     Pattern regDecode = Pattern.compile("\\G\\&([A-Za-z0-9+,]+)-?");  
  8.     Pattern regComma = Pattern.compile(",");  
  9.     while (index < folder.length()) {  
  10.         Matcher m;  
  11.         m = regAsis.matcher(folder);  
  12.         if (m.find(index)) {  
  13.         index = index + (m.end() - m.start());  
  14.         rtn = rtn + m.group();  
  15.         continue;  
  16.         }  
  17.         m = reg26.matcher(folder);  
  18.         if (m.find(index)) {  
  19.         index = index + (m.end() - m.start());  
  20.         rtn = rtn + "&";  
  21.         continue;  
  22.         }  
  23.         m = regDecode.matcher(folder);  
  24.         if (m.find(index)) {  
  25.         index = index + (m.end() - m.start());  
  26.         base64 = m.group().substring(1, m.group().length() - 1);  
  27.         base64 = base64.replaceAll(regComma.pattern(), "/");  
  28.         int mod = base64.length() % 4;  
  29.         int count = 4 - mod;  
  30.         while (count > 0) {  
  31.             base64 += "=";  
  32.             count--;  
  33.         }  
  34.         base64 = SimpleUtil.base64Decode(base64, "UTF-16BE");  
  35.         rtn = rtn + base64;  
  36.         continue;  
  37.         }  
  38.     }  
  39.     return rtn;  
  40.     }  
  41. }  

该文章在 2017/10/25 14:27:59 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved