Downloading Email Attachments Processing 3

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!

import java.io.File;
import java.io.IOException;
import java.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;

/**
  This program demonstrates how to download e-mail messages and save
 
 attachments into files on disk.
 
 
 @author www.codejava.net
 
 
/
private String saveDirectory;

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

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

  // server setting
¬†¬†properties.put(‚Äúmail.pop3.host‚ÄĚ, host);
¬†¬†properties.put(‚Äúmail.pop3.port‚ÄĚ, ‚Äú587‚ÄĚ);

  // SSL setting
¬†¬†properties.setProperty(‚Äúmail.pop3.socketFactory.class‚ÄĚ, ‚Äújavax.net.ssl.SSLSocketFactory‚ÄĚ);
¬†¬†properties.setProperty(‚Äúmail.pop3.socketFactory.fallback‚ÄĚ, ‚Äúfalse‚ÄĚ);
¬†¬†properties.setProperty(‚Äúmail.pop3.socketFactory.port‚ÄĚ,
    String.valueOf(port));

  Session session = Session.getDefaultInstance(properties);

  try {
    // connects to the message store
¬†¬†¬†¬†Store¬†store¬†=¬†session.getStore(‚Äúpop3‚ÄĚ);
    store.connect(userName, password);

    // opens the inbox folder
¬†¬†¬†¬†Folder¬†folderInbox¬†=¬†store.getFolder(‚ÄúINBOX‚ÄĚ);
    folderInbox.open(Folder.READ_ONLY);

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

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

      String contentType = message.getContentType();
¬†¬†¬†¬†¬†¬†String messageContent = ‚Äú‚ÄĚ;

      // store attachment file name, separated by comma
¬†¬†¬†¬†¬†¬†String attachFiles = ‚Äú‚ÄĚ;

¬†¬†¬†¬†¬†¬†if (contentType.contains(‚Äúmultipart‚ÄĚ)) {
        // content may contain attachments
        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())) {
            // this part is attachment
            String fileName = part.getFileName();
            attachFiles += fileName + ", ";
            part.saveFile(saveDirectory + File.separator + fileName);
          } else {
            // this part may be the message content
            messageContent = part.getContent().toString();
          }
        }

        if (attachFiles.length() > 1) {
          attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
        }
¬†¬†¬†¬†¬†¬†}¬†else if (contentType.contains(‚Äútext/plain‚ÄĚ)
¬†¬†¬†¬†¬†¬†¬†¬†||¬†contentType.contains(‚Äútext/html‚ÄĚ)) {
        Object content = message.getContent();
        if (content != null) {
          messageContent = content.toString();
        }
      }

      // print out details of each message
¬†¬†¬†¬†¬†¬†System.out.println(‚ÄúMessage #‚ÄĚ + (i + 1) + ‚Äú:‚ÄĚ);
      System.out.println("\t From: " + from);
      System.out.println("\t Subject: " + subject);
      System.out.println("\t Sent Date: " + sentDate);
      System.out.println("\t Message: " + messageContent);
      System.out.println("\t Attachments: " + attachFiles);
    }

    // disconnect
    folderInbox.close(false);
    store.close();
  } 
  catch (NoSuchProviderException ex) {
¬†¬†¬†¬†System.out.println(‚ÄúNo provider for pop3.‚ÄĚ);
    ex.printStackTrace();
  } 
  catch (MessagingException ex) {
¬†¬†¬†¬†System.out.println(‚ÄúCould not connect to the message store‚ÄĚ);
    ex.printStackTrace();
  } 
  catch (IOException ex) {
    ex.printStackTrace();
  }
}

/**
  Runs this program with Gmail pop3 server
 
/
void setup () {
¬†¬†String host = ‚Äúpop3.gmail.com‚ÄĚ;
¬†¬†String port = ‚Äú587‚ÄĚ;
¬†¬†String userName = ‚Äúrandom email‚ÄĚ;
¬†¬†String password = ‚Äúrandom password‚ÄĚ;
¬†¬†String saveDirectory = ‚ÄúC://‚ÄĚ;
  setSaveDirectory(saveDirectory);
  downloadEmailAttachments(host, port, userName, password);
}

Hi,

Welcome to the forum! :wink:

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 or Oracle community

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

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

Looks like it’s not a valid email and password :grinning:

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.

1 Like

Clever point here ahah, my bad! :wink: