Asked by:
C# MAPI Issues

Question
-
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. The application will be used to send emails under an account different from the logged in user. 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?
I have read that the login is for a profile name rather than a username. Is this a Windows Profile? My understanding is that Exchange uses accounts.
Finally, when the message is sent from the default profile, 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. As you can see above, I am using simple MAPI. I've done some testing with Extended MAPI, but keep getting a message saying that there is no default mail client.
I appreciate any insights people may have.
Monday, December 3, 2012 10:40 PM
All replies
-
You are using Simple MAPI. Extended MAPI cannot be used from .Net languages.
The profile name is the name of the profile as specified in Control Panel | Mail | Show Profiles.
Why not use the Outlook Object Model?
Dmitry Streblechenko (MVP) http://www.dimastr.com/redemption
Redemption - what the Outlook
Object Model should have been
Version 5.4 is now available!Monday, December 3, 2012 11:01 PM -
Thank you for your response Dmitry. I appreciate the information you gave me on the login.
I am relatively new to working with mail, and have never done anything with MAPI. We currently have a pre-existing application that will send mail through SMTP, but one customer says that he will only allow access through MAPI. This is why I am not using Outlook Object Model.
I need to be able to allow a user, whether they are logged in to the Exchange server or just a Windows account, to send an email through a different profile. All mail from the company will be sent through the same account, which will be completely automated.
Again, thank you all for any assistance. I have spent the last couple of days searching for information that would allow me to log in to Exchange as a different user and suppress the access warning messages, and have had little luck
Tuesday, December 4, 2012 4:19 PM -
Does your customer mean Simple MAPI or Extended MAPI? What is the basis for that requirement?
Dmitry Streblechenko (MVP) http://www.dimastr.com/redemption
Redemption - what the Outlook
Object Model should have been
Version 5.4 is now available!Tuesday, December 4, 2012 10:24 PM -
Thank you again for your response Dimitry!
The customer did not specify simple or extended, so I assume either would do. I am not in contact with the customer, but I can only assume that his reason for wanting to use MAPI has something to do with some security benefit or perceived benefit.
Tuesday, December 4, 2012 11:25 PM -
Simple and Extended MAPi have nothing in common besides having "MAPI" in their names...
Can you find out what and why exactly the customer wants?
Dmitry Streblechenko (MVP) http://www.dimastr.com/redemption
Redemption - what the Outlook
Object Model should have been
Version 5.4 is now available!Tuesday, December 4, 2012 11:51 PM