Skip to content
This repository was archived by the owner on Feb 24, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 66 additions & 59 deletions EasyContact.app/server/pom.xml
Original file line number Diff line number Diff line change
@@ -1,61 +1,68 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/>
</parent>
<groupId>app.EasyContact.server</groupId>
<artifactId>EasyContact.server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>EasyContact.server</name>
<description>EasyContact Backend Java Server</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/>
</parent>
<groupId>app.EasyContact.server</groupId>
<artifactId>EasyContact.server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>EasyContact.server</name>
<description>EasyContact Backend Java Server</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package app.easycontact.server.imap;

import lombok.Data;
import lombok.experimental.SuperBuilder;

@SuperBuilder
@Data
public abstract class Email {
public final long id;
public final String from;
public final String subject;
public final String toList;
public final String ccList;
public final String sendDate;
public final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package app.easycontact.server.imap;

import lombok.Data;
import lombok.experimental.SuperBuilder;

@SuperBuilder
@Data
public final class EmailImplementation extends Email {
}
Original file line number Diff line number Diff line change
@@ -1,138 +1,19 @@
package app.easycontact.server.imap;

import javax.mail.*;
import java.util.Properties;
import lombok.NonNull;
import lombok.experimental.SuperBuilder;

public class EmailReceiver {

/**
* Test downloading e-mail messages
*/
public static void main(String[] args) {
// for IMAP
String protocol = "imap";
String host = "mail.rpi.edu";
String port = "993";

String userName = "usrName";
String password = "password";

EmailReceiver receiver = new EmailReceiver();
receiver.downloadEmails(protocol, host, port, userName, password);
}

/**
* Returns a Properties object which is configured for a POP3/IMAP server
*
* @param protocol either "imap" or "pop3"
* @param host
* @param port
* @return a Properties object
*/
private Properties getServerProperties(String protocol, String host, String port) {
Properties properties = new Properties();

// server setting
properties.put(String.format("mail.%s.host", protocol), host);
properties.put(String.format("mail.%s.port", protocol), port);

// SSL setting
properties.setProperty(String.format("mail.%s.socketFactory.class", protocol), "javax.net.ssl.SSLSocketFactory");
properties.setProperty(String.format("mail.%s.socketFactory.fallback", protocol), "false");
properties.setProperty(String.format("mail.%s.socketFactory.port", protocol), String.valueOf(port));

return properties;
}
@SuperBuilder
public abstract class EmailReceiver {
@NonNull String userName;
@NonNull String userPassword;
@NonNull String serverName;
@NonNull String serverPort;

/**
* Downloads new messages and fetches details for each message.
*
* @param protocol
* @param host
* @param port
* @param userName
* @param password
*/
public void downloadEmails(String protocol, String host, String port, String userName, String password) {
Properties properties = getServerProperties(protocol, host, port);
Session session = Session.getDefaultInstance(properties);

try {
// connects to the message store
Store store = session.getStore(protocol);
store.connect(userName, password);

// opens the inbox folder
Folder folderInbox = store.getFolder("INBOX");
folderInbox.open(Folder.READ_ONLY);

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

for (int i = 0; i < messages.length; i++) {
Message msg = messages[i];
Address[] fromAddress = msg.getFrom();
String from = fromAddress[0].toString();
String subject = msg.getSubject();
String toList = parseAddresses(msg.getRecipients(Message.RecipientType.TO));
String ccList = parseAddresses(msg.getRecipients(Message.RecipientType.CC));
String sentDate = msg.getSentDate().toString();

String contentType = msg.getContentType();
String messageContent = "";

if (contentType.contains("text/plain") || contentType.contains("text/html")) {
try {
Object content = msg.getContent();
if (content != null) {
messageContent = content.toString();
}
} catch (Exception ex) {
messageContent = "[Error downloading content]";
ex.printStackTrace();
}
}

// print out details of each message
System.out.println("Message #" + (i + 1) + ":");
System.out.println("\t From: " + from);
System.out.println("\t To: " + toList);
System.out.println("\t CC: " + ccList);
System.out.println("\t Subject: " + subject);
System.out.println("\t Sent Date: " + sentDate);
System.out.println("\t Message: " + messageContent);
}

// disconnect
folderInbox.close(false);
store.close();
} catch (NoSuchProviderException ex) {
System.out.println("No provider for protocol: " + protocol);
ex.printStackTrace();
} catch (MessagingException ex) {
System.out.println("Could not connect to the message store");
ex.printStackTrace();
}
}

/**
* Returns a list of addresses in String format separated by comma
*
* @param address an array of Address objects
* @return a string represents a list of addresses
* @param folder name of the email folder
*/
private String parseAddresses(Address[] address) {
String listAddress = "";

if (address != null) {
for (int i = 0; i < address.length; i++) {
listAddress += address[i].toString() + ", ";
}
}
if (listAddress.length() > 1) {
listAddress = listAddress.substring(0, listAddress.length() - 2);
}

return listAddress;
}
public abstract void downloadEmails(String folder);
}
Loading