【hmailserver】IMAP文件夹名称编码及解码
当前位置:点晴教程→点晴OA办公管理信息系统
→『 经验分享&问题答疑 』
using System; using System.Text.RegularExpressions; public partial class MIS_uploader_Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Response.Write("收件箱:" + IMAPEncode("收件箱") + "<br>"); Response.Write("发件箱:" + IMAPEncode("发件箱") + "<br>"); Response.Write("垃圾箱:" + IMAPEncode("垃圾箱") + "<br>"); Response.Write("草稿箱:" + IMAPEncode("草稿箱") + "<br>"); } // 编码 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(System.Text.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) base64 = base64.PadRight(base64.Length + (4 - mod), '='); base64 = System.Text.Encoding.GetEncoding("UTF-16BE").GetString(Convert.FromBase64String(base64)); rtn = rtn + base64; continue; } } return rtn; } } 执行结果: 收件箱:&ZTZO9nux- 发件箱:&U9FO9nux- 垃圾箱:&V4NXPnux- 草稿箱:&g0l6P3ux- 下载代码: 相关教程:
该文章在 2020/7/7 17:28:11 编辑过 |
关键字查询
相关文章
正在查询... |