Aspose.Words 是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
Aspose.words 最新下载(qun:761297826)https://www.evget.com/product/4116/download
电子邮件正文的呈现是吸引读者的重要因素之一。因此,电子邮件可以使用标题、副标题、表格、图像等进行良好格式化。但是,大多数内置电子邮件编辑器不提供高级格式设置选项。为了解决这个限制,本文介绍了如何在C#中使用 Word 文档作为电子邮件正文来撰写电子邮件。
一、下载将 Word 文档导入电子邮件的 C# API
为了从 Word 文档导入内容,我们将使用Aspose.Words for .NET API。然而,为了撰写和发送电子邮件,我们将利用Aspose.Email for .NET的功能。上述两个 API 都可以作为 DLL 下载或通过 NuGet 安装。
PM> Install-Package Aspose.Words PM> Install-Package Aspose.Email
二、使用 C# 在电子邮件正文中发送 Word 文档
1.使用Aspose.Words.Document类加载 Word 文档并将其作为 MHTML 保存到MemoryStream对象中。
// Load a Word document from disk Document wordDocument = new Document("Word.docx"); // Save document as MHTML into memory stream MemoryStream mhtmlStream = new MemoryStream(); wordDocument.Save(mhtmlStream, SaveFormat.Mhtml);
2.将 MHTML 从MemoryStream对象加载到Aspose.Email.MailMessage对象,并设置电子邮件的主题、收件人和发件人字段。
// Set position to 0 mhtmlStream.Position = 0; // Create email message from MHTML MailMessage message = MailMessage.Load(mhtmlStream, new MhtmlLoadOptions()); // Set email fields message.Subject = "Sending Invoice in Email"; message.From = "sender@gmail.com"; message.To = "recipient@gmail.com"; 3.使用Aspose.Email.Clients.Smtp.SmtpClient类设置 SMTP 客户端并发送邮件。 // Send email via SMTP SmtpClient client = new SmtpClient("smtp.gmail.com", 587, "sender@gmail.com", "pwd"); client.SecurityOptions = SecurityOptions.SSLExplicit; client.Send(message);
源代码
以下是使用 C# 将 MS Word 文档导入为电子邮件正文的完整源代码。
// Load a Word document from disk Document wordDocument = new Document("Word.docx"); // Save document as MHTML into memory stream MemoryStream mhtmlStream = new MemoryStream(); wordDocument.Save(mhtmlStream, SaveFormat.Mhtml); // Set position to 0 mhtmlStream.Position = 0; // Create email message from MHTML MailMessage message = MailMessage.Load(mhtmlStream, new MhtmlLoadOptions()); // Set email fields message.Subject = "Sending Invoice in Email"; message.From = "sender@gmail.com"; message.To = "recipient@gmail.com"; // Send email via SMTP SmtpClient client = new SmtpClient("smtp.gmail.com", 587, "sender@gmail.com", "pwd"); client.SecurityOptions = SecurityOptions.SSLExplicit; client.Send(message);
以上便是使用 C# 在电子邮件正文中发送 Word 文档详细步骤 ,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。