Is it possible to read .eml files in .net

后端 未结 3 1421
悲&欢浪女
悲&欢浪女 2021-01-06 03:48

I would like to know if it is possible to parse .eml and .msg files in dot net (preferably from a memorystream) such that I can use them on an ASP.Net page.

相关标签:
3条回答
  • 2021-01-06 04:18

    USE CDOSYS.DLL for EML files

    You can do this 'properly' (certainly better than trying to parse it as a text file) by using the COM cdosys.dll ('Microsoft CDO for Windows 2000 Library').

    You'll need to create a .Net wrapper for it by adding a reference: because it depends on ADODB.dll, you'll get Interop.CDO.dll and Interop.ADODB.dll created for you in your bin directory.

    There's a lot of information around for sending emails using CDO but almost nothing about using it to read EML files, but I found this page that got me started http://support.microsoft.com/kb/310224

    0 讨论(0)
  • 2021-01-06 04:25

    EML (MIME messages)

    EML are in most cases MIME encoded files with mail messages. Common sources of EML files include messages saved from Outlook Express or Thunderbird, messages downloaded from IMAP or POP3 servers.

    Loading an EML file correctly is not as easy as it looks. You can write an implementation working in 95% cases within few days. Remaining 5% would take at least several months ;-). I know, because I involved in developing one.

    Consider following difficulties:

    • unicode emails
    • right-to-left languages
    • correcting malformed EML files caused by well known errors in popular mail clients and servers
    • dealing with S/MIME (encrypted and signed email messages)
    • dealing correctly with several methods of encoding attachments
    • dealing with inline images and stylesheets embedded into HTML emails
    • making sure that it parses correctly a MIME torture message from Mike Crispin (coauthor of Mime and IMAP RFCs)
    • making sure that malformed message will not result in buffer overun or other application crash
    • handling hierarchical messages (message with attached messages)
    • making sure that it handles correctly very big emails

    Maturing of such parser takes years and continuous feedback for it's users. Right now is no such parser included in the .NET Framework. Until it changes I would suggest getting a third party MIME parser from an established vendor.

    Following code uses our Rebex Secure Mail component, but I'm sure that similar task could be replicated easily with components from other vendors as well.

    The code is based on Mail Message tutorial.

    // create an instance of MailMessage 
    MailMessage message = new MailMessage();
    
    // load the message from a local disk file 
    message.Load("c:\\message.eml");
    
    // load the message from MemoryStream
    MemoryStream stream = new MemoryStream(); 
    // TODO: fill the stream, seek to the beginning
    message.Load(stream);
    

    MSG (Outlook messages)

    MSG format is a email message format introduced by Microsoft in Microsoft Outlook. There is MSG format specification on Microsoft site. You may also want to try a third-party component. I'm aware of one MSG format component from IndependentSoft but haven't tried it personally. Also our Rebex Secure Mail version 1.0.4060.0 or later does support MSG format too.

    0 讨论(0)
  • 2021-01-06 04:26

    Yes you can. They are just regular text files, nothing fancy.

    This is what an eml file looks like on the inside

    X-Sender: somewhere@google.com
    X-Receiver: somewhere@google.com
    MIME-Version: 1.0
    From: somewhere@google.com
    To: somewhere@google.com
    Date: 7 Jun 2009 18:58:01 -0400
    Subject: From someone you know
    Content-Type: text/plain; charset=us-ascii
    Content-Transfer-Encoding: quoted-printable
    
    This is the body
    
    0 讨论(0)
提交回复
热议问题