none
How to get email address in ItemSend event of outlook vsto plugin RRS feed

  • Question

  • HI All,

    I am trying to access sender's and reciever's email address in item send event. I have tried following code :--

            private void HandleEvent_Application_ItemSend(object Item, ref bool Cancel)

            {
                if (Item is MailItem)
                {
                    MailItem objmail = Item as MailItem;
                    try
                    {

                        string accountName = objmail.SenderEmailAddress;

                        foreach (Recipient reciver in recipitentList)
                        {

                            string emailAddress = reciver.Address;

                        }

    But I am getting following type of value :--

    /o=DarkMatterORG/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=6579de53124448ddbc082d330d8352f5-Devendra T

    It is not giving me proper email address. Same type of value, I am getting for receiver. I have tried some Mapi property as well. Like using "0x39FE001E" for objMail says that property does not exist.

    "0x0C1F001F" property is giving same value. Can Some one please suggest how to solve it ?

    Thanks

    Devendra

    Sunday, July 9, 2017 4:15 PM

Answers

  • Use the file extension - read "Content Type" registry value from the HKCR\.ext (e.g. for text files, use HKCR\.txt).

    You can also use the MIME content type DB from HKEY_CLASSES_ROOT\MIME\Database\Content Type


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, July 10, 2017 6:10 PM
  • That is a perfectly valid EX type address (as opposed to SMTP). If you want the SMTP address, you need check the address type first - either read the PR_ADDRTYPE property using Recipient.PropertyAccessor.GetProperty (PR_ADDRTYPE DASL name is "http://schemas.microsoft.com/mapi/proptag/0x3002001F") or use Recipient.AddressEntry.Type (the latter is more expensive). If it is "EX", use Recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress (be prepared to handle nulls/exceptions), otherwise just use Recipient.Address.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Sunday, July 9, 2017 5:59 PM
  • Hi Dmitry,

    Thanks for response. As you had mentioned, it is working fine for Exchange address not for Smtp addresses. In my search, I have found one more mapi property :-- "0x39FE001E". In my testing it is working for both. 

    Can you suggest if you see any drawback to using this MAPI property ?

    Thanks

    Devendra

    Monday, July 10, 2017 2:43 AM

All replies

  • I have gotten success to get sender's email address and it is as following :--

    http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8580001F

    But I did not get any thing for receiver. Any help would be appreciable. 

    Thanks

    Devendra

    Sunday, July 9, 2017 4:25 PM
  • That is a perfectly valid EX type address (as opposed to SMTP). If you want the SMTP address, you need check the address type first - either read the PR_ADDRTYPE property using Recipient.PropertyAccessor.GetProperty (PR_ADDRTYPE DASL name is "http://schemas.microsoft.com/mapi/proptag/0x3002001F") or use Recipient.AddressEntry.Type (the latter is more expensive). If it is "EX", use Recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress (be prepared to handle nulls/exceptions), otherwise just use Recipient.Address.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Sunday, July 9, 2017 5:59 PM
  • Hi Dmitry,

    Thanks for response. As you had mentioned, it is working fine for Exchange address not for Smtp addresses. In my search, I have found one more mapi property :-- "0x39FE001E". In my testing it is working for both. 

    Can you suggest if you see any drawback to using this MAPI property ?

    Thanks

    Devendra

    Monday, July 10, 2017 2:43 AM
  • The property is called PR_SMTP_ADDRESS. It is not guaranteed to be present. What exactly does not work for you in case of SMTP addresses?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, July 10, 2017 5:22 AM
  • Hi Devendra Tewari,

    you had mentioned that,"In my search, I have found one more mapi property :-- "0x39FE001E". In my testing it is working for both."

    does that fulfil your requirement and solve our issue?

    if yes, then I suggest you to mark it as an answer.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 10, 2017 5:22 AM
    Moderator
  • HI Dmitry,

    Thanks for your reply. My point is that as per your suggestion, I have to first get AddressType and then if it is 'Ex', then we should follow the flow mentioned by you or I have to use .Address property. As per my opinion, using just one MAPI PR_SMTP_ADDRESS is simple and fast. But as you have mentioned it will not work in all case, I will vote for your solution.

    Thanks for help.

    Thanks

    Devendra

    Monday, July 10, 2017 9:00 AM
  • HI Dmitry,

    I have one more question.

    To access the attachment's content type we have mime property PR_ATTACH_MIME_TAG i.e. 0x370e001e. But for plain/text attachment like notepad file, it always returns empty string.

    Is this behavior as per expectation ?

    Does it return empty only for plain /text or it is possible for other cases as well ?

    Thanks

    Dev


    Monday, July 10, 2017 4:35 PM
  • PR_ATTACH_MIME_TAG  is no guaranteed to be present. Why do you need it? Why not use the file extension?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, July 10, 2017 4:47 PM
  • Hi Dmitry,

    Thanks for answer. I have to pass content type of attachment to one common module. They might use it to make mime from email parts. So we need exact content-type (as per rfc 822 standard). PR_ATTACH_MIME_TAG is giving values for all other cases except plain-text attachment. Am I using some wrong property ?

    Thanks

    Dev

    Monday, July 10, 2017 5:10 PM
  • Use the file extension - read "Content Type" registry value from the HKCR\.ext (e.g. for text files, use HKCR\.txt).

    You can also use the MIME content type DB from HKEY_CLASSES_ROOT\MIME\Database\Content Type


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, July 10, 2017 6:10 PM
  • HI Dmitry,

    Sorry to bother you again and again. But I need your suggestion for one more point. In my mail, is there any way to delete the extension from attachment of mail. In my case if there is any attachment without extension, outlook client add ".dat" extension with that. Is there any way to remove this extension in code ?

    Thanks

    Devendra

    Tuesday, July 11, 2017 4:58 PM
  • You might want to start a new thread.

    How exactly do you add an attachment? I have no problem sending attachments without a fiel extension.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, July 12, 2017 4:59 PM
  • Hi Dmitry,

    Thanks for reply. I am receiving mime from one component and I am converting it back to outlook mail item like below :--

            public static bool GetMailFromMime(string mimeSource, ref MailItem oldMail)
            {
                bool retVal = false;
                try
                {
                    if ((string.IsNullOrEmpty(mimeSource) != true) && (oldMail != null))
                    {
                        var mimeStream = new COMMemoryStream(Encoding.ASCII.GetBytes(mimeSource));

                        Type converter = Type.GetTypeFromCLSID(MAPIMethods.CLSID_IConverterSession);
                        object obj = Activator.CreateInstance(converter);
                        MAPIMethods.IConverterSession session = (MAPIMethods.IConverterSession)obj;

                        if (session != null)
                        {
                            uint hr = session.SetEncoding(MAPIMethods.ENCODINGTYPE.IET_QP);//IET_UNICODE
                            hr = session.SetSaveFormat(MAPIMethods.MIMESAVETYPE.SAVE_RFC822);

                            hr = session.MIMEToMAPI(mimeStream, (MAPIMethods.IMessage)oldMail.MAPIOBJECT, null, MAPIMethods.MAPITOMIMEFLAGS.CCSF_SMTP);

                            if (hr != 0)
                                throw new ArgumentException("There are some invalid COM arguments");

                            retVal = true;

                        }
                    }
                }

    Is this a wrong way to convert the mime ? Because I am facing one more issue. when I am sending a mail from 9 MB attachment , it is showing size of 33 MB in send item. However, receiver receives only 9 MB mail. So it is something with send item object.  it is stopping me to send large attachment as size of 20 MB attachment is showing 100 MB in send item, which outlook discards due to big size.

    Thanks

    Devendra

    Thursday, July 13, 2017 4:00 AM
  • HI Dmitry,

    I think, you are right. I should open a new thread, so that we can do a fresh discussion.

    Thanks

    Devendra

    Thursday, July 13, 2017 6:39 PM
  • Yes, I can confirm that IConverterSession::MIMEToMAPI indeed adds an extension to file attachments with no extension. Microsoft is aware of the problem, but there are no immediate plans to fix this.

    The only workaround I can suggest is to either write your own MIME converter, or use an existing converter - Redemption exposes its own converter through either RDOMail.Import(..., olRfc822_Redemption) or through the HrRFC822MessageToIMessage exported function.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Saturday, July 15, 2017 7:08 PM