管理邮件附件

在 Outlook 中处理附件

创建和保存 Outlook 消息(MSG)文件 解释了如何创建和保存消息,以及如何创建带附件的 MSG 文件。本文说明了如何使用 Aspose.Email 管理 Microsoft Outlook 附件。使用该方法可以访问消息文件中的附件并将其保存到磁盘。 MapiMessage附件 属性的重载版本。 附件 属性是类型为的集合 MapiAttachmentCollection 类。

检查附件类型(内联或常规)

内联附件和常规附件的用途不同。内联附件在视觉上集成到电子邮件中,通常是图像或媒体文件。而常规附件是附加在电子邮件上的独立文件,可能包括各种类型的文件。 MapiAttachment.IsInline 属性的 MapiAttachment 类获取指示附件是内联还是普通的值。

下面的代码示例提取并显示已加载 MapiMessage 中每个附件的信息,包括其显示名称以及是否为内联附件。

var message = MapiMessage.Load(fileName);

foreach (var attach in message.Attachments)
{
    Console.WriteLine($"{attach.DisplayName0} : {attach.IsInline)}");
}

检查附件类型(IsReference)

MapiAttachment 类包含了 IsReference 属性允许开发人员在消息中识别引用附件。使用下面的代码示例,您可以检查附件是否为引用附件:

foreach (var attachment in msg.Attachments)
{
    if (attachment.IsReference)
    {
        // Process reference attachment
    }
}

从 MSG 文件保存附件

从 MSG 文件保存附件:

  1. 遍历 MapiAttachmentCollection 集合并获取各个附件。
  2. 要保存附件,请调用 MapiAttachment 类的 Save() 方法。

下面的代码片段展示了如何将附件保存到本地磁盘。

从 RTF 格式的 MSG 文件中提取附件

对于 RTF 格式的消息,可以使用以下代码区分并提取内联附件或以图标形式出现在消息正文中的附件。下面的代码片段展示了如何识别并提取 RTF 格式 MSG 中的嵌入式附件。


var eml = MapiMessage.Load("MSG file with RTF Formatting.msg");

foreach (var attachment in eml.Attachments)
{
    if (IsAttachmentInline(attachment))
    {
        try
        {
            SaveAttachment(attachment, Data.Out/new Guid().ToString());
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

static bool IsAttachmentInline(MapiAttachment attachment)
{
    foreach (var property in attachment.ObjectData.Properties.Values)
    {
        if (property.Name == "\x0003ObjInfo")
        {
            var odtPersist1 = BitConverter.ToUInt16(property.Data, 0);
            return (odtPersist1 & (1 << (7 - 1))) == 0;
        }
    }
    return false;
}

static void SaveAttachment(MapiAttachment attachment, string fileName)
{
    foreach (var property in attachment.ObjectData.Properties.Values)
    {
        if (property.Name == "Package")
        {
            using var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
            fs.Write(property.Data, 0, property.Data.Length);
        }
    }
}

获取嵌套邮件附件

嵌入的 OLE 附件也出现在 MapiMessage Attachment 类的集合。下面的代码示例解析消息文件中的嵌入式邮件附件并将其保存到磁盘。 MapiMessage FromProperties 类的静态方法可以从嵌入的附件创建新消息。下面的代码片段展示了如何获取嵌套的邮件附件。

删除附件

Aspose Outlook 库提供了从 Microsoft Outlook 消息(.msg)文件中删除附件的功能:

  • 调用 RemoveAttachments() 方法。它接受消息文件的路径作为参数。该方法实现为公共静态方法,无需实例化对象。

下面的代码片段展示了如何删除附件。

您也可以调用 MapiMessage 类的静态方法 DestroyAttachment()。它比 RemoveAttachment() 更快,因为 RemoveAttachment() 方法会解析消息文件。

添加 MSG 附件

Outlook 消息可以在附件中包含其他 Microsoft Outlook 消息,无论是常规还是嵌入式消息。 MapiAttachmentCollection 提供了 Add 方法的重载成员,以创建包含两种类型附件的 Outlook 消息。

向 MapiMessage 添加引用附件

ReferenceAttachmentOptions 该类通过将所有必要属性封装在一个对象中,简化了添加引用附件的过程。

ReferenceAttachmentOptions 参数:

  • sharedLink:由托管文件的网络服务提供的完整共享链接。
  • url:文件位置或资源 URL。
  • providerName:引用附件提供者的名称(例如 Google Drive、Dropbox)。
  • 示例:使用 ReferenceAttachmentOptions 添加引用附件
var options = new ReferenceAttachmentOptions(
    "https://drive.google.com/file/d/1HJ-M3F2qq1oRrTZ2GZhUdErJNy2CT3DF/",
    "https://drive.google.com/drive/my-drive",
    "GoogleDrive");

// Add reference attachment
msg.Attachments.Add("Document.pdf", options);

将消息嵌入为附件

下面的代码片段展示了如何将 MSG 文件附件嵌入到消息中。

从附件读取嵌入式消息

以下代码片段展示了如何从附件中读取嵌入式消息。

插入和替换附件

Aspose.Email API 提供了在父消息中按特定索引插入附件的能力,还提供了将附件内容替换为另一条消息附件的功能。

在特定位置插入附件

Aspose.Email API 提供了使用 MapiAttachmentCollection 的 Insert 方法(MapiAttachmentCollection Insert(int index, string name, MapiMessage msg))将 MSG 附件插入到父 MSG 中的功能。下面的代码片段展示了如何在特定位置插入附件。

替换附件内容

可以使用 Replace 方法将嵌入的附件内容替换为新内容。但无法在集合计数为 2 的情况下,向集合中插入 PR_ATTACH_NUM = 4(例如)的附件。下面的代码片段展示了如何替换附件内容。

重命名 MapiMessage 中的附件

可以编辑 MapiMessage 附件中的 DisplayName 属性值。

var msg = MapiMessage.Load(fileName);
msg.Attachments[0].DisplayName = "New display name 1";
msg.Attachments[1].DisplayName = "New display name 2";

从数字签名的消息中保存附件

Aspose.Email API 提供了获取或设置指示是否对明文签名消息进行解码的值的功能。