Committing changes for BAEL-3962

This commit is contained in:
rvsathe 2021-05-09 21:21:26 +05:30
parent 54277a0bfb
commit a6a9db7065
3 changed files with 223 additions and 0 deletions

View File

@ -0,0 +1,82 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>DemoEmailAttachment</groupId>
<artifactId>EmailAttachments</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>EmailAttachments</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven
defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@ -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();
}
}
}

View File

@ -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);
}
}
}