Http://www.studa.net/pc-Theory/080505/11033039.html
Summary MIME is the most commonly used format standard of Internet Mail, this article gives the basic method of extracting all kinds of information from mail by analyzing and explaining the format specification of MIME message.
Key WordsMIME, mail, format, information extraction
MIME, English is all called "Multipurpose Internet Mail Extensions", that is, multi-purpose Internet Mail extension, is the current Internet e-mail messages generally follow the technical specifications. Before the advent of mime, Internet e-mail mainly follows the standard set by RFC 822, e-mail is generally used only to pass basic ASCII code text information, mime on the basis of RFC 822 to the e-mail specification to do a large number of extensions, introduced a new format specification and encoding method, With the support of MIME, the binary files such as image, sound and animation can be easily transmitted via e-mail, which greatly enriches the function of e-mail. Currently, the basic use of the Internet is to follow the MIME specification of e-mail.
e-mail analysis and reading are generally implemented through dedicated mail software, such as Outlook, Foxmail, but this third-party software can not be integrated with the developer's own system, through the MIME message format analysis, we are in our own applications to implement the MIME message contains the information contained in the read.1 MIME message format Analysis
The full content of the MIME specification is defined by RFC 2045-2049, including information formats, media types, encoding methods, and so on, where we only describe some of the key formats and specifications, and by understanding these format specifications, We can implement a programmatic way to extract basic message information from MIME messages.1.1 Domains
MIME message basic information, format information, encoding method and other important content are recorded in the message of the various fields, the basic format of the domain: {domain name}:{content}, domain by the domain name followed by ":" plus the information content of the domain, a field in the message to occupy one or more lines, the first line of the field can not have white space For example, a space or a tab, a field that occupies more than a row must begin with a blank character. You can also include attributes in the information content of a field, between attributes with ";" Delimited, the format of the property is as follows: {attribute name}= ' {attribute value} '.
Table 1 is the contents of a sample message, where row 1-5, line 8 are a single-line field, Row 6-7 is a multi-row field with a property named CharSet, and the property value is Us-ascii. Table 1 Sample e-mail
| Line 1 From: "Suntao" <[email protected]> line 2 to: <[email protected]> line 3 Subject:hello World row 4 Date:mon, 9 Oct 2006 16:51:34 + 0800 row 5 mime-version:1.0 line 6 Content-type:text/plain line 7 charset= "Us-ascii" line 8 Date:mon, 9 Oct 2006 16 : 48:25 + 0800 rows 9 rows ten Hello World 11 |
A large number of domains are defined in the message specification to store various messages related to the message, such as the sender's name and e-mail address information stored in the from domain, and the recipient's mail address information is stored in the to domain, and the developer can query the RFC to get a complete list of mail domain definitions.1.2 content-type Domain
The Content-type field defines the type of content and related properties that are contained in the message. The text, hypertext, attachments and other information contained in the message are stored in the message according to the media type, storage location, encoding method and other information specified by the corresponding Content-type domain. Content-type domain basic format: content-type:{main type}/{subtype}. Line 6-7 in the sample message is a Content-type field, the main type is text, the subtype is plain, and the character set property is Us-ascii. Table 2:mime main types common in messages
| Main type |
Common Properties |
Parameter meaning |
| Text |
CharSet |
Character set used for text information |
| Image |
Name |
The name of the image |
| Application |
Name |
The name of the application |
| Multipart |
Boundary |
Message Segmentation Boundary Identification |
1.3 Multipart Type
The various types of content in a MIME message are segmented, and the arrangement and location information for each segment is defined by the multipart type of the Content-type field. Multipart types mainly have three seed types: mixed, alternative, related. 1.3.1 Multipart type basic format multipart/mixed type
If an attachment is included in a message, the multipart/mixed type must be defined in the Content-type field of the message, and the message is divided into segments with the contents of the message through the boundary identity defined in the multipart/mixed type. The basic format is as follows: content-type:multipart/mixed; Boundary= "{fragment identifier}" multipart/alternative type
MIME messages can deliver hypertext content, but for compatibility reasons, it is common to send a copy of a plain text content at the same time as a hypertext format, and if both plain text and hypertext content are present in the message, the message needs to be defined in the Content-type domain multipart/ The alternative type, which divides plain text, hypertext, and other content of a message into separate segments through the segmentation identity in its boundary. The basic format is as follows: Content-type:multipart/alternative; Boundary= "{fragment identifier}" multipart/related type
In addition to carrying a variety of attachments, MIME messages can be stored in messages in the same way that other content is embedded within resources. For example, when we send HTML-formatted message content, we may use an image as the background of the HTML, the HTML text is stored in the alternative segment, and the image as the background is stored in the segment defined by the multipart/related type. The basic format is as follows: content-type:multipart/related; Type= "Multipart/alternative"; Boundary= "{fragment identifier}" 1.3.2 Multipart type boundary property
The boundary properties are defined in the subtypes of multipart, which use the strings defined in these boundary as identities, divide the contents of the message into different segments, and each sub-segment in the body begins with a "--" +boundary line, and the parent segment is "--" + boundary+ "--" line ends, separated by a blank line between different segments. 1.3.3 hierarchical relationships between multipart types of hierarchical tables 3:multipart subtypes
| Multipart/mixed |
| multipart/related |
| Multipart/alternative |
| Plain text body |
| Hypertext text |
|
| Embedded Resources |
|
| Attachment |
MIME messages divide the content into segments through the multipart type of multiple Content-type fields, which are not in line order in the message, but have a hierarchical relationship with each other, and the hierarchical structure of the multipart subtypes is shown in table 3.1.4 content-transfer-encoding Domain
MIME messages can transfer images, sounds, videos, and attachments, and these non-ASCII data are transferred through a certain encoding rule and attached to the message. The encoding is stored in the content-transfer-encoding domain of the message, and there may be multiple content-transfer-encoding domains in a message that correspond to the encoding of different parts of the message. Currently, data encoding in MIME messages is generally implemented using BASE64 encoding or quoted-printable encoding. 1.4.1 BASE64 Encoding
The purpose of the BASE64 encoding is to convert all the input data into a sequence of characters consisting of 64 specified ASCII characters, which are made up of the 64 characters from {' A '-' Z ', ' a '-' z ', ' 0 '-' 9 ', ' + ', '/'. The code will need to convert the data to take out 6bit each time, and then convert it to a decimal number, the range of the minimum is 0, the maximum is 63, and then query {' A '-' Z ', ' a '-' z ', ' 0 '-' 9 ', ' + ', '/'} form the dictionary table, the output , so that every 3 bytes of data content is converted to ASCII characters in 4 dictionaries, and when converted to less than 3 bytes at the end of the data, it is populated with "=". 1.4.2 quoted-printable Encoding
The purpose of quoted-printable encoding is also to convert the input information into printable ASCII characters, but it is based on the content of the information to determine whether to encode, if the read-in bytes in the range of 33-60, 62-126, these are directly printable ASCII characters, The direct output, if not, divides the byte into two 4bit, each represented by a 16-digit number, and then preceded by "=", so that each byte that needs to be encoded is converted to three characters.2 MIME message information extraction
As can be seen from the above analysis, MIME message delivery is actually a specially encoded and in a contract format of the sequence of characters, we just need to extract the information stored in the various fields of the message format, location and encoding, according to this information from the character sequence extracted from the corresponding character content and reverse decoding it, We can get the relevant content we need. Given below. NET environment, using C # in conjunction with regular expressions to extract relevant information from the message of the basic ideas and some code.2.1 Extraction of Recipient/sender/message subject
The recipient, sender, and message subject is the basic composing information of a message, which is stored in the from domain, to domain, and subject domain of the message. In development, you only need to match the specified fields with regular expressions and then remove the relevant information from the matching results.
Sample code: Extract the message subject string emailcontent = "...";//emailcontent stores the contents of the message Pat = @ "^subject:\s* (? <title>.*) \s*\r\n"; Mymatches = Regex.Matches (Emailcontent,pat,regexoptions.multiline); foreach (Match nextmatch in mymatches) {GroupCol lection mygroup = nextmatch.groups; string title = mygroup["title"]. ToString ();//title variable stores the contents of the From field ...}
It is important to note that the above code extracts the string following the subject: The following, if the subject content of the message is Chinese or other locale text that needs to be encoded, then it needs to be decoded. For example, if the information in the subject field of a message is "Hello", then the extracted string would be this form: =?gb2312? B?xoo6ww==?=, the first one? With the second one? The gb2312 between is the character set used for the title content, and the second one? And the third one? Between the B for this part of the content is the Base64 encoding method, if the use of Quoted-printabel encoding display Q, the third? And the fourth one? Between the "Hello" after the base64 encoded string.extraction of segmented information of 2.2 multipart
The message separates the content into different segments through the multipart type, and the boundary identifier between the segments is defined by the boundary attribute of the corresponding multipart type. To extract the required content from a message, you first need to extract the segment information from the message. The following code extracts all the name and boundary properties of the multipart type from a message. Sample code: Extract multipart information string emailcontent = "...";//emailcontent stores the message content string pat = @ "\bcontent-type:\s* (? <type>\ w+/\w+); \s+ (type=\s (? <subtype>\s+) \s) \s+boundary= "" (? <flag>\s+) "" "; MatchCollection mymatches = regex.matches (Emailcontent,pat); foreach (Match nextmatch in mymatches) {GroupCollection MyGroup = nextmatch.groups; String type = mygroup["type"]. ToString ();//type variable stores the name of the multipart type string flag = mygroup["Flag"]. ToString ();//flag variable stores the boundary property of the multipart type ...}2.3 Extraction of mail attachments
The attachment information in the message is defined by the corresponding Content-type field, content-transfer-encoding field, content-disposition field, and multipart/mixed type, the first three fields define the type of attachment, Name and encoding, multipart/mixed defines the fragment identifier of the attachment with the other content of the message. The basic format is as follows:
| --boundary segment Identification Content-type:application/msword; Name= "Readme.doc" content-transfer-encoding:base64content-disposition:attachment; Filename= "Readme.doc" ... BASE64 encoding of file contents ...--boundary segment identification |
Sample code: Extract Message Attachment//boundarymixed represents the boundary identity of the multipart/mixed type that has been extracted//decodebase64 as a custom Base64 decoding function// Decodequotedprintable is a custom quoted-printable decoding function string emailcontent = "...";//emailcontent stores the message content string pat = @ "\r\ ncontent-type:\s* (<filetype>\s*); \s*name= "" (?<name> \s*) "\s*content-transfer-encoding:\s* (?< encoding>\s*) \s*content-disposition:\s*attachment;\s*filename= "" (? <filename>\s+) "" \S+ (?& lt; content>[\s|\r\n]+) "+"--"+ boundarymixed; MatchCollection mymatches = regex.matches (emailcontent,pat,regexoptions.singleline); foreach ( Match NextMatch in Mymatches) { //Extract attachment type, encoding, file name, content information groupcollection MyGroup = nextmatch.groups; string fileType = mygroup["FileType"]. ToString (); string encoding = mygroup["encoding"]. ToString (); string filename = mygroup["filename"]. ToString (); string content = mygroup["Content"]. ToString (). Trim (); byte[] attachfile; //Decoding the extracted attachment contents according to the encoding of the attachment if (encoding = = "Base64") { attachfile = DecodeBase64 (content);} if (encoding = = "Quoted-printable") { attachfile = decodequotedprintable (content);} Writes the decoded content to disk FileStream fs = new FileStream ("c:\\" + fileName, filemode.createnew); BinaryWriter bw = new BinaryWriter (FS); bw. Write (attachfile); bw. Close (); fs. Close ();}
The above program extracts the attachment information from the original message and decodes it according to the encoding type used in the attachment, then stores the decoded content in the C packing directory according to the original file name. Similarly, if the file name of the attachment is Chinese or other text that needs to be encoded, the file name needs to be decoded first.3 Summary
This paper analyzes and expounds the basic format of MIME message, introduces several important specifications and definitions in mime, and gives the basic idea and method of extracting relevant information from message content by using regular expression. In development, it is important to note that the content contained in the message determines the specific format of the message, the type of multipart and the corresponding segment identification will only appear in the message when there is relevant content, in the development of the need for specific analysis. A detailed technical specification for MIME can be used to query RFC related documentation.Reference Documents[1] Francois liger,c# Text manipulation [M], Wrox press INC, 2002. [2] Internet rfc/std/fyi/bcp Archives [Eb/ol]., http://www.faqs.org/rfcs/
MIME Message Format analysis and information extraction