diff --git a/java-download-email/pom.xml b/java-download-email/pom.xml new file mode 100644 index 0000000000..7885fd9a48 --- /dev/null +++ b/java-download-email/pom.xml @@ -0,0 +1,82 @@ + + + + 4.0.0 + + DemoEmailAttachment + EmailAttachments + 0.0.1-SNAPSHOT + + EmailAttachments + + http://www.example.com + + + UTF-8 + 1.7 + 1.7 + + + + + junit + junit + 4.11 + test + + + com.sun.mail + javax.mail + 1.6.2 + + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + + + diff --git a/java-download-email/src/main/java/com/baeldung/downloadAttachments/DownloadEmailAttachments.java b/java-download-email/src/main/java/com/baeldung/downloadAttachments/DownloadEmailAttachments.java new file mode 100644 index 0000000000..c320b69b75 --- /dev/null +++ b/java-download-email/src/main/java/com/baeldung/downloadAttachments/DownloadEmailAttachments.java @@ -0,0 +1,116 @@ +package com.baeldung.downloadAttachments; + +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; + +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(); + String contentType = message.getContentType(); + String messageContent = ""; + String attachments = ""; + if (contentType.contains("multipart")) { + 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(); + attachments += file + ", "; + part.saveFile(downloadDirectory + File.separator + file); + } else { + messageContent = part.getContent() + .toString(); + } + } + if (attachments.length() > 1) { + attachments = attachments.substring(0, attachments.length() - 2); + } + } else if (contentType.contains("text/plain") || contentType.contains("text/html")) { + Object content = message.getContent(); + if (content != null) { + messageContent = content.toString(); + } + } + 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(" Message: " + messageContent); + System.out.println(" Attachments: " + attachments); + } + inbox.close(false); + store.close(); + } + + 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(); + } + } +} \ No newline at end of file diff --git a/java-download-email/src/test/java/com/baeldung/downloadAttachments/DownloadEmailAttachmentsTest.java b/java-download-email/src/test/java/com/baeldung/downloadAttachments/DownloadEmailAttachmentsTest.java new file mode 100644 index 0000000000..b23ec87f05 --- /dev/null +++ b/java-download-email/src/test/java/com/baeldung/downloadAttachments/DownloadEmailAttachmentsTest.java @@ -0,0 +1,25 @@ +package com.baeldung.downloadAttachments; + +import static org.junit.Assert.fail; +import org.junit.Test; + +public class DownloadEmailAttachmentsTest { + @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); + } + } +}