<p>Using MAPI functions from within managed code is officially unsupported. Apparently, MAPI uses its own memory management and it crashes and burns within managed code (see <a href="http://blogs.msdn.com/pcreehan/archive/2007/05/04/what-does-unsupported-mean.aspx" rel="noreferrer">here</a> and <a href="http://blogs.msdn.com/mstehle/archive/2007/10/03/fyi-why-are-mapi-and-cdo-1-21-not-supported-in-managed-net-code.aspx" rel="noreferrer">here</a>)</p>

<p><strong>All I want to do is launch the default e-mail client</strong> with subject, body, <strong>AND one or more attachments</strong>. </p>

<p>So I've been looking into <a href="http://pinvoke.net/default.aspx/mapi32.MAPISendDocuments" rel="noreferrer">MAPISendDocuments</a> and it seems to work. But I haven't been able to gather courage to actually use the function in production code.</p>

<p>Has anybody used this function a lot? Do you have any horror stories?</p>

<p><em>PS. No, I won't shellExecute Outlook.exe with command line arguments for attachments.</em></p>

<p><em>PPS. Attachment support is a</em> requirement <em>, so Mailto: solutions do not cut it for me.</em></p>

[{'answer_id': 4590, 'author': 'John', 'author_id': 33, 'author_profile': 'https://Stackoverflow.com/users/33', 'pm_score': 2, 'selected': False, 'text': '<p>Calling process.Start on the <a href="http://msdn.microsoft.com/en-us/library/aa767737(VS.85).aspx" rel="nofollow noreferrer">Mailto: protocol</a> (as shown below) will give you basic functionality but not attachments.</p>\n\n<pre><code>Process.Start("mailto:name@domain.com?subject=TestCode&amp;Body=Test Text");\n</code></pre>\n\n<p>You can do this approach with attachment paths but this option only works with some old version of outlook such as 98. I assume this is due to the potential securty risk. </p>\n\n<p>If anyone does use outlook.exe it will give security warnings under outlook 2003 (and 2007 Dependant on settings). </p>\n'}, {'answer_id': 18691, 'author': 'Chris', 'author_id': 2134, 'author_profile': 'https://Stackoverflow.com/users/2134', 'pm_score': 1, 'selected': False, 'text': "<p>You should be able to make an unmanaged DLL that performs the operations you want using MAPI, and then invoke that DLL from your managed code.  I wouldn't write a straight MAPI wrapper, but something that performs all of the functionality you require of MAPI contained in that unmanaged DLL.  That would probably be the safest way to use MAPI from managed code.</p>\n"}, {'answer_id': 30444, 'author': 'Dan Mitchell', 'author_id': 3220, 'author_profile': 'https://Stackoverflow.com/users/3220', 'pm_score': 1, 'selected': False, 'text': '<p>You could also use <a href="http://www.dimastr.com/redemption/" rel="nofollow noreferrer">Outlook Redemption</a>, which is supported from managed code; I\'m not immediately sure if it has a simple MAPISendDocuments replacement, but Dmitry\'s helpful if you have questions.</p>\n\n<p>As for "crashes and burns", here\'s another quote from an MS support guy, <a href="http://groups.google.com/group/microsoft.public.win32.programmer.messaging/msg/20149ae3de2f4920?hl=en&amp;" rel="nofollow noreferrer">here</a></p>\n\n<blockquote>\n  <p>It\'s the sort of thing that\'ll mostly work. It\'ll work while you\'re writing it. Then it\'ll work while you\'re testing it. It\'ll work while your customer is evaluating it. Then as soon as the customer deploys it - BAM! That\'s when it\'ll decide to start having problems. And Microsoft ain\'t gonna help you with it, since we told you not to do it in the first place. :)</p>\n</blockquote>\n'}, {'answer_id': 36518, 'author': 'Scott Dorman', 'author_id': 1559, 'author_profile': 'https://Stackoverflow.com/users/1559', 'pm_score': 1, 'selected': False, 'text': "<p>I have done this using the MAPISendMail function and several internal classes to wrap some of the other MAPI related structures. As long as this is the only use, it is possible although not trivial to do safely as it requires a very close attention to the various unmanaged data types and memory allocation/deallocation and GC. While it still isn't supported, I am using this in production code (although it hasn't shipped yet).</p>\n\n<p>When I asked Matt Stehle about this, the response I received was:</p>\n\n<blockquote>\n  <p>I really don't know of a much better way to do this and any issues you ran into here would be probably reproducible in a supported scenario (i.e. VB6 or unmanaged C++).  Just know that if you ever ran into a scenario were an issue was caused specifically by this function being called from .NET that we wouldn't have any other recommendation for you then to not use .NET.</p>\n</blockquote>\n\n<p>Not exactly a blessing on using it, but also not saying there are any other options to actually do this from managed code.</p>\n"}, {'answer_id': 38383, 'author': 'Duncan Smart', 'author_id': 1278, 'author_profile': 'https://Stackoverflow.com/users/1278', 'pm_score': 4, 'selected': True, 'text': '<p>Have a separate helper EXE that takes command-line params (or pipe to its StandardInput) that does what is required and call that from your main app. This keeps the MAPI stuff outside of your main app\'s process space. OK, you\'re still mixing MAPI and .NET but in a very short-lived process. The assumption is that MAPI and the CLR start causing issues with longer-running processes.</p>\n\n<p>We use Dmitry Streblechenko\'s superb <a href="http://www.dimastr.com/redemption/" rel="noreferrer">Redemption Data Objects</a> library which allows us to write such "shim" code in JScript and invoke that, which keeps the CLR and MAPI worlds in separate processes, but in a supported fashion.</p>\n\n<p>@Chris Fournier re. writing an unmanaged DLL. This won\'t work because the issue is mixing MAPI and managed code <em>in the same process</em>.</p>\n'}, {'answer_id': 716577, 'author': 'Ash', 'author_id': 43192, 'author_profile': 'https://Stackoverflow.com/users/43192', 'pm_score': -1, 'selected': False, 'text': "<p>For someone experienced with MAPI, it would take them less time to crank out the code to do exactly what you want from unmanaged code (read: plain C++) than typing this post and reading the response (no offense).</p>\n\n<p>You're lucky the functionality you need is limited. All you need is a simple C++ utility to take the params you need on the command-line and issue the right MAPI calls. Then, you all this utility from your managed code just as you'd to execute any other process.</p>\n\n<p>HTH</p>\n"}, {'answer_id': 1555033, 'author': 'André van Schoubroeck', 'author_id': 188450, 'author_profile': 'https://Stackoverflow.com/users/188450', 'pm_score': 2, 'selected': False, 'text': '<p>MAPISendDocuments is deprecated and might be removed.\nYou should use MAPISendMail instead.\nSee <a href="http://msdn.microsoft.com/nl-nl/library/dd296734(en-us,VS.85).aspx" rel="nofollow noreferrer">Simple MAPI</a></p>\n'}, {'answer_id': 4408605, 'author': 'Roman Starkov', 'author_id': 33080, 'author_profile': 'https://Stackoverflow.com/users/33080', 'pm_score': 0, 'selected': False, 'text': '<p>The following code doesn\'t use MAPI as such, but it does open the "Compose Mail" window with arbitrary attachments.</p>\n\n<p>(actually, it\'s entirely untested but I dug it up in an application that I believe to have worked)</p>\n\n<pre><code>using Microsoft.Office;\nusing Microsoft.Office.Core;\n\n...\n\nOutlook.Application outlook = new Outlook.Application();\nOutlook.MailItem mail = (Outlook.MailItem) outlook.CreateItem(Outlook.OlItemType.olMailItem);\n\nmail.BodyFormat = Outlook.OlBodyFormat.olFormatRichText;\nmail.HTMLBody = "stuff";\nmail.Subject = "more stuff";\nstring file = File.ReadAllBytes(...);\nmail.Attachments.Add(file, Outlook.OlAttachmentType.olByValue, 1, file)\n\nmail.Display(false);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/4508', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/227/']