Decode the mime-format email file to obtain its readable content and attachments.

Source: Internet
Author: User

In the past few days, we need to import all emails from the OA system X to the OA system Y. All emails of X are saved on the disk in the MIME format. Therefore, I need to read the content of these files, parse all related content and import it to y. For plain text emails, the trouble is that the emails with HTML format or attachments are searched by rummaging through the Internet, and finally a usable source code is found, what I need is the mime part of the project under the net directory.
However, after tests, one of the problems is mainly the support for Chinese characters and the calculation of the number of bytes. After my modifications and some functions I don't need, get a "mimedecoder", whereProgramThe Set name turns out to be lumisoft. net. dll. I re-compiled it into mimedecoder. dll, which retains the namespace of the original author. Do you know if this is against "Morality "? I don't know the Open Source protocols very well. If the practice is inappropriate or there is something missing, please give me a suggestion. Thank you.
The basic usage of this mimedecoder is as follows:

// Read email content
Filestream FS = File. openread ( " Mymimefile. In " );
Byte [] Buffer =   New   Byte [Fs. Length];
FS. Read (buffer, 0 ,( Int ) Fs. Length );
FS. Close ();

// Analyze email content
Lumisoft. net. Mime. mimeparser P =   New Mimeparser (buffer );
MessageBox. Show (P. Subject );
MessageBox. Show (P. bodytext );
Response. Write (P. mimeentries [I] As Mimeentry). filename +   " <Br> " );
Foreach (Mimeentry entry In P. mimeentries) // The original mimeentries uses arraylist, which may be for. NET 1.1. I changed it to list <mimeentry> for ease of use.
{
If ( False   =   String . Isnullorempty (entry. filename )) // Here we only save the attachment
{
FS = File. Create (entry. filename );
FS. Write (entry. Data, 0 , Entry. Data. Length );
FS. Close ();
}
}

The usage is relatively simple.
Due to the rush of time, whether there are other problems after this class library is modified has not been carefully tested (but it has never been tested). If a new problem is found and solved, I will update it in time.
Finally, I am very grateful to the original author Ivar Lumi of this class library for solving my major problems.
Here is the source code of the modified class library, which is published on the home page to all people who need it. If it is not suitable for the home page, tell me to change the category.

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.