Utility.cs
资源名称:Email.rar [点击查看]
上传用户:hncsjykj
上传日期:2022-08-09
资源大小:461k
文件大小:21k
源码类别:
Email客户端
开发平台:
C#
- /******************************************************************************
- Copyright 2003-2004 Hamid Qureshi and Unruled Boy
- OpenPOP.Net is free software; you can redistribute it and/or modify
- it under the terms of the Lesser GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- OpenPOP.Net is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- Lesser GNU General Public License for more details.
- You should have received a copy of the Lesser GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- /*******************************************************************************/
- /*
- *Name: OpenPOP.Utility
- *Function: Utility
- *Author: Hamid Qureshi
- *Created: 2003/8
- *Modified: 2004/5/31 14:22 GMT+8 by Unruled Boy
- *Description:
- *Changes:
- * 2004/5/31 14:22 GMT+8 by Unruled Boy
- * 1.Fixed a bug in decoding Base64 text when using non-standard encoding
- * 2004/5/30 15:04 GMT+8 by Unruled Boy
- * 1.Added all description to all functions
- * 2004/5/25 13:55 GMT+8 by Unruled Boy
- * 1.Rewrote the DecodeText function using Regular Expression
- * 2004/5/17 14:20 GMT+8 by Unruled Boy
- * 1.Added ParseFileName
- * 2004/4/29 19:05 GMT+8 by Unruled Boy
- * 1.Adding ReadPlainTextFromFile function
- * 2004/4/28 19:06 GMT+8 by Unruled Boy
- * 1.Rewriting the Decode method
- * 2004/3/29 12:25 GMT+8 by Unruled Boy
- * 1.GetMimeType support for MONO
- * 2.cleaning up the names of variants
- */
- using System;
- using System.Text;
- using System.IO;
- using System.Threading;
- using System.Text.RegularExpressions;
- using System.Collections;
- namespace OpenPOP.MIMEParser
- {
- /// <summary>
- /// Summary description for Utility.
- /// </summary>
- public class Utility
- {
- private static bool m_blnLog=false;
- private static string m_strLogFile = "OpenPOP.log";
- public Utility()
- {
- }
- // public static string[] SplitText(string strText, string strSplitter)
- // {
- // string []segments=new string[0];
- // int indexOfstrSplitter=strText.IndexOf(strSplitter);
- // if(indexOfstrSplitter!=-1)
- // {
- //
- // }
- // return segments;
- // }
- //
- /// <summary>
- /// Verifies whether the file is of picture type or not
- /// </summary>
- /// <param name="strFile">File to be verified</param>
- /// <returns>True if picture file, false if not</returns>
- public static bool IsPictureFile(string strFile)
- {
- try
- {
- if(strFile!=null&&strFile!="")
- {
- strFile=strFile.ToLower();
- if(strFile.EndsWith(".jpg")||strFile.EndsWith(".bmp")||strFile.EndsWith(".ico")||strFile.EndsWith(".gif")||strFile.EndsWith(".png"))
- return true;
- else
- return false;
- }
- else
- return false;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// Parse date time info from MIME header
- /// </summary>
- /// <param name="strDate">Encoded MIME date time</param>
- /// <returns>Decoded date time info</returns>
- public static string ParseEmailDate(string strDate)
- {
- string strRet=strDate.Trim();
- int indexOfTag=strRet.IndexOf(",");
- if(indexOfTag!=-1)
- {
- strRet=strRet.Substring(indexOfTag+1);
- }
- strRet=QuoteText(strRet,"+");
- strRet=QuoteText(strRet,"-");
- strRet=QuoteText(strRet,"GMT");
- strRet=QuoteText(strRet,"CST");
- return strRet.Trim();
- }
- /// <summary>
- /// Quote the text according to a tag
- /// </summary>
- /// <param name="strText">Text to be quoted</param>
- /// <param name="strTag">Quote tag</param>
- /// <returns>Quoted Text</returns>
- public static string QuoteText(string strText, string strTag)
- {
- int indexOfTag=strText.IndexOf(strTag);
- if(indexOfTag!=-1)
- return strText.Substring(0,indexOfTag-1);
- else
- return strText;
- }
- /// <summary>
- /// Parse file name from MIME header
- /// </summary>
- /// <param name="strHeader">MIME header</param>
- /// <returns>Decoded file name</returns>
- public static string ParseFileName(string strHeader)
- {
- string strTag;
- strTag="filename=";
- int intPos=strHeader.ToLower().IndexOf(strTag);
- if(intPos==-1)
- {
- strTag="name=";
- intPos=strHeader.ToLower().IndexOf(strTag);
- }
- string strRet;
- if(intPos!=-1)
- {
- strRet=strHeader.Substring(intPos+strTag.Length);
- intPos=strRet.ToLower().IndexOf(";");
- if(intPos!=-1)
- strRet=strRet.Substring(1,intPos-1);
- strRet=RemoveQuote(strRet);
- }
- else
- strRet="";
- return strRet;
- }
- /// <summary>
- /// Parse email address from MIME header
- /// </summary>
- /// <param name="strEmailAddress">MIME header</param>
- /// <param name="strUser">Decoded user name</param>
- /// <param name="strAddress">Decoded email address</param>
- /// <returns>True if decoding succeeded, false if failed</returns>
- public static bool ParseEmailAddress(string strEmailAddress,ref string strUser, ref string strAddress)
- {
- int indexOfAB=strEmailAddress.Trim().LastIndexOf("<");
- int indexOfEndAB=strEmailAddress.Trim().LastIndexOf(">");
- strUser=strEmailAddress;
- strAddress=strEmailAddress;
- if(indexOfAB>=0&&indexOfEndAB>=0)
- {
- if(indexOfAB>0)
- {
- strUser=strUser.Substring(0,indexOfAB-1);
- // strUser=strUser.Substring(0,indexOfAB-1).Trim('"');
- // if(strUser.IndexOf(""")>=0)
- // {
- // strUser=strUser.Substring(1,strUser.Length-1);
- // }
- }
- strUser=strUser.Trim();
- strUser=strUser.Trim('"');
- strAddress=strAddress.Substring(indexOfAB+1,indexOfEndAB-(indexOfAB+1));
- }
- strUser=strUser.Trim();
- strUser=DecodeText(strUser);
- strAddress=strAddress.Trim();
- return true;
- }
- /// <summary>
- /// Save byte content to a file
- /// </summary>
- /// <param name="strFile">File to be saved to</param>
- /// <param name="bytContent">Byte array content</param>
- /// <returns>True if saving succeeded, false if failed</returns>
- public static bool SaveByteContentToFile(string strFile,byte[] bytContent)
- {
- try
- {
- if(File.Exists(strFile))
- File.Delete(strFile);
- FileStream fs=File.Create(strFile);
- fs.Write(bytContent,0,bytContent.Length);
- fs.Close();
- return true;
- }
- catch(Exception e)
- {
- Utility.LogError("SaveByteContentToFile():"+e.Message);
- return false;
- }
- }
- /// <summary>
- /// Save text content to a file
- /// </summary>
- /// <param name="strFile">File to be saved to</param>
- /// <param name="strText">Text content</param>
- /// <param name="blnReplaceExists">Replace file if exists</param>
- /// <returns>True if saving succeeded, false if failed</returns>
- public static bool SavePlainTextToFile(string strFile, string strText, bool blnReplaceExists)
- {
- try
- {
- bool blnRet=true;
- if(File.Exists(strFile))
- {
- if(blnReplaceExists)
- File.Delete(strFile);
- else
- blnRet=false;
- }
- if(blnRet==true)
- {
- StreamWriter sw=File.CreateText(strFile);
- sw.Write(strText);
- sw.Close();
- }
- return blnRet;
- }
- catch(Exception e)
- {
- Utility.LogError("SavePlainTextToFile():"+e.Message);
- return false;
- }
- }
- /// <summary>
- /// Read text content from a file
- /// </summary>
- /// <param name="strFile">File to be read from</param>
- /// <param name="strText">Read text content</param>
- /// <returns>True if reading succeeded, false if failed</returns>
- public static bool ReadPlainTextFromFile(string strFile, ref string strText)
- {
- if(File.Exists(strFile))
- {
- StreamReader fs=new StreamReader(strFile);
- strText=fs.ReadToEnd();
- fs.Close();
- return true;
- }
- else
- return false;
- }
- /// <summary>
- /// Sepearte header name and header value
- /// </summary>
- /// <param name="strRawHeader"></param>
- /// <returns></returns>
- public static string[] GetHeadersValue(string strRawHeader)
- {
- if(strRawHeader==null)
- throw new ArgumentNullException("strRawHeader","Argument was null");
- string []array=new string[2]{"",""};
- int indexOfColon=strRawHeader.IndexOf(":");
- try
- {
- array[0]=strRawHeader.Substring(0,indexOfColon).Trim();
- array[1]=strRawHeader.Substring(indexOfColon+1).Trim();
- }
- catch(Exception){}
- return array;
- }
- /// <summary>
- /// Get quoted text
- /// </summary>
- /// <param name="strText">Text with quotes</param>
- /// <param name="strSplitter">Splitter</param>
- /// <param name="strTag">Target tag</param>
- /// <returns>Text without quote</returns>
- public static string GetQuotedValue(string strText, string strSplitter, string strTag)
- {
- if(strText==null)
- throw new ArgumentNullException("strText","Argument was null");
- string []array=new string[2]{"",""};
- int indexOfstrSplitter=strText.IndexOf(strSplitter);
- try
- {
- array[0]=strText.Substring(0,indexOfstrSplitter).Trim();
- array[1]=strText.Substring(indexOfstrSplitter+1).Trim();
- int pos=array[1].IndexOf(""");
- if(pos!=-1)
- {
- int pos2=array[1].IndexOf(""",pos+1);
- array[1]=array[1].Substring(pos+1,pos2-pos-1);
- }
- }
- catch(Exception){}
- //return array;
- if(array[0].ToLower()==strTag.ToLower())
- return array[1].Trim();
- else
- return null;
- /* string []array=null;
- try
- {
- array=Regex.Split(strText,strSplitter);
- //return array;
- if(array[0].ToLower()==strTag.ToLower())
- return RemoveQuote(array[1].Trim());
- else
- return null;
- }
- catch
- {return null;}*/
- }
- /// <summary>
- /// Change text encoding
- /// </summary>
- /// <param name="strText">Source encoded text</param>
- /// <param name="strCharset">New charset</param>
- /// <returns>Encoded text with new charset</returns>
- public static string Change(string strText,string strCharset)
- {
- if (strCharset==null || strCharset=="")
- return strText;
- byte[] b=Encoding.Default.GetBytes(strText);
- return new string(Encoding.GetEncoding(strCharset).GetChars(b));
- }
- /// <summary>
- /// Remove non-standard base 64 characters
- /// </summary>
- /// <param name="strText">Source text</param>
- /// <returns>standard base 64 text</returns>
- public static string RemoveNonB64(string strText)
- {
- return strText.Replace("