Skip to content
Merged
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
103 changes: 92 additions & 11 deletions src/main/java/utils/email/EmailUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import utils.DateUtilities;
import utils.Printer;
import utils.reflection.ReflectionUtilities;

import java.io.FileWriter;
import java.io.IOException;
import java.io.File;
Expand Down Expand Up @@ -74,6 +75,7 @@ public Boolean sendEmail(String subject, String content, String contentType, Str
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the use case for this?
Perhaps it makes sense to bind a property here and let the user change this value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this property sendEmail method is not working:

SEVERE: 530-5.7.0 Must issue a STARTTLS command first. For more information, go to
530-5.7.0 https://support.google.com/a/answer/3221692 and review RFC 3207
530 5.7.0 specifications. a640c23a62f3a-ac3efd569fesm853582966b.173 - gsmtp

com.sun.mail.smtp.SMTPSendFailedException: 530-5.7.0 Must issue a STARTTLS command first. For more information, go to
530-5.7.0 https://support.google.com/a/answer/3221692 and review RFC 3207
530 5.7.0 specifications. a640c23a62f3a-ac3efd569fesm853582966b.173 - gsmtp

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats odd, it worked before 🤔


// Get the Session object.// and pass username and password
Session session = Session.getInstance(properties, new Authenticator() {
Expand Down Expand Up @@ -227,7 +229,7 @@ public static EmailMessage from(Message message) {
return new EmailMessage(message);
}

public void setFileName(String fileName){
public void setFileName(String fileName) {
this.fileName = fileName + ".html";
}
}
Expand Down Expand Up @@ -275,6 +277,7 @@ public Inbox(String host,
this.userName = userName;
this.password = password;
this.secureCon = secureCon;
messages = new ArrayList<>();
}

public static EmailMessage getEmail(
Expand Down Expand Up @@ -386,7 +389,7 @@ public void load(boolean print, boolean save, boolean saveAttachments, List<Pair
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);
Session session = Session.getInstance(properties);
//----------------------------------------

try {
Expand All @@ -400,7 +403,10 @@ public void load(boolean print, boolean save, boolean saveAttachments, List<Pair
log.info("Getting inbox..");

// fetches new messages from server
List<Message> messages = List.of(folderInbox.getMessages());
List<Message> messages = new ArrayList<>(List.of(folderInbox.getMessages()));

// Reverse the order of the list
Collections.reverse(messages);

for (Message message : messages) {
if (emailMatch(EmailMessage.from(message), filterPairs))
Expand Down Expand Up @@ -653,18 +659,93 @@ public static void clearInbox(
);
}

/**
* IMAP connection to get the inbox
*/
private Store getImapStore() {
Properties properties = new Properties();

//---------- Server Setting---------------
properties.put("mail.imap.host", host);
properties.put("mail.imap.port", port);
if (secureCon.equalsIgnoreCase("ssl")) {
properties.put("mail.imap.ssl.enable", "true");
} else {
properties.put("mail.imap.ssl.enable", "false");
}
//---------- SSL setting------------------
properties.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.imap.socketFactory.fallback", "false");
properties.setProperty("mail.imap.socketFactory.port", String.valueOf(port));
Session session = Session.getInstance(properties);
Store store = null;
try {
log.info("Connecting please wait....");
store = session.getStore("imap");
store.connect(userName, password);
} catch (MessagingException exception) {
log.error(exception.getLocalizedMessage(), exception);
}
return store;
}


/**
* Clears the email inbox using the configured email credentials and server settings.
*/
public void clearInbox() {
log.info("Flushing email inbox...");
new EmailUtilities.Inbox(
host,
port,
userName,
password,
secureCon
);
try {
Store store = getImapStore();
Folder folderInbox = store.getFolder("INBOX");
folderInbox.open(Folder.READ_WRITE);

// fetches new messages from server
log.info("Getting inbox..");
List<Message> messages = List.of(folderInbox.getMessages());

log.info("Deleting messages..");
// Delete all the messages
for (Message message : messages) {
message.setFlag(Flags.Flag.DELETED, true);
}

// Delete messages and close connection
folderInbox.close(true);
store.close();
log.info(messages.size() + " messages have been successfully deleted!");

} catch (MessagingException exception) {
log.error(exception.getLocalizedMessage(), exception);
}
}

/**
* Clear inbox in batches - use it on the large inboxes to optimize the process
*/
public void clearInboxInBatches(int batchSize) {
try {
Store store = getImapStore();
Folder folderInbox = store.getFolder("INBOX");
folderInbox.open(Folder.READ_WRITE);

// fetches new messages from server
log.info("Getting inbox..");
List<Message> messages = List.of(folderInbox.getMessages());
List<Message> batchToDelete = messages.subList(0, batchSize);
log.info("Deleting messages..");
// Delete all the messages
for (Message message : batchToDelete) {
message.setFlag(Flags.Flag.DELETED, true);
}

// Delete messages and close connection
folderInbox.close(true);
store.close();
log.info(batchToDelete.size() + " messages out of " + messages.size() + " have been successfully deleted!");

} catch (MessagingException exception) {
log.error(exception.getLocalizedMessage(), exception);
}
}
}
}
49 changes: 47 additions & 2 deletions src/test/java/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,47 @@ public void getCurrentDateTest() {
);
printer.success("The getSimpleDateFormatStringFromTest() test passed!");
}


@Test
public void cleanEmailTest() {
EmailUtilities.Inbox inbox = new EmailUtilities.Inbox("pop.gmail.com",
"995",
ContextStore.get("test-email"),
ContextStore.get("test-email-application-password"),
"ssl");

String emailTestContent = "username:xyz";
String emailSubject = "Test subject of email for deletion";
EmailUtilities emailUtilities = new EmailUtilities(ContextStore.get("host"));
emailUtilities.sendEmail(
emailSubject,
emailTestContent,
ContextStore.get("test-email"),
ContextStore.get("sender-test-email"),
ContextStore.get("test-email-master-password"),
null);

inbox.load(30, 1, false, false, false,
List.of(Pair.of(SUBJECT, emailSubject)));
Assert.assertEquals("Unexpected number of emails found!", 1, inbox.getMessages().size());

new EmailUtilities.Inbox("imap.gmail.com",
"993",
ContextStore.get("test-email"),
ContextStore.get("test-email-application-password"),
"ssl").clearInbox();

EmailUtilities.Inbox newInbox = new EmailUtilities.Inbox("pop.gmail.com",
"995",
ContextStore.get("test-email"),
ContextStore.get("test-email-application-password"),
"ssl");
newInbox.load(SUBJECT, emailSubject, false, true, true);

Assert.assertEquals("Unexpected number of emails found!", 0, newInbox.getMessages().size());
printer.success("cleanEmailTest() is successful!");
}

@Test
public void filterEmailTest() {
EmailUtilities.Inbox inbox = new EmailUtilities.Inbox(
Expand All @@ -189,7 +229,12 @@ public void filterEmailTest() {
"ssl"
);

inbox.clearInbox();
new EmailUtilities.Inbox("imap.gmail.com",
"993",
ContextStore.get("test-email"),
ContextStore.get("test-email-application-password"),
"ssl").clearInbox();

String emailTestContent = "username:xyz";
EmailUtilities emailUtilities = new EmailUtilities(ContextStore.get("host"));
emailUtilities.sendEmail(
Expand Down