300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Word处理控件Aspose.Words功能演示:使用 C# 在电子邮件正文中发送 Word 文档

Word处理控件Aspose.Words功能演示:使用 C# 在电子邮件正文中发送 Word 文档

时间:2024-04-26 09:09:16

相关推荐

Word处理控件Aspose.Words功能演示:使用 C# 在电子邮件正文中发送 Word 文档

Aspose.Words是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

Aspose.words 最新下载(qun:761297826)/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.WordsPM> Install-Package Aspose.Email

二、使用 C# 在电子邮件正文中发送 Word 文档

1.使用Aspose.Words.Document类加载 Word 文档并将其作为 MHTML 保存到MemoryStream对象中。

// Load a Word document from diskDocument wordDocument = new Document("Word.docx");// Save document as MHTML into memory streamMemoryStream mhtmlStream = new MemoryStream();wordDocument.Save(mhtmlStream, SaveFormat.Mhtml);

2.将 MHTML 从MemoryStream对象加载到Aspose.Email.MailMessage对象,并设置电子邮件的主题、收件人和发件人字段。

// Set position to 0mhtmlStream.Position = 0;// Create email message from MHTMLMailMessage message = MailMessage.Load(mhtmlStream, new MhtmlLoadOptions());// Set email fieldsmessage.Subject = "Sending Invoice in Email";message.From = "sender@";message.To = "recipient@";3.使用Aspose.Email.Clients.Smtp.SmtpClient类设置 SMTP 客户端并发送邮件。// Send email via SMTPSmtpClient client = new SmtpClient("", 587, "sender@", "pwd");client.SecurityOptions = SecurityOptions.SSLExplicit;client.Send(message);

源代码

以下是使用 C# 将 MS Word 文档导入为电子邮件正文的完整源代码。

// Load a Word document from diskDocument wordDocument = new Document("Word.docx");// Save document as MHTML into memory streamMemoryStream mhtmlStream = new MemoryStream();wordDocument.Save(mhtmlStream, SaveFormat.Mhtml);// Set position to 0mhtmlStream.Position = 0;// Create email message from MHTMLMailMessage message = MailMessage.Load(mhtmlStream, new MhtmlLoadOptions());// Set email fieldsmessage.Subject = "Sending Invoice in Email";message.From = "sender@";message.To = "recipient@";// Send email via SMTPSmtpClient client = new SmtpClient("", 587, "sender@", "pwd");client.SecurityOptions = SecurityOptions.SSLExplicit;client.Send(message);

以上便是使用 C# 在电子邮件正文中发送 Word 文档详细步骤 ,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。