C # Open eml

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.