diff --git a/core-java-modules/core-java-networking-3/pom.xml b/core-java-modules/core-java-networking-3/pom.xml index 35e88b3b92..0ad800e173 100644 --- a/core-java-modules/core-java-networking-3/pom.xml +++ b/core-java-modules/core-java-networking-3/pom.xml @@ -36,16 +36,29 @@ ${assertj.version} test + + junit + junit + 4.11 + test + + + com.sun.mail + javax.mail + 1.6.2 + core-java-networking-3 - - - org.apache.maven.plugins - maven-compiler-plugin - - + + + + org.apache.maven.plugins + maven-compiler-plugin + + + diff --git a/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/downloadattachments/DownloadEmailAttachments.java b/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/downloadattachments/DownloadEmailAttachments.java new file mode 100644 index 0000000000..4030f3b983 --- /dev/null +++ b/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/downloadattachments/DownloadEmailAttachments.java @@ -0,0 +1,112 @@ +package com.baeldung.downloadattachments; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +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; + +public class DownloadEmailAttachments { + private String downloadDirectory; + + public void setSaveDirectory(String dir) { + this.downloadDirectory = dir; + } + + public void downloadEmailAttachments(String host, String port, String userName, String password) throws NoSuchProviderException, MessagingException, IOException { + Properties properties = setMailServerProperties(host, port); + Store store = setSessionStoreProperties(userName, password, properties); + Folder inbox = store.getFolder("INBOX"); + inbox.open(Folder.READ_ONLY); + Message[] arrayMessages = inbox.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(); + List attachments = new ArrayList(); + if (message.getContentType().contains("multipart")) { + attachments = downloadAttachments(message); + } + + System.out.println("Message #" + (i + 1) + ":"); + System.out.println(" From: " + from); + System.out.println(" Subject: " + subject); + System.out.println(" Sent Date: " + sentDate); + System.out.println(" Attachments: " + attachments); + } + inbox.close(false); + store.close(); + } + + public List downloadAttachments(Message message) throws IOException, MessagingException { + List downloadedAttachments = new ArrayList(); + 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; + } + + public Store setSessionStoreProperties(String userName, String password, Properties properties) throws NoSuchProviderException, MessagingException { + Session session = Session.getDefaultInstance(properties); + + Store store = session.getStore("pop3"); + store.connect(userName, password); + return store; + } + + public Properties setMailServerProperties(String host, String port) { + Properties properties = new Properties(); + + properties.put("mail.pop3.host", host); + properties.put("mail.pop3.port", port); + + 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)); + return properties; + } + + public static void main(String[] args) { + String host = "pop.gmail.com"; + String port = "995"; + String userName = "your_email"; + String password = "your_password"; + + String saveDirectory = "valid_folder_path"; + + DownloadEmailAttachments receiver = new DownloadEmailAttachments(); + receiver.setSaveDirectory(saveDirectory); + try { + receiver.downloadEmailAttachments(host, port, userName, password); + } 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(); + } + } +} diff --git a/core-java-modules/core-java-networking-3/src/test/java/com/baeldung/downloadattachments/DownloadEmailAttachmentsLiveTest.java b/core-java-modules/core-java-networking-3/src/test/java/com/baeldung/downloadattachments/DownloadEmailAttachmentsLiveTest.java new file mode 100644 index 0000000000..050790e6be --- /dev/null +++ b/core-java-modules/core-java-networking-3/src/test/java/com/baeldung/downloadattachments/DownloadEmailAttachmentsLiveTest.java @@ -0,0 +1,25 @@ +package com.baeldung.downloadattachments; + +import static org.junit.Assert.fail; +import org.junit.Test; + +public class DownloadEmailAttachmentsLiveTest { + @Test + public void when_Run_then_downloadAttachments() { + + String host = "pop.gmail.com"; + String port = "995"; + String userName = "your_email"; + String password = "your_password"; + + String saveDirectory = "valid_folder_path"; + + DownloadEmailAttachments receiver = new DownloadEmailAttachments(); + receiver.setSaveDirectory(saveDirectory); + try { + receiver.downloadEmailAttachments(host, port, userName, password); + } catch (Exception ex) { + fail("Exception: " + ex); + } + } +}