Sending Emails with Attachments in Java – 在Java中发送带附件的电子邮件

最后修改: 2020年 9月 19日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll learn how to send emails with single and multiple attachments in Java using JavaMail API.

在这个快速教程中,我们将学习如何使用JavaMail API在Java中发送带有单个和多个附件的电子邮件。

2. Project Setup

2.项目设置

In this article, we’re creating a simple Maven project with javax.mail dependency:

在本文中,我们将创建一个简单的Maven项目,其中包含javax.mail依赖项。

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.5.0-b01</version>
</dependency>

3. Sending Mail With Attachments

3.发送带附件的邮件

First, we need to configure the email service provider’s credentials. Then, the Session object is created by providing the email host, port, username, and password. All these details are provided by the email host service. We can use any fake SMTP testing servers for our code.

首先,我们需要配置电子邮件服务提供商的凭证。然后,通过提供电子邮件主机、端口、用户名和密码来创建Session对象。所有这些细节都是由电子邮件主机服务提供的。我们可以为我们的代码使用任何假的SMTP测试服务器。

Session object will work as a connection factory to handle the configuration and authentication for JavaMail.

Session对象将作为一个连接工厂来处理JavaMail的配置和认证。

Now that we have a Session object, let’s move further and create MimeMessage and MimeBodyPart object. We use these objects to create the email message:

现在我们有了一个Session对象,让我们进一步创建MimeMessageMimeBodyPart对象。我们使用这些对象来创建电子邮件信息。

Message message = new MimeMessage(session); 
message.setFrom(new InternetAddress(from)); 
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); 
message.setSubject("Test Mail Subject"); 

BodyPart messageBodyPart = new MimeBodyPart(); 
messageBodyPart.setText("Mail Body");

In the above snippet, we’ve created the MimeMessage object with required details such as from, to, and subject. Then, we’ve got a MimeBodyPart object with the email body.

在上面的片段中,我们已经创建了MimeMessage对象,其中包括所需的细节,如发件人、收件人和主题。然后,我们有一个包含电子邮件正文的MimeBodyPart对象。

Now, we need to create another MimeBodyPart to add an attachment in our mail:

现在,我们需要创建另一个MimeBodyPart来在我们的邮件中添加一个附件。

MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.attachFile(new File("path/to/file"));

We’ve now two MimeBodyPart objects for one mail Session. So we need to create one MimeMultipart object and then add both the MimeBodyPart objects into it:

我们现在有两个MimeBodyPart对象用于一个邮件会话。所以我们需要创建一个MimeMultipart对象,然后将两个MimeBodyPart对象都添加到其中。

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(attachmentPart);

Finally, the MimeMultiPart is added to the MimeMessage object as our mail content and the Transport.send() method is invoked to send the message:

最后,MimeMultiPart被添加到MimeMessage对象中作为我们的邮件内容,Transport.send()方法被调用以发送该邮件。

message.setContent(multipart);
Transport.send(message);

To summarize, the Message contains MimeMultiPart which further contains multiple MimeBodyPart(s). That’s how we assemble the complete email.

综上所述,Message包含MimeMultiPart,它进一步包含多个MimeBodyPart(s)。这就是我们如何组装完整的电子邮件。

Moreover, to send multiple attachments you can simply add another MimeBodyPart.

此外,要发送多个附件,您只需再添加一个MimeBodyPart

4. Conclusion

4.总结

In this tutorial, we’ve learned how to send emails with single and multiple attachments in Java.

在本教程中,我们已经学会了如何用Java发送带有单个和多个附件的电子邮件。

As always, the complete source code is available over on GitHub.

一如既往,完整的源代码可在GitHub上获得