简体   繁体   中英

.MSG File Metadata Extraction - Sender SMTP Email

Our organization is currently in the process of extracting a processing old .MSG files that have been stored by current and possibly former employees. As part of this process we are trying to extract the Sender SMTP email address from these old emails. However since most of these were/are internal users, the msg file has the Sender Address stored as an exchange address. We have tried the following to with no luck:

**

MsgReader.Outlook.Storage.Message

**

var senderAddress = string.Empty;
using (var msg = new Storage.Message(emailFilePath))
{
    senderAddress = msg.Sender.Email;
}

**

Microsoft.Office.Interop.Outlook.MailItem

**

var senderEmailAddress = this.SafeExtractEmailAddress(mail.Sender, mail.SenderEmailAddress);


private string SafeExtractEmailAddress(AddressEntry addressEntry, string currentEmail)
{
    var userEmailAddress = string.Empty;

    if (addressEntry != null &&
        (addressEntry.AddressEntryUserType == OlAddressEntryUserType.olExchangeUserAddressEntry
            || addressEntry.AddressEntryUserType == OlAddressEntryUserType.olExchangeRemoteUserAddressEntry))
    {
        userEmailAddress = addressEntry.GetExchangeUser()?.PrimarySmtpAddress;
    }

    if (string.IsNullOrWhiteSpace(userEmailAddress))
    {
        var recipient = this.outlookApplication.Session.CreateRecipient(currentEmail);
        var exchangeUser = recipient?.AddressEntry.GetExchangeUser();

        userEmailAddress = exchangeUser?.PrimarySmtpAddress ?? currentEmail;

        recipient.SafeRelease();
        exchangeUser.SafeRelease();
    }

    return userEmailAddress.ToLowerInvariant();
}

**

Aspose.Email.Mapi.MapiMessage

**

public string ExtractSender(MapiMessage msg)
{
    if (msg == null)
    {
        throw new ArgumentNullException(nameof(msg));
    }
    var senderEmailAddress = msg.SenderEmailAddress;
    ExchangeService exchangeService = this.exchangeServiceFactory.BuildService();

    // Create the ResolveNamesType and set the unresolved entry.

    if (msg.SenderAddressType == "EX")
    { 
        var resolutionCollection = exchangeService.ResolveName(msg.SenderEmailAddress, ResolveNameSearchLocation.ContactsThenDirectory, true);
        var nameResolutions = resolutionCollection.ToList();
        if (nameResolutions.Count > 0)
        {
            var nameResolution = nameResolutions.ElementAt(0);
            senderEmailAddress = nameResolution.Mailbox.Address;
        }
    }

    return senderEmailAddress;
}

What other options do I have to obtain this data?

While using Aspose.Email.Mapi.MapiMessage, the object of this class has following properties returning Sender information:

  • msg.SenderEmailAddress
  • msg.SenderSmtpAddress

Both properties get or set the message sender's e-mail address.

You may use the following code snippet to load and read MSG information:

// Create an instance of MapiMessage from file
MapiMessage msg = MapiMessage.FromFile(dataDir + @"message.msg");

// Get subject
Console.WriteLine("Subject:" + msg.Subject);

// Get from address
Console.WriteLine("Sender:" + msg.SenderEmailAddress);
Console.WriteLine("Sender SMTP:" + msg.SenderSmtpAddress);

// Get body
Console.WriteLine("Body" + msg.Body);

// Get recipients information
Console.WriteLine("Recipient: " + msg.Recipients);

// Get attachments
foreach (MapiAttachment att in msg.Attachments)
{
    Console.Write("Attachment Name: " + att.FileName);
    Console.Write("Attachment Display Name: " + att.DisplayName);
}

Note: I am working as Support developer/ Evangelist at Aspose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM