# Downloading Email Attachments Processing 3

**URL:** <https://discourse.processing.org/t/downloading-email-attachments-processing-3/28475>\
**Category:** Coding Questions\
**Created:** [March 12, 2021, 7:15pm UTC](https://discourse.processing.org/t/downloading-email-attachments-processing-3/28475 "2021-03-12T19:15:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Flamingo](https://avatars.discourse-cdn.com/v4/letter/f/919ad9/32.png) [@Flamingo](https://discourse.processing.org/u/Flamingo)\
**Post date:** [March 12, 2021, 7:15pm UTC](https://discourse.processing.org/t/downloading-email-attachments-processing-3/28475/1 "2021-03-12T19:15:09Z")

</div>

Hi there. I'm new to the JavaMail library and I'm simply trying to download the attachments from my last received email to a folder. Every time I run the code I get an error regarding the host and connection. Something to do with the pop3... please help!

```
importjava.io.File;importjava.io.IOException;importjava.util.Properties;

import javax.mail.Address;  
import javax.mail.Folder;  
import javax.mail.Message;  
import javax.mail.MessagingException;  
import javax.mail.Multipart;  
import javax.mail.NoSuchProviderException;  
import javax.mail.Part;  
import javax.mail.Session;  
import javax.mail.Store;  
import javax.mail.internet.MimeBodyPart;

/\*\*  
&nbsp;_&nbsp;This&nbsp;program&nbsp;demonstrates&nbsp;how&nbsp;to&nbsp;download&nbsp;e-mail&nbsp;messages&nbsp;and&nbsp;save_  
 _&nbsp;_&nbsp;attachments&nbsp;into&nbsp;files&nbsp;on&nbsp;disk.  
&nbsp;  
 _&nbsp;_&nbsp;@author&nbsp;[www.codejava.net](http://www.codejava.net)  
&nbsp;  
 _&nbsp;_/  
private String saveDirectory;

/\*\*  
&nbsp;_&nbsp;Sets&nbsp;the&nbsp;directory&nbsp;where&nbsp;attached&nbsp;files&nbsp;will&nbsp;be&nbsp;stored._  
 _&nbsp;_&nbsp;@param&nbsp;dir&nbsp;absolute&nbsp;path&nbsp;of&nbsp;the&nbsp;directory  
&nbsp;\*/  
public void setSaveDirectory(String dir) {  
&nbsp;&nbsp;this.saveDirectory = dir;  
}

/\*\*  
&nbsp;_&nbsp;Downloads&nbsp;new&nbsp;messages&nbsp;and&nbsp;saves&nbsp;attachments&nbsp;to&nbsp;disk&nbsp;if&nbsp;any._  
 _&nbsp;_&nbsp;@param&nbsp;host  
&nbsp;_&nbsp;@param&nbsp;port_  
 _&nbsp;_&nbsp;@param&nbsp;userName  
&nbsp;_&nbsp;@param&nbsp;password_  
 _&nbsp;_/  
public void downloadEmailAttachments(String host, String port,  
&nbsp;&nbsp;String userName, String password) {  
&nbsp;&nbsp;Properties&nbsp;properties&nbsp;=&nbsp;new Properties();

&nbsp;&nbsp;// server setting  
&nbsp;&nbsp;properties.put(“mail.pop3.host”, host);  
&nbsp;&nbsp;properties.put(“mail.pop3.port”, “587”);

&nbsp;&nbsp;// SSL setting  
&nbsp;&nbsp;properties.setProperty(“mail.pop3.socketFactory.class”, “javax.net.ssl.SSLSocketFactory”);  
&nbsp;&nbsp;properties.setProperty(“mail.pop3.socketFactory.fallback”, “false”);  
&nbsp;&nbsp;properties.setProperty(“mail.pop3.socketFactory.port”,  
&nbsp;&nbsp;&nbsp;&nbsp;String.valueOf(port));

&nbsp;&nbsp;Session&nbsp;session&nbsp;=&nbsp;Session.getDefaultInstance(properties);

&nbsp;&nbsp;try {  
&nbsp;&nbsp;&nbsp;&nbsp;// connects to the message store  
&nbsp;&nbsp;&nbsp;&nbsp;Store&nbsp;store&nbsp;=&nbsp;session.getStore(“pop3”);  
&nbsp;&nbsp;&nbsp;&nbsp;store.connect(userName,&nbsp;password);

&nbsp;&nbsp;&nbsp;&nbsp;// opens the inbox folder  
&nbsp;&nbsp;&nbsp;&nbsp;Folder&nbsp;folderInbox&nbsp;=&nbsp;store.getFolder(“INBOX”);  
&nbsp;&nbsp;&nbsp;&nbsp;folderInbox.open(Folder.READ\_ONLY);

&nbsp;&nbsp;&nbsp;&nbsp;// fetches new messages from server  
&nbsp;&nbsp;&nbsp;&nbsp;Message[]&nbsp;arrayMessages&nbsp;=&nbsp;folderInbox.getMessages();

&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 0; i \< arrayMessages.length; i++) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Message&nbsp;message&nbsp;=&nbsp;arrayMessages[i];  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Address[]&nbsp;fromAddress&nbsp;=&nbsp;message.getFrom();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String from = fromAddress[0].toString();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String subject = message.getSubject();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String sentDate = message.getSentDate().toString();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String contentType = message.getContentType();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String messageContent = “”;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// store attachment file name, separated by comma  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String attachFiles = “”;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (contentType.contains(“multipart”)) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// content may contain attachments  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Multipart&nbsp;multiPart&nbsp;=&nbsp;(Multipart)&nbsp;message.getContent();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int numberOfParts = multiPart.getCount();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int partCount = 0; partCount \< numberOfParts; partCount++) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MimeBodyPart&nbsp;part&nbsp;=&nbsp;(MimeBodyPart)&nbsp;multiPart.getBodyPart(partCount);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// this part is attachment  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String fileName = part.getFileName();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attachFiles&nbsp;+=&nbsp;fileName&nbsp;+&nbsp;", ";  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;part.saveFile(saveDirectory + File.separator + fileName);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// this part may be the message content  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;messageContent&nbsp;=&nbsp;part.getContent().toString();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (attachFiles.length() \> 1) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attachFiles&nbsp;=&nbsp;attachFiles.substring(0, attachFiles.length() - 2);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else if (contentType.contains(“text/plain”)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;contentType.contains(“text/html”)) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object&nbsp;content&nbsp;=&nbsp;message.getContent();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (content != null) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;messageContent&nbsp;=&nbsp;content.toString();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// print out details of each message  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(“Message #” + (i + 1) + “:”);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("\t From: " + from);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("\t Subject: " + subject);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("\t Sent Date: " + sentDate);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("\t Message: " + messageContent);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("\t Attachments: " + attachFiles);  
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;// disconnect  
&nbsp;&nbsp;&nbsp;&nbsp;folderInbox.close(false);  
&nbsp;&nbsp;&nbsp;&nbsp;store.close();  
&nbsp;&nbsp;}&nbsp;  
&nbsp;&nbsp;catch (NoSuchProviderException ex) {  
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(“No provider for pop3.”);  
&nbsp;&nbsp;&nbsp;&nbsp;ex.printStackTrace();  
&nbsp;&nbsp;}&nbsp;  
&nbsp;&nbsp;catch (MessagingException ex) {  
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(“Could not connect to the message store”);  
&nbsp;&nbsp;&nbsp;&nbsp;ex.printStackTrace();  
&nbsp;&nbsp;}&nbsp;  
&nbsp;&nbsp;catch (IOException ex) {  
&nbsp;&nbsp;&nbsp;&nbsp;ex.printStackTrace();  
&nbsp;&nbsp;}  
}

/\*\*  
&nbsp;_&nbsp;Runs&nbsp;this&nbsp;program&nbsp;with&nbsp;Gmail&nbsp;pop3&nbsp;server_  
 _&nbsp;_/  
void **setup** () {  
&nbsp;&nbsp;String host = “[pop3.gmail.com](http://pop3.gmail.com)”;  
&nbsp;&nbsp;String port = “587”;  
&nbsp;&nbsp;String userName = “random email”;  
&nbsp;&nbsp;String password = “random password”;  
&nbsp;&nbsp;String saveDirectory = “C://”;  
&nbsp;&nbsp;setSaveDirectory(saveDirectory);  
&nbsp;&nbsp;downloadEmailAttachments(host,&nbsp;port,&nbsp;userName,&nbsp;password);  
}
```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [March 12, 2021, 11:33pm UTC](https://discourse.processing.org/t/downloading-email-attachments-processing-3/28475/2 "2021-03-12T23:33:01Z")

</div>

Hi,

Welcome to the forum! 😉

Note that this question is not specific to Processing but Java and the email library in general. Next time you should consider posting your issue on StackOverflow or specific forums like [Java forum](https://www.javaprogrammingforums.com/) or [Oracle community](https://community.oracle.com/tech/developers/categories/13287-java)

Just by looking at your code, how is it supposed to work if you have this? :

```auto
String userName = "random email";
String password = "random password";

```

Looks like it’s not a valid email and password 😀

---

<div class="post-metadata">

**Author:** ![Flamingo](https://avatars.discourse-cdn.com/v4/letter/f/919ad9/32.png) [@Flamingo](https://discourse.processing.org/u/Flamingo)\
**Post date:** [March 19, 2021, 9:47pm UTC](https://discourse.processing.org/t/downloading-email-attachments-processing-3/28475/3 "2021-03-19T21:47:43Z")

</div>

Lol thanks, I just kept the username and pass like that so I’m not doxing myself on a public forum. I figured it out anyway. I have to enable POP3/IMAP on gmail.

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [March 20, 2021, 12:03am UTC](https://discourse.processing.org/t/downloading-email-attachments-processing-3/28475/4 "2021-03-20T00:03:34Z")

</div>

Clever point here ahah, my bad! 😉
