C#及JAVA之郵件服務器IMAP文件夾名稱編碼解碼實現(xiàn)(支持中文名稱)
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
IMAP郵件客戶端中讀取和創(chuàng)建郵件夾時需要對郵件夾名稱進行編碼和解碼。該方法是通過網(wǎng)絡上流傳的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:
該文章在 2017/10/25 14:27:59 編輯過 |
關鍵字查詢
相關文章
正在查詢... |