Search code examples
c#outlookmodal-dialogmapi

Mapi opening default mail client under my application


Im using Mapi function to send e-mails from my application. What i want to do is to display new window of default mail client.

And it works- it open a new window with new mail defined (with all attachments etc.).

Problem is that on some machines, it doesnt open on top of my application. It's opening under my application, and its a bit confusing. My code looks like:

Importing function:

 [DllImport("MAPI32.DLL")]
    static extern int MAPISendMail(IntPtr sess, IntPtr hwnd, MapiMessage message, int flg, int rsv);

MapiMessage class:

public class MapiMessage
{
    public int reserved;
    public string subject;
    public string noteText;
    public string messageType;
    public string dateReceived;
    public string conversationID;
    public int flags;
    public IntPtr originator;
    public int receipCount;
    public IntPtr receips;
    public int fileCount;
    public IntPtr files;
}

Send Email function (Core):

 protected int SendMail(string strSubject, string strBody, int how)
    {
        m_MapiMessage = new MapiMessage();
        m_MapiMessage.subject = strSubject;
        m_MapiMessage.noteText = strBody;

        m_MapiMessage.receips = GetRecipients(out m_MapiMessage.receipCount);
        m_MapiMessage.files = GetAttachments(out m_MapiMessage.fileCount);

        m_lastError = MAPISendMail(new IntPtr(0), new IntPtr(0), m_MapiMessage, how, 0);
        return m_lastError;
    }

Send Email Function (UI):

public int SendMailPopup(string strSubject, string strBody)
    {
        return SendMail(strSubject, strBody, MAPI_LOGON_UI | MAPI_DIALOG);
    }

And the constants used above are:

   const int MAPI_LOGON_UI = 0x00000001;
    const int MAPI_DIALOG = 0x00000008;

I have tried setting my forms with:

TopMost=false;

But it didnt worked.

Any ideas, why on some machines the behaviour is different (every machine has updated windows 10 ) ?

Maybe there are some flags that are responsible for that behaviour in Mapi?


Solution

  • You need to specify the parent window handle. Here is what MSDN states for the second argument of the MAPISendMail function:

    If the value of the ulUIParam parameter is zero and a dialog box is displayed, the dialog box is application modal. If the ulUIParam parameter contains a parent window handle, it is of type HWND (cast to a ULONG_PTR). If no dialog box is displayed during the call, ulUIParam is ignored.