0
votes

I am writing a C# program that makes use of MAPI. I know I should use a different language for this, but I was told to use .NET. I have the application running, but I am having a couple of problems logging in.

I can log in just fine using the current user account, but when I try to log in to a dedicated account, the mail is still sent from the current user account. My login code follows:

    private bool Logon()
    {
        if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(password))
        {
            error = MAPILogon(WinHandle, null, null, 0, 0, ref session);
            Logging.Log("Logged in using credentials for the current user.");
        }
        else
            error = MAPILogon(WinHandle, username, password, 0, 0, ref session);

        if (error != 0)
            error = MAPILogon(WinHandle, null, null, MapiLogonUI, 0, ref session);

        return error == 0;
    }

    [DllImport("MAPI32.DLL", CharSet = CharSet.Ansi)]
    private static extern int MAPILogon(IntPtr hwnd, string profileName, string password, 
        int flag, int rsv, ref IntPtr session);

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

I have added a null terminator to the username and password ("\0") so it seems like I should be able to log in as a different user.

I tried closing my Outlook and running the application and it said that it sent the message correctly, but froze on closing. Outlook would no longer open, and eventually I rebooted my computer. Does Outlook have to be open to use MAPI controls?

Finally, when the message is sent, I get a confirmation that "A program is trying to send an e-mail message on your behalf." Since this app may be sending several messages a day, I don't want the user to have to keep pressing Allow. Is there a way to suppress this message programatically or as an Outlook or Exchange Server configuration?

My dev environment is Windows 7 and Outlook 2010, but this needs to work in all environments. Sorry for having so many questions; I appreciate any insights people may have.

=== REVISED ---

The first part of my question has been answered. MAPI is logging in to a profile. However, I still wonder if anyone knows how to repress the access warning message, and to get Simple MAPI to log in to an account other than the one the user is logged in to. I need to do this because all email from any station will be sent from a single dedicated account.

I would be happy to use extended MAPI if I could find more detailed information on how to use it. I found some information and even a sample program, but it gives me an error saying that "Either there is no default mail client or the current mail client cannot fulfill the messaging request". I tried changing the processor the application is built for, but the message persisted. Since my simple MAPI application does not have this error, I know I have a default email client.

Again, thank you for any assistance.

2

2 Answers

1
votes

MAPILogon takes the name of an existing profile, not a user name. Have you tried to use the Outlook Object Model? Why are you trying to use Simple MAPI? For all Outlook related question you muight want to post at http://social.msdn.microsoft.com/Forums/en/outlookdev/threads

0
votes

I had to switch over to using Extended MAPI, and was able to resolve both of these issues.

Thank you, Dimitry, for your help both on this site and here.