Domanda

So I had a lot of research on this topic. There was a COM approach, I spent quite a lot of time to implement it, it was involving the IMessage, IConverterSession, some reflection, things like that. Most answers I've found suggested this can't be used in C# but to me it seems there is a way. Anyway the closes I've ever gotten was to receive an empty IStream object (length = 0). The other thing that made me really decide to drop this method completelly was some reports it may not work under the 64 bit version of Outlook 2010.

The more obvious way was to simple get the MailItem from outlook and start building a MIME Message out of it. This one seems very tedious and error prone + it will additionally bloat my code with parsing / building MIME functionality.

I was thinking if there is another way, some way to leaverage existing .NET classes in order to easilly turn my Outlook Mail Item into MIME Message. I tought the MailMessage / SmtpClient classes may do the trick. So I did this:

        SmtpClient client = new SmtpClient();
        client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;

From then on I could get the message (I am specifying a new Drop directory for each message as I cannot have their names).

So before deep diving into reflection to see if I can get the contents without actually saving a file, is there any other built-in way in .Net to construct MIME Messages out of separate fields (from, to, subject, body, attachments), I don't insist on a method that will turn MailItem to MIME Message right away from me (although it will be good if there is any) but at least be able to set properties on some object and get the constructed message out of it later?

Thanks a lot!

È stato utile?

Soluzione

Okay, so here is how I solved it: First of all I am using the MailMessage class, filling it with the data from the mail item, something like this:

MailMessage msgMIME = new MailMessage();
msgMIME.Subject = mailItem.Subject; // mailItem is the Outlook Message.
// The rest of the logic here, setting from, to, bcc, cc, importance, body.

Then I use some of the code in this article in Code Project: Adding Save() functionality to Microsoft.Net.Mail.MailMessage

Except that I replaced the FileStream there with MemoryStream. Seems to work great.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top