Downloading Email Attachments in Java – 在Java中下载电子邮件附件

最后修改: 2021年 5月 28日

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

1. Overview

1.概述

In this tutorial, we take a look at how we can download email attachments using Java. For doing so, we need the JavaMail API. The JavaMail API is available as either a Maven dependency or as separate jars.

在本教程中,我们将看看如何使用Java下载电子邮件附件。要做到这一点,我们需要JavaMail API。JavaMail API既可以作为Maven的依赖项,也可以作为独立的jars提供。

2. JavaMail API Overview

2.JavaMail API概述

The JavaMail API is used to compose, send, and receive emails from an email server like Gmail. It provides a framework for an email system using abstract classes and interfaces. The API supports most RFC822 and MIME Internet messaging protocols like SMTP, POP, IMAP, MIME, and NNTP.

JavaMail API用于编写、发送和接收来自Gmail等电子邮件服务器的邮件。它使用抽象类和接口为电子邮件系统提供了一个框架。该API支持大多数RFC822和MIME互联网消息传输协议,如SMTP、POP、IMAP、MIME和NNTP。

3. JavaMail API Setup

3.JavaMail API设置

We need to add the javax.mail Maven dependency in our Java project to use the JavaMail API:

我们需要在Java项目中添加 javax.mail Maven依赖项,以使用JavaMail API。

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId> 
    <version>1.6.2</version>
</dependency>

4. Download Email Attachments

4.下载电子邮件附件

For handling email in Java, we use the Message class from the javax.mail package. Message implements the javax.mail.Part interface.

为了在Java中处理电子邮件,我们使用Message类,它来自javax.mail包。Message实现了javax.mail.Part接口。

The Part interface has BodyPart and attributes. The content with attachments is a BodyPart called MultiPart. If an email has any attachments, it has a disposition equal to “Part.ATTACHMENT“. In case there are no attachments, the disposition is null. The getDisposition method from the Part interface gets us the disposition.

Part接口有BodyPart和属性。带有附件的内容是一个名为MultiPartBodyPart如果一个电子邮件有任何附件,它的处理方式等于”Part.ATTACHMENT“。如果没有附件,则处理方式为null。来自Part接口的getDisposition方法可以得到处置。

We look at a simple Maven-based project to understand how downloading email attachments work. We’ll concentrate on getting the emails to download and saving attachments to the disk.

我们看一个简单的基于Maven的项目,了解下载电子邮件附件的工作原理。我们将专注于让邮件下载并将附件保存到磁盘。

Our project has a utility that deals with downloading emails and saving them to our disk. We’re also displaying the list of attachments.

我们的项目有一个处理下载电子邮件并将其保存到我们的磁盘的工具。我们也在显示附件的列表。

To download the attachment(s), we first check if the content type has multipart content or not. If yes, we can process it further to check if the part has any attachments. To check the content type, we write:

为了下载附件,我们首先检查内容类型是否有多部分内容。如果有,我们可以进一步处理,检查该部分是否有任何附件。为了检查内容类型,我们写道。

if (contentType.contains("multipart")) {
    //send to the download utility...
}

If we have a multipart, we first check if it is of the type Part.ATTACHMENT and, if it is, we save the file to our destination folder using the saveFile method. So, in the download utility, we would check:

如果我们有一个多部件,我们首先检查它是否属于Part.ATTACHMENT类型,如果是,我们使用saveFile方法将文件保存到目标文件夹。因此,在下载工具中,我们会检查。

if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
    String file = part.getFileName();
    part.saveFile(downloadDirectory + File.separator + part.getFileName());
    downloadedAttachments.add(file);
}

Since we’re using the JavaMail API version greater than 1.4, we can use the saveFile method from the Part interface. The saveFile method works with either a File object or a String. We have used a string in the example. This step saves the attachments to the folder we specify. We also maintain a list of attachments for the display.

由于我们使用的JavaMail API版本大于1.4,我们可以使用Part接口中的saveFile方法。saveFile方法适用于File对象或String。在本例中我们使用了字符串。这一步将附件保存到我们指定的文件夹中。我们还维护了一个附件的列表,以便显示。

Before the JavaMail API version 1.4, we had to write the entire file byte by byte using FileStream and InputStream. In our example, we’ve used a Pop3 server for a Gmail account. So, to call the method in the example, we need a valid Gmail username and password and a folder to download attachments.

在JavaMail API 1.4版本之前,我们必须使用FileStreamInputStream逐字逐句地写入整个文件。在我们的例子中,我们使用了一个Gmail账户的Pop3服务器。所以,要调用例子中的方法,我们需要一个有效的Gmail用户名和密码,以及一个下载附件的文件夹。

Let’s see the example code for downloading attachments and saving them to disk:

让我们看看下载附件并将其保存到磁盘的示例代码。

public List<String> downloadAttachments(Message message) throws IOException, MessagingException {
    List<String> downloadedAttachments = new ArrayList<String>();
    Multipart multiPart = (Multipart) message.getContent();
    int numberOfParts = multiPart.getCount();
    for (int partCount = 0; partCount < numberOfParts; partCount++) {
        MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
        if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
            String file = part.getFileName();
            part.saveFile(downloadDirectory + File.separator + part.getFileName());
            downloadedAttachments.add(file);
        }
    }
    return downloadedAttachments;
}  

5. Conclusion

5.总结

This article showed how to download emails in Java using the native JavaMail library to download email attachments. The entire code for this tutorial is available over on over on GitHub.

本文展示了如何使用本地JavaMail库在Java中下载电子邮件,以下载电子邮件附件。本教程的全部代码可在GitHub上找到