.MSG文件元数据提取-发件人SMTP电子邮件

时间:2018-11-16 18:38:27

标签: c# email exchangewebservices office-interop aspose

我们的组织目前正在提取正在处理的旧的.MSG文件,这些文件已由现任和可能的前雇员存储。作为此过程的一部分,我们尝试从这些旧电子邮件中提取发件人SMTP电子邮件地址。但是,由于其中大多数是内部用户,因此msg文件将发件人地址存储为交换地址。我们已经尝试了以下方法,但是没有运气:

**

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;
}

我还需要获得什么其他选择?

1 个答案:

答案 0 :(得分:0)

在使用Aspose.Email.Mapi.MapiMessage时,此类的对象具有返回Sender信息的以下属性:

  • msg.SenderEmailAddress
  • msg.SenderSmtpAddress

这两个属性均获取或设置邮件发件人的电子邮件地址。

您可以使用以下代码段加载和读取MSG信息:

// 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);
}

注意:我是Aspose的支持开发人员/传播者。