Launching email application (MAPI) from C# (with attachment)

Steven picture Steven · Apr 24, 2009 · Viewed 19.6k times · Source

In the past I have used MAPISendMail to launch Outlook (or whatever the desired MAPI email application was) from a C++ application with a file attachment. (Similar to say Microsoft Word's Send Email functionality).

I need to do the equivalent from a C# application and to have it work when running on XP, Vista, Server 2008 (and Windows 7 I suppose).

MAPISendMail is a no go under Vista/2008 as it always returns MAPI_ E_FAILURE when Outlook is running and MAPI is not supported in managed code. Even after checking this fix: http://support.microsoft.com/kb/939718 I can't get it to reliably work.

I know that Microsoft Word & Adobe Reader 9 can both launch Outlook with an attachment under Vista.

A C# compatible solution would be preferred but I'd be happy with anything that works (doesn't have to use MAPI). I can't seem to find what the current "solution" is. None of the existing answers on Stack Overflow seem to cover this either.

Edit:

I am aware MAPI and C# do not work together, so I will take a C/C++ solution that works in Vista and Server 2008 when NOT running as administrator. See Adobe Reader 9 & Microsoft Word as examples that work.

Answer

AnotherDan picture AnotherDan · May 21, 2009

At work we have successfully done this using VSTO.

Here is a snippet of some lines we have running on VISTA with Outlook 2007: (the code is in VB.net).

Note that the usage is security locked when doing certain things to the outlook object. (to address, body and other properties marked as security risks). We use a 3rd party component (Redemption) to go around this security. If you dont use a security manager of some sort, outlook will give a little popup that something outside is trying to access it and you can give it access in a period of time.

The import of the Outlook interface.

Imports Outlook = Microsoft.Office.Interop.Outlook

This example is to give you some direction, not a full working example.

dim MailItem As Microsoft.Office.Interop.Outlook.MailItem

' Lets initialize outlook object '
MailItem = OutlookSession.Application.CreateItem(Outlook.OlItemType.olMailItem)
MailItem.To = mailto

MailItem.Subject = communication.Subject
MailItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML
MailItem.HTMLBody = htmlBody

MailItem.Attachments.Add(filename, Outlook.OlAttachmentType.olByValue)

' If True is supplied to Display it will act as modal and is executed sequential. '
SafeMail.Display(True)

The OutlookSession in the above example is coming from this Property:

    Public ReadOnly Property OutlookSession() As Outlook.NameSpace
        Get
            If Not OutlookApplication Is Nothing Then
                Return OutlookApplication.GetNamespace ("MAPI")
            Else
                Return Nothing
            End If
        End Get
    End Property

As you can see it is using MAPI inside for this.

Good luck with it.