300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Word处理控件Aspose.Words功能演示:在Java中将Word文档以邮件形式发送

Word处理控件Aspose.Words功能演示:在Java中将Word文档以邮件形式发送

时间:2020-08-08 19:58:37

相关推荐

Word处理控件Aspose.Words功能演示:在Java中将Word文档以邮件形式发送

在大多数情况下,电子邮件按照特定模板以格式正确的布局发送。但是,各种电子邮件编辑器不提供增强的格式选项。在这种情况下,可以在Word文档中创建一条消息,并将其用作电子邮件正文。在本文中,将学习如何使用Java将MS Word文档作为电子邮件正文发送。

在电子邮件正文中发送Word文档

为了在电子邮件正文中导入和发送Word文档,我们将利用Aspose.Words for Java和Aspose.Email for Java前者将用于以MHTML格式保存Word文档,而后者将用于创建和发送电子邮件。如果想要测试这项新功能,可下载Aspose.Words for JavaAspose.Email for Java最新版试用。

使用Java在电子邮件正文中发送Word文档

以下是在电子邮件正文中导入和发送Word文档的步骤。

使用com.aspose.words.Document类加载Word文档。创建一个ByteArrayOutputStream类的实例。将Word文档作为MHTML保存在ByteArrayOutputStream对象中。在ByteArrayInputStream对象中加载MHTML。创建com.aspose.email.MailMessage类的实例并加载保存在ByteArrayInputStream对象中的MHTML。设置电子邮件的字段,如收件人、发件人、主题等。创建com.aspose.email.SmtpClient类的一个实例设置主机并使用SmtpClient.send(MailMessage)方法发送邮件。

下面的代码示例演示如何将Word文档作为电子邮件正文发送。

// Load the documentDocument doc = new Document("Document.doc");// Save to an output stream in MHTML format.ByteArrayOutputStream outputStream = new ByteArrayOutputStream();doc.save(outputStream, SaveFormat.MHTML);// Load the MHTML stream back into an input stream to use with Aspose.Email.ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());// Create an Aspose.Email MIME email message from the stream.MailMessage message = MailMessage.load(inputStream);message.setFrom(new MailAddress("your_from@"));message.getTo().add("your_to@");message.setSubject("Aspose.Words + Aspose.Email MHTML Test Message");// Save the message in Outlook MSG format.message.save("Message.msg", SaveOptions.getDefaultMsg());// Send the message using Aspose.EmailSmtpClient client = new SmtpClient();client.setHost("");client.send(message);

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