Search code examples
c#outlook

Get the Email-Adress out of an Email from Outlook


i'm trying to get the Email-Address out of an Email from Outlook. I get the Email, only when i'm trying to get the Email-Adress out of an Email witch was send from the same Excanche, i get a weird String but how can i get only the Email-Adress out of it.

/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/[email protected]

Thats the Code where i get the Email-Address

private List<OutlookEmailObject> DecodeOutlookEmail(IDataObject data)
        {
            XPressSideBoardLogger.Log($"DecodeOutlookEmail()", true, ServiceLogger.LoggerMode.Verbose);
            List<OutlookEmailObject> outlookEmailObjects = new List<OutlookEmailObject>();
            OutlookDataObject dataObject = new OutlookDataObject(data);
            string[] filenames = (string[])dataObject.GetData("FileGroupDescriptorW");
            List<string> tempfiles = new List<string>();
            MemoryStream[] filestreams = (MemoryStream[])dataObject.GetData("FileContents");
            int memoryStreamIndex = 0;

            foreach (string s in filenames)
            {
                MemoryStream filestream = filestreams[memoryStreamIndex];
                tempfiles.Add(Path.GetTempPath() + s);
                FileStream outputStream = File.Create(Path.GetTempPath() + s);
                filestream.WriteTo(outputStream);
                outputStream.Close();
                memoryStreamIndex++;
            }
            foreach (string s in tempfiles)
            {
                using (Storage.Message msg = new Storage.Message(s))
                {
                    if (!_outlookDropInSearchbar)
                        AssignCategories(msg.Subject, msg.Sender.Email, msg.BodyText);
...


Solution

  • That looks like a perfectly valid address of type EX (as opposed to SMTP).

    You need to read PidTagSenderSmtpAddress MAPI property (DASL name is http://schemas.microsoft.com/mapi/proptag/0x5D01001F) or, if the property is not available, retrieve the SMTP address from from the sender GAL object (MailItem.Sender.GetExchangeUser().PrimarySmtpAddress in Outlook Object Model).

    I do not know what Storage.Message is and whether it allows to retrieve arbitrary MAPI proprieties, but your can get away with using just the Outlook Object Model - since the message needs to be selected first before it can be dragged out of Outlook, you can simply use the Application.ActiveExplorer.Selection collection.