TheXela
TheXela

Reputation: 71

Open new email window in Windows desktop e-mail client from C#

I am trying to open a filled email window with the method below which is called from a separate STA thread.

    private void SendMailMessage(object ignore)
    {
        MAPIHelperInterop.MapiMessage message = new MAPIHelperInterop.MapiMessage();

        using(RecipientCollection.InteropRecipientCollection interopRecipients
            = fRecipientCollection.GetInteropRepresentation())
        {
            message.Subject = fSubject;
            message.NoteText = fBody;

            message.Recipients = interopRecipients.Handle;
            message.RecipientCount = fRecipientCollection.Count;

            // Check if we need to add attachments
            if(fFiles.Count > 0)
            {
                // Add attachments
                message.Files = AllocateFileAttachments(out message.FileCount);
            }

            // Signal the creating thread (make the remaining code async)
            fManualResetEvent.Set();

            int error = MAPIHelperInterop.MAPISendMailW(IntPtr.Zero, IntPtr.Zero, message, 0x8, 0);

            if(fFiles.Count > 0)
            {
                // Deallocate the files
                DeallocateFileAttachments(message);
            }

            // Check for error
            if(error != SUCCESS_SUCCESS)
            {
                LogMAPIError(error);
            }
        }
  }

I have been testing this with Outlook and I keep getting error code 2 (MAPI_E_FAILURE) and nothing visible happens in Outlook (eventually it should work with any mail client, but Outlook is the main use case and so an extendable solution that works for Outlook would be a good first step). It works only if I either start Outlook as an administrator, or have Outlook closed when I run the code. I have tried calling MAPISendMailW with a handle to Outlook and with different combinations of flags but that did not work either.

The closest I have found to my problem is this https://social.msdn.microsoft.com/Forums/office/en-US/63e9f5b2-f5f2-4cf8-bdc2-ca1fad88ebe5/problem-with-outlook-and-mapisendmail-returns-mapiefailure-when-outlook-is-running?forum=outlookdev. In order to follow the suggested solution tried to run the SendMailMessage method in a separate AppDomain like this:

     public void ShowDialog()
    {
        Evidence e = new Evidence();
        e.AddHostEvidence(new Zone(SecurityZone.MyComputer));           
        AppDomain appDomain = AppDomain.CreateDomain("Outlook Launcher", e);            
        appDomain.DoCallBack(SendMailMessage);                     
    }

If I use "SecurityZone.MyComputer" then I get the same error as before and if I use any other SecurityZone I get this error message: "Request for the permission of type 'System.Security.Permissions.ReflectionPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed." Maybe that is not what they suggested in the above post but this is anything I can think off.

Thank you for your help.

Upvotes: 3

Views: 1080

Answers (1)

toby
toby

Reputation: 40

I have used MAPI in the past and it can be a pain to get working well across different Outlook versions.

A very simple work around is to use mailto links. The advantage is that it will work with different mail clients automatically.

 Process.Start("mailto:[email protected]&subject=This is the subject&body=This is the body");

This will open up your default mail client with the subject and body filled in.

Upvotes: 1

Related Questions