Use regular expressions to separate the various parts of the email. after the separation, you need to use base64 and qP to decode the content when reading the content. You need to determine the specific type based on the mail information.
Generally, the regular expression of the content encoding method obtained by mail is as follows:
-
C # code
-
Private Const StringEncodingreg= "(? <= (Content \-transfer \-encoding \\:)).*";
Make a textanalyze Method for overall analysis
-
-
C # code
-
// C content // Charset, text encoding, UTF-8/gb2312, etc. // Encoding: the encryption method, including QP and base64. // Because the encryption mark obtained by encoding may be different, the switch is used. Private String Textanalyze ( String C, String Charset, String Encoding ){ Switch (Encoding. tolower ()){ Case " Quoted-printable " : Case " QP " : Case " Q " : Try {C = Qdecode (encoding. getencoding (charset), c );} Catch (Exception e ){ Throw New Exception ( " Textanalyze (quoted-printabl) " + E. Message );} Break ; Case " Base64 " : Case " B " : Try {C = Decodebase64 (charset, encoding );} Catch (Exception e ){ Throw New Exception ( " Textanalyze (decodebase64) " + E. Message );} Break ;} Return C ;}
Base64 is easy to process in C #, so we put a QP decoding. This was also found on the Internet. At that time, my level was too concave and the Editor-in-Chief was not good.
-
-
C # code
-
/// <Summary> /// Quoted-printable DecodingProgram. /// </Summary> /// <Param name = "encoding"> Decodes the target Character Set </Param> /// <Param name = "data"> String to be decoded </Param> /// <Returns> </returns> Private Static String Qdecode (system. Text. Encoding encoding, String Data) {memorystream STRM = New Memorystream (system. Text. encoding. Default. getbytes (data )); Int B = STRM. readbyte (); memorystream dstrm = New Memorystream (); While (B > - 1 ){ // Hex eg. = e4 If (B = ' = ' ){ Byte [] Buf = New Byte [ 2 ]; STRM. Read (BUF, 0 , 2 ); If ( ! (BUF [ 0 ] = ' \ R ' && Buf [ 1 ] = ' \ N ' )){ Int Val = Int . Parse (system. Text. encoding. Default. getstring (BUF), system. Globalization. numberstyles. hexnumber ); // Int val = int. parse (system. Text. encoding. Default. getstring (BUF )); Byte [] Temp = New Byte [] {( Byte ) Val}; dstrm. Write (temp, 0 , Temp. Length );}} Else { String Encodedchar = Encoding. getstring ( New Byte [] {( Byte ) B }); Byte [] D = System. Text. encoding. Default. getbytes (encodedchar); dstrm. Write (D, 0 , D. Length);} B = STRM. readbyte ();} Return Encoding. getstring (dstrm. toarray ());}
At that time, the most difficult thing for me was the QP part. After this part was solved, others were very easy to handle. I don't know what your situation is.