Link to home
Start Free TrialLog in
Avatar of masoncooper
masoncooper

asked on

Sending Email using MAPI33

I am trying to use MAPI33 to send an email over a MAPI profile without triggering the security prompt.  Outlook Redemption does this to a limited extent but requires office and isn't as powerful.  
I will award 500 points to the first person to come up with complete working C# code that accomplishes this task using the MAPI33 library.

The function needs to be able to take the standard message fields such as To, CC, BCC, Subject, Body, and some form of attachments.  
If its any help, I am working with the source code sample provided at http://g8.cx/mapi so if you need to take advantage of any functions in the MapiWrapper.cs file, you can.
ASKER CERTIFIED SOLUTION
Avatar of muellerfan
muellerfan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of muellerfan
muellerfan

BTW, don't know if you've seen this link.  It's obviously not MAPI33, but it helped me get as far as I did:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/mapi/html/_mapi1book_handling_an_outgoing_message.asp
Avatar of masoncooper

ASKER

Great code muellerfan. I noticed you pass an outlook interop app object as an argument but  appobj isn't being used.  I guess this is something related to another bit of code of yours?  I was able to remove that argument and the code worked just fine.

Thanks for your help, A+ Response!
Thank you!  It was a challenge.  You are right about appobj being related to another bit of code that I'm actually not even using anymore.  I guess I can get rid of that argument.
Thanks!
Christie

Hi Do you know what happened? When i am using above code?
Thanks!

An unhandled exception of type 'System.MissingMethodException' occurred in finaltest.exe

Additional information: Method not found: MAPI33.Error __MAPI33__INTERNALS__.MAPITable.QueryRows(Int32, FLAGS, MAPI33.MapiTypes.Value[,] ByRef).
Hi,

How do I get the current user SMTP address in outlook 2003. See the code below but I get the erorr InvaliEntyId.

Thanks,
Dave

            MAPI33.IMAPISession session;
            Error hr;
            MAPIINIT init = new MAPIINIT();
            init.Flags = MAPIINIT.FLAGS.NoCoInit | MAPIINIT.FLAGS.MultithreadNotifications | MAPIINIT.FLAGS.NTService;
            //OPEN MAPI33.MAPI
            hr = MAPI33.MAPI.Initialize(init);

                hr = MAPI33.MAPI.LogonEx(IntPtr.Zero, null,
                      null, MAPI33.MAPI.FLAGS.Extended | MAPI33.MAPI.FLAGS.LogonUI | MAPI33.MAPI.FLAGS.NoMail,
                      out session);
                if (hr == 0)
                {
                    MessageBox.Show("Logon Successful");
                    getcurrentuser(ref session);
                   
                }
                else
                {
                    MessageBox.Show("Logon Failed " + hr.ToString());
                }
                session.Logoff(IntPtr.Zero, 0);
                session.Dispose();
                MAPI33.MAPI.Uninitialize();


 private void getcurrentuser(ref MAPI33.IMAPISession session)
        {
            Error hr;
            MAPI33.ENTRYID entry;
            MAPI33.IAddrBook ab;
            MAPI33.IUnknown unknown = null;
            MAPI33.MAPI.TYPE retType;
            string myaddress, myname;

            myaddress = myname = "";

            hr = session.QueryIdentity(out entry);
            hr = session.OpenAddressBook(IntPtr.Zero, Guid.Empty, 0, out ab);
            hr = ab.OpenEntry(entry, Guid.Empty, 0, out retType, out unknown);

            /* THIS IS A HACK.  SOMETIMES ON OUTLOOK 2003, QUERYIDENTITY
                DOESN'T WORK  THE FIRST TIME, BUT WILL ON THE 2ND OR 3RD TRY */
            int count = 0;
            while (hr != MAPI33.Error.Success && count < 10)
            {
                hr = session.QueryIdentity(out entry);
                hr = ab.OpenEntry(entry, Guid.Empty, 0, out retType, out unknown);
                count++;
            }

            if (hr != MAPI33.Error.Success)
            {
                MessageBox.Show(hr.ToString());
                return;
            }

            IMailUser mu = (IMailUser)unknown;
            unknown.Dispose();

            // Now we query the following properties:
            Tags[] ptags = new Tags[] { Tags.PR_DISPLAY_NAME, Tags.PR_SMTP_ADDRESS, Tags.PR_EMAIL_ADDRESS };
            MAPI33.MapiTypes.Value[] pvals = new MAPI33.MapiTypes.Value[0];

            // Now perform query
            hr = mu.GetProps(ptags, 0, out pvals);
            for (int k = 0; k < pvals.Length; k++)
            {
                Tags attrTag = Tags.PR_DEBUG;
                string attrVal = "";

                if (pvals[k].GetType() == typeof(MAPI33.MapiTypes.MapiString))
                {
                    attrTag = ((MAPI33.MapiTypes.MapiString)pvals[k]).PropTag;
                    attrVal = ((MAPI33.MapiTypes.MapiString)pvals[k]).Value;
                }

                if (pvals[k].GetType() == typeof(MAPI33.MapiTypes.MapiUnicode))
                {
                    attrTag = ((MAPI33.MapiTypes.MapiUnicode)pvals[k]).PropTag;
                    attrVal = ((MAPI33.MapiTypes.MapiUnicode)pvals[k]).Value;
                }

                if (attrTag != Tags.PR_DEBUG && attrVal.Length > 0)
                {
                    switch (attrTag)
                    {
                        case Tags.PR_DISPLAY_NAME_A:
                        case Tags.PR_DISPLAY_NAME_W:
                            myname = attrVal;
                            break;

                        case Tags.PR_SMTP_ADDRESS_A:
                        case Tags.PR_SMTP_ADDRESS_W:
                            myaddress = attrVal;
                            break;
                        case Tags.PR_EMAIL_ADDRESS_A:
                        case Tags.PR_EMAIL_ADDRESS_W:
                            //FOR MY CASE I ONLY WANT TO USE EMAIL IF SMTP IS NOT FOUND
                            if (myaddress == "")
                                myaddress = attrVal;
                            break;
                    }
                }
            }

            mu.Dispose();
            ab.Dispose();

        }