一、下载将 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@gmail.com";message.To = "recipient@gmail.com";3.使用Aspose.Email.Clients.Smtp.SmtpClient类设置 SMTP 客户端并发送邮件。// Send email via SMTPSmtpClient 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 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@gmail.com";message.To = "recipient@gmail.com";// Send email via SMTPSmtpClient client = new SmtpClient("smtp.gmail.com", 587, "sender@gmail.com", "pwd");client.SecurityOptions = SecurityOptions.SSLExplicit;client.Send(message);
以上便是使用 C# 在电子邮件正文中发送 Word 文档详细步骤,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。
欢迎下载|体验更多Aspose产品
点此获取更多Aspose产品信息 或 加入Aspose技术交流群()
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!