【hmailserver】IMAP文件夾名稱編碼及解碼
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("發(fā)件箱:" + IMAPEncode("發(fā)件箱") + "<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; } } 執(zhí)行結(jié)果: 收件箱:&ZTZO9nux- 發(fā)件箱:&U9FO9nux- 垃圾箱:&V4NXPnux- 草稿箱:&g0l6P3ux- 下載代碼: 相關(guān)教程:
該文章在 2020/7/7 17:28:11 編輯過(guò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |