创建和保存 Outlook 文件

Aspose.Email 支持创建 Outlook 消息(MSG)文件。本文章解释了如何:

创建并保存 Outlook 消息

MailMessage 类具有 Save() 可以将 Outlook MSG 文件保存到磁盘或流的 method。下面的代码片段创建了该类的实例。 MailMessage 类,设置如 from、to、subject 和 body 等属性。该 Save() 方法将文件名作为参数。此外,还可以使用 … 创建 Outlook 消息。 压缩的 RTF 正文 使用 MapiConversionOptions

  1. 创建该类的新实例 MailMessage 类,并设置 From、To、Subject 和 Body 属性。
  2. 调用 MapiMessage 类 FromMailMessage 方法接受 … 类的对象 MailMessage 类型。该 FromMailMessage 方法将 … 转换为 MailMessage 转换为 a MapiMessage (MSG)。
  3. 调用 MapiMessage.Save() 方法来保存 MSG 文件。

在 Windows 应用程序的按钮控件的 Click 事件中编写以下代码。

// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();

// Set from, to, subject and body properties
mailMsg.From = "sender@domain.com";
mailMsg.To = "receiver@domain.com";
mailMsg.Subject = "This is test message";
mailMsg.Body = "This is test body";

// Create an instance of the MapiMessage class and pass MailMessage as argument
MapiMessage outlookMsg = MapiMessage.FromMailMessage(mailMsg);
            
// Save the message (MSG) file
string strMsgFile = @"CreatingAndSavingOutlookMessages_out.msg";
outlookMsg.Save(dataDir + strMsgFile);

创建带附件的 MSG 文件

在上面的示例中, 我们创建了一个简单的 MSG 文件。Aspose.Email 也支持保存带附件的消息文件。您所需做的只是将附件添加到 … MailMessage 实例。通过调用 Add() 方法在 MailMessage.Attachments 集合。在上面创建的表单中添加一个列表框并添加两个按钮,分别用于添加和删除附件。添加附件的应用程序工作方式如下:

  1. 点击 Add Attachment 按钮时,会弹出 Open File Dialog,帮助用户浏览并选择附件。
  2. 当选择文件后,会将完整路径添加到列表中。
  3. 创建 MSG 文件时,附件路径会从列表中获取并添加到 MailMessage.Attachments 集合。

Add Attachment 按钮的点击事件中编写以下代码。

// Open a file open dialog to select the attachment
OpenFileDialog fd = new OpenFileDialog();
// If user selected the file and clicked OK, add the file name and path to the list
if (fd.ShowDialog() == DialogResult.OK)
{
    lstAttachments.Items.Add(fd.FileName);
}

单击 Remove Attachment 按钮时,从列表框中移除选中的项目。 在 Remove Attachment 按钮的点击事件中编写以下代码。

// Check if the user has selected any item in the attachments listbox
if (lstAttachments.SelectedItems.Count > 0)
{
    // Remove the selected attachment from the listbox
    lstAttachments.Items.RemoveAt(lstAttachments.SelectedIndex);
}

添加用于添加附件的代码到 MailMessage 实例。Write Msg 函数的最终代码如下所示。

// File name for output MSG file
string strMsgFile;

// Open a save file dialog for saving the file and Add filter for msg files
SaveFileDialog fd = new SaveFileDialog();
fd.Filter = "Outlook Message files (*.msg)|*.msg|All files (*.*)|*.*";

// If user pressed OK, save the file name + path
if (fd.ShowDialog() == DialogResult.OK)
{
    strMsgFile = fd.FileName;
}
else
{
    // If user did not selected the file, return
    return;
}

// Create an instance of MailMessage class
MailMessage mailMsg = new MailMessage();
// Set from, to, subject and body properties
mailMsg.From = txtFrom.Text;
mailMsg.To = txtTo.Text;
mailMsg.Subject = txtSubject.Text;
mailMsg.Body = txtBody.Text;

// Add the attachments
foreach (string strFileName in lstAttachments.Items)
{
    mailMsg.Attachments.Add(new Attachment(strFileName));
}

// Create an instance of MapiMessage class and pass MailMessage as argument
MapiMessage outlookMsg = MapiMessage.FromMailMessage(mailMsg);
outlookMsg.Save(strMsgFile);

创建带 RTF 正文的 MSG 文件

您还可以使用 Aspose.Email 创建带有富文本(RTF)正文的 Outlook 消息(MSG)文件。RTF 正文支持文本格式化。通过设置 … 来创建。 MailMessage.HtmlBody 属性。当您转换 a MailMessage 实例转换为 a MapiMessage 实例中,HTML 正文被转换为 RTF。这样,电子邮件正文的格式得以保留。

下面的示例创建了一个带有 RTF 正文的 MSG 文件。HTML 正文中包含一个标题、加粗和下划线格式。转换为 RTF 时,这些格式会被保留。

// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();
            
// Set from, to, subject and body properties
mailMsg.From = "from@domain.com";
mailMsg.To = "to@domain.com";
mailMsg.Subject = "subject";
mailMsg.HtmlBody = "<h3>rtf example</h3><p>creating an <b><u>outlook message (msg)</u></b> file using Aspose.Email.</p>";
            
MapiMessage outlookMsg = MapiMessage.FromMailMessage(mailMsg);
outlookMsg.Save(dataDir + "CreatingMSGFilesWithRTFBody_out.msg");

MAPI 消息正文的 RTF 压缩

注意: 压缩过程在创建消息时可能会降低性能。了解这一点并根据具体需求以及文件大小与性能之间的权衡来配置压缩标志,开发人员可以在处理电子邮件时有效管理 MSG 和 PST 文件的创建。

RTF 压缩旨在减小消息的体积以及 Microsoft Outlook 用于存储电子邮件和其他数据的 PST(个人存储表)文件的大小。通过在配置消息正文时使用 RTF 压缩,开发人员可以降低存储电子邮件所需的内存量,或在传输消息时优化网络带宽。

为此,已设计了两个重载方法:

  • MapiMessageItemBase.SetBodyContent(string content, BodyContentType contentType, bool compression):此方法允许您使用指定的字符串内容并指定正文 contentType(例如,纯文本、HTML 等)来设置消息正文内容。可选的 compression 参数是一个值,指示是否应使用 RTF 压缩对内容进行压缩。如果 compression 参数为 true,内容将被压缩,从而减小消息大小。

  • MapiMessageItemBase.SetBodyRtf(string content, bool compression):此方法专门设置以 RTF 格式的消息正文内容。content 参数是表示将设置为消息正文的 RTF 内容的字符串。与前一个方法类似,compression 参数决定是否对内容应用 RTF 压缩。若 compression 为 true,则 RTF 内容将被压缩以减小大小。

以下代码示例展示了如何设置 HTML 正文并保持压缩:

var htmlBody = "<html><body><h1>This is the body content</h1></body></html>";

var msg = new MapiMessage("from@domain.com", "to@domain.com", "subject", "body");
// set the html body and keep it compressed
// this will reduce the message size
msg.SetBodyContent(htmlBody, BodyContentType.Html, true);

还有一个 MapiConversionOptions.UseBodyCompression 属性。启用此属性后,在 MailMessage 转换为 MapiMessage 时会应用 RTF 正文压缩,从而减小 MSG 文件大小。以下代码示例展示了该用法:

var message = MailMessage.Load(fileName);
var options = new MapiConversionOptions();
options.UseBodyCompression = true;
var msg = MapiMessage.FromMailMessage(message, options);

以草稿状态保存消息

当用户已开始编辑邮件但想稍后返回完成时,邮件会以草稿形式保存。Aspose.Email 支持通过设置消息标志将电子邮件保存为草稿状态。以下示例代码演示了如何将 Outlook 邮件(MSG)保存为草稿。

// Change properties of an existing MSG file
string strExistingMsg = @"message.msg";
            
// Load the existing file in MailMessage and Change the properties
MailMessage msg = MailMessage.Load(dataDir + strExistingMsg, new MsgLoadOptions());
msg.Subject += "NEW SUBJECT (updated by Aspose.Email)";
msg.HtmlBody += "NEW BODY (updated by Aspose.Email)";
            
// Create an instance of type MapiMessage from MailMessage, Set message flag to un-sent (draft status) and Save it
MapiMessage mapiMsg = MapiMessage.FromMailMessage(msg);
mapiMsg.SetMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT);
mapiMsg.Save(dataDir + "SavingMessageInDraftStatus_out.msg");