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

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

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

为了在电子邮件正文中导入和发送Word文档,我们将利用Aspose.Words for JavaAspose.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 document
Document 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@email.com"));
message.getTo().add("your_to@email.com");
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.Email
SmtpClient client = new SmtpClient();
client.setHost("your_smtp.com");
client.send(message);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值