翻译|使用教程|编辑:李显亮|2020-05-21 10:34:45.673|阅读 898 次
概述:Aspose.Email for Java是功能强大的电子邮件处理API,它提供了电子邮件管理的所有基本和高级功能。在本文中,我将演示如何使用Java创建和发送Outlook电子邮件的一些基本但重要的功能。
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
Aspose.Email for Java是功能强大的电子邮件处理API,它提供了电子邮件管理的所有基本和高级功能。它可以从Java应用程序中创建Outlook电子邮件,阅读现有电子邮件,并将MSG,EML,PST,OST,MBOX和MHT消息转换为其他格式。
在本文中,我将演示如何使用Java创建和发送Outlook电子邮件的一些基本但重要的功能。在本文中将介绍以下创建和发送电子邮件的功能:
Aspose.Email for Java允许您创建电子邮件并将其保存为所需的电子邮件格式,包括EML,EMLX,MSG和MHTML。以下是创建电子邮件并将其保存为文件的步骤。
下面的代码示例演示如何使用Java创建Outlook电子邮件。
MailMessage message = new MailMessage(); // Set subject of the message, body and sender information message.setSubject("New message created by Aspose.Email for .NET"); message.setBody("This is the body of the email."); message.setFrom(new MailAddress("from@domain.com", "Sender Name", false)); // Add To recipients and CC recipients message.getTo().addItem(new MailAddress("to1@domain.com", "Recipient 1", false)); message.getCC().addItem(new MailAddress("cc1@domain.com", "Recipient 3", false)); // Add attachments message.getAttachments().addItem(new Attachment("word.docx")); // Save message in EML, EMLX, MSG and MHTML formats message.save("EmailMessage.eml", SaveOptions.getDefaultEml()); message.save("EmailMessage.emlx", SaveOptions.createSaveOptions(MailMessageSaveType.getEmlxFormat())); message.save("EmailMessage.msg", SaveOptions.getDefaultMsgUnicode()); message.save("EmailMessage.mhtml", SaveOptions.getDefaultMhtml());
在前面的示例中,我们仅创建了纯文本电子邮件并将其另存为电子邮件文件。但是,当今大多数电子邮件都是使用HTML正文创建的,可以很好地组织和呈现电子邮件的内容。在这种情况下,HTML标记用于指定电子邮件的布局。让我们看看如何使用Aspose.Email for Java使用HTML正文创建电子邮件。
以下代码示例显示了如何使用Java创建带有HTML正文的电子邮件。
MailMessage message = new MailMessage();
// Set subject of the message, body and sender information
message.setSubject("New message created by Aspose.Email for .NET");
message.setHtmlBody("This line is in bold. This line is in blue color");
message.setFrom(new MailAddress("from@domain.com", "Sender Name", false));
// Add To recipients and CC recipients
message.getTo().addItem(new MailAddress("to1@domain.com", "Recipient 1", false));
message.getCC().addItem(new MailAddress("cc1@domain.com", "Recipient 3", false));
// Save message in EML, EMLX, MSG and MHTML formats
message.save("EmailMessage.eml", SaveOptions.getDefaultEml());
Aspose.Email for Java还允许您设置电子邮件的所需文本编码。编码标准用于告知电子邮件应用程序或浏览器如何解释电子邮件正文中的字符。以下是创建具有特定编码标准的电子邮件的步骤。
以下代码示例显示了如何使用Java创建具有特定编码标准的电子邮件。
MailMessage message = new MailMessage();
// Set subject of the message, body and sender information
message.setSubject("New message created by Aspose.Email for .NET");
message.setHtmlBody("This line is in bold. This line is in blue color");
message.setFrom(new MailAddress("from@domain.com", "Sender Name", false));
// Specify encoding
message.setBodyEncoding(Charset.forName("US-ASCII"));
// Add To recipients and CC recipients
message.getTo().addItem(new MailAddress("to1@domain.com", "Recipient 1", false));
message.getCC().addItem(new MailAddress("cc1@domain.com", "Recipient 3", false));
// Save message in EML, EMLX, MSG and MHTML formats
message.save("EmailMessage.eml", SaveOptions.getDefaultEml());
创建电子邮件后,可以使用简单邮件传输协议(SMTP)将其发送给收件人。以下是使用Aspose.Email for Java发送电子邮件的步骤。
以下代码示例显示了如何通过Java中的SMTP客户端发送电子邮件。
MailMessage message = new MailMessage();
// Set subject of the message, body and sender information
message.setSubject("New message created by Aspose.Email for .NET");
message.setHtmlBody("This line is in bold. This line is in blue color");
message.setFrom(new MailAddress("from@domain.com", "Sender Name", false));
// Specify encoding
message.setBodyEncoding(Charset.forName("US-ASCII"));
// Add To recipients and CC recipients
message.getTo().addItem(new MailAddress("to1@domain.com", "Recipient 1", false));
message.getCC().addItem(new MailAddress("cc1@domain.com", "Recipient 3", false));
// Create an instance of SmtpClient Class
SmtpClient client = new SmtpClient();
// Specify your mailing host server, Username, Password, Port
client.setHost("smtp.server.com");
client.setUsername("Username");
client.setPassword("Password");
client.setPort(25);
try
{
// Client.Send will send this message
client.send(message);
}
catch (Exception ex)
{
ex.printStackTrace();
}
Aspose.Email for Java还允许您异步发送电子邮件。为此,可以使用SmtpClient.beginSend()方法。以下代码示例显示了如何使用Java异步发送电子邮件。
MailMessage message = new MailMessage();
// Set subject of the message, body and sender information
message.setSubject("New message created by Aspose.Email for .NET");
message.setHtmlBody("This line is in bold. This line is in blue color");
message.setFrom(new MailAddress("from@domain.com", "Sender Name", false));
// Specify encoding
message.setBodyEncoding(Charset.forName("US-ASCII"));
// Add To recipients and CC recipients
message.getTo().addItem(new MailAddress("to1@domain.com", "Recipient 1", false));
message.getCC().addItem(new MailAddress("cc1@domain.com", "Recipient 3", false));
// Create an instance of SmtpClient Class
SmtpClient client = new SmtpClient();
// Specify your mailing host server, Username, Password, Port
client.setHost("smtp.server.com");
client.setUsername("Username");
client.setPassword("Password");
client.setPort(25);
try
{
// Client.beginSend will send this message
client.beginSend(message);
}
catch (Exception ex)
{
ex.printStackTrace();
}
可以向电子邮件中添加已读回执的请求。此功能使您可以在收到电子邮件后接收通知。以下是创建和发送包含已读回执请求的电子邮件的步骤。
以下代码示例显示了如何使用Java发送包含已读回执的电子邮件。
MailMessage message = new MailMessage();
// Set subject of the message, body and sender information
message.setSubject("New message created by Aspose.Email for .NET");
message.setHtmlBody("This line is in bold. This line is in blue color");
message.setFrom(new MailAddress("from@domain.com", "Sender Name", false));
// Set notification options
message.setDeliveryNotificationOptions(DeliveryNotificationOptions.OnSuccess);
message.getHeaders().add("Return-Receipt-To", "from@domain.com");
message.getHeaders().add("Disposition-Notification-To", "from@domain.com");
// Add To recipients
message.getTo().addItem(new MailAddress("to1@domain.com", "Recipient 1", false));
// Create an instance of SmtpClient Class
SmtpClient client = new SmtpClient();
// Specify your mailing host server, Username, Password, Port
client.setHost("smtp.server.com");
client.setUsername("Username");
client.setPassword("Password");
client.setPort(25);
try
{
// Client.Send will send this message
client.send(message);
}
catch (Exception ex)
{
ex.printStackTrace();
}
在某些情况下,您需要发送大量不同的电子邮件。在这种情况下,您只需将所有电子邮件添加到MailMessageCollection对象,然后将其传递到SMTP客户端进行发送。以下代码示例显示了如何使用Java发送大量电子邮件。
// Create instances of MailMessage class and specify To, From, Subject and Message MailMessage message1 = new MailMessage("msg1@from.com", "msg1@to.com", "Subject1", "message1, how are you?"); MailMessage message2 = new MailMessage("msg1@from.com", "msg2@to.com", "Subject2", "message2, how are you?"); MailMessage message3 = new MailMessage("msg1@from.com", "msg3@to.com", "Subject3", "message3, how are you?"); // Create an instance of MailMessageCollection and add email messages to it MailMessageCollection manyMsg = new MailMessageCollection(); manyMsg.addItem(message1); manyMsg.addItem(message2); manyMsg.addItem(message3); // Create SmtpClient and specify server, port, user name and password SmtpClient client = new SmtpClient("mail.server.com", 25, "Username", "Password"); try { // Send messages client.send(manyMsg); } catch (Exception ex) { ex.printStackTrace(); }
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@cahobeh.cn