MAPI and managed code experience?

Using MAPI functions from managed code is not officially supported. Apparently, the MAPI uses its own memory management, and it crashes and burns in managed code (see here and here )

All I want to do is launch the default email client with the subject, body, and one or more attachments .

So, I was looking for MAPISendDocuments and it seems to work. But I could not get the courage to actually use the function in production code.

Has anyone used this feature? Do you have terrible stories?

PS. No, I will not shellExecute Outlook.exe with command line arguments for attachments.

SFC. Support for attachments is a requirement, so Mailto: solutions do not cut for me.

+9
email pinvoke mapi
Aug 07 '08 at 7:56
source share
8 answers

Have a separate helper EXE that takes command line parameters (or a channel to its standard) that does what is required and calls it from the main application. This saves MAPI stuff outside of your main application space. Well, you are still mixing MAPI and .NET, but in a very short process. MAPI and CLR are supposed to start causing problems with longer processes.

We use Dmitry Streblechenko an excellent error data library that allows us to write such “cushioning” code in JScript and reference it, which saves the CLR and MAPI in separate processes, but are supported.

@ Chris Fournier re. writing an unmanaged DLL. This will not work because the problem is mixing the MAPI and managed code in the same process.

+7
01 Sep '08 at 20:26
source share

MAPISendDocuments is deprecated and can be removed. Instead, you should use MAPISendMail. See Simple MAPI

+2
Oct. 12 '09 at 14:51
source share

Process call. Start with Mailto: protocol (as shown below) will give you basic functions, but not attachments.

Process.Start("mailto:name@domain.com?subject=TestCode&Body=Test Text"); 

You can do this approach using attachment paths, but this option only works with some older version of Outlook 98. I assume that this involves a potential security risk.

If someone uses outlook.exe, he will provide security warnings in Outlook 2003 (and 2007 depends on the settings).

+1
Aug 07 '08 at 11:16
source share

You should be able to make an unmanaged DLL that performs the operations you want to use the MAPI, and then call this DLL from managed code. I would not write a direct MAPI shell, but something that performs all the MAPI functions you need contained in this unmanaged DLL. This would probably be the safest way to use MAPI from managed code.

+1
Aug 20 '08 at 20:01
source share

You can also use Outlook Redemption , which is supported from managed code; I'm not immediately sure that it has a simple replacement for MAPISendDocuments, but Dmitry is useful if you have questions.

As for the “glitches and burns”, here is another quote from the MS support guy, here

This is what will basically work. It will work while you write it. Then it will work while you are testing it. It will work while your client evaluates it. Then, as soon as the customer delivers it - BAM! This is when he decides to start a problem. And Microsoft will not help you with this, as we told you not to do this in the first place. :)

+1
Aug 27 '08 at 15:26
source share

I did this using the MAPISendMail function and several inner classes to port some other structures related to MAPI. As long as this is the only application, it is possible, although not trivial, to do it safely, since it requires very close attention to various unmanaged data types and memory allocation / deallocation and GC. Although it is still not supported, I use it in production code (although it has not been submitted yet).

When I asked Matt Schell about this, I got the answer:

I really don't know a much better way to do this, and any problems you encounter here will probably be reproducible in a supported scenario (i.e. VB6 or unmanaged C ++). Just know that if you have ever come across a script, the problem was caused by this particular function called from .NET, so we would not have any other recommendation for you not to use .NET.

Not really a blessing to use it, but it also doesn't say that there are other options to actually do this from managed code.

+1
Aug 31 '08 at 0:43
source share

The following code does not use MAPI as such, but it opens the Create Mail window with arbitrary attachments.

(in fact, it is completely untested, but I dug it out in an application that I think worked)

 using Microsoft.Office; using Microsoft.Office.Core; ... Outlook.Application outlook = new Outlook.Application(); Outlook.MailItem mail = (Outlook.MailItem) outlook.CreateItem(Outlook.OlItemType.olMailItem); mail.BodyFormat = Outlook.OlBodyFormat.olFormatRichText; mail.HTMLBody = "stuff"; mail.Subject = "more stuff"; string file = File.ReadAllBytes(...); mail.Attachments.Add(file, Outlook.OlAttachmentType.olByValue, 1, file) mail.Display(false); 
0
Dec 10 '10 at
source share

For someone who has experience with MAPI, it will take less time to check the code to do exactly what you want from unmanaged code (read: plain C ++) than print this post and read the answer (no offense) .

You are lucky that you need limited functionality. All you need is a simple C ++ utility to execute the parameters you need on the command line and issue the correct MAPI calls. Then you use this utility from your managed code just as you would for any other process.

NTN

-3
Apr 04 '09 at 5:46
source share



All Articles