diff --git a/src/main/java/utils/email/EmailAcquisition.java b/src/main/java/utils/email/EmailAcquisition.java index c322576..afb8e47 100644 --- a/src/main/java/utils/email/EmailAcquisition.java +++ b/src/main/java/utils/email/EmailAcquisition.java @@ -188,4 +188,20 @@ public String acquireEmail( } return absolutePath; } + + public EmailUtilities.Inbox getInbox() { + return inbox; + } + + public void setInbox(EmailUtilities.Inbox inbox) { + this.inbox = inbox; + } + + public int getEmailAcquisitionTimeout() { + return emailAcquisitionTimeout; + } + + public void setEmailAcquisitionTimeout(int emailAcquisitionTimeout) { + this.emailAcquisitionTimeout = emailAcquisitionTimeout; + } } diff --git a/src/main/java/utils/email/EmailUtilities.java b/src/main/java/utils/email/EmailUtilities.java index 0b730f5..2fce5ee 100644 --- a/src/main/java/utils/email/EmailUtilities.java +++ b/src/main/java/utils/email/EmailUtilities.java @@ -10,19 +10,28 @@ import utils.email.mapping.EmailFlag; import utils.reflection.ReflectionUtilities; +import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.io.File; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.*; import static utils.StringUtilities.markup; import static utils.arrays.lambda.Collectors.toSingleton; +/** + * Utility class for sending and managing emails. + *
+ * This class provides functionality to send emails via SMTP and includes an inner {@link Inbox} class + * for retrieving, filtering, and managing emails using IMAP or POP3 protocols. + *
+ */ @SuppressWarnings({"unused", "UnusedReturnValue"}) public class EmailUtilities { /** - * Creates a new instance of EmailUtilities with the specified host. + * Creates a new instance of EmailUtilities with the specified SMTP host. * * @param host the hostname of the SMTP server for sending emails */ @@ -35,14 +44,14 @@ public EmailUtilities(String host) { private String host; /** - * Sends an email message with an optional attachment to the specified recipient. + * Sends an email message with an optional attachment to the specified recipient using default text/plain content type. * * @param subject the subject of the email - * @param content the content of the email + * @param content the body content of the email * @param receiver the email address of the recipient - * @param ID the username for authenticating with the SMTP server - * @param password the password for authenticating with the SMTP server - * @param attachment the optional multipart attachment to include in the email + * @param ID the username (sender email) for authenticating with the SMTP server + * @param password the password (or application password) for authenticating with the SMTP server + * @param attachment the optional multipart attachment to include in the email; can be null * @return true if the email was sent successfully, false otherwise */ public Boolean sendEmail(String subject, String content, String receiver, String ID, String password, Multipart attachment) { @@ -58,58 +67,48 @@ public Boolean sendEmail(String subject, String content, String receiver, String } /** - * Sends an email message with an optional attachment to the specified recipient. + * Sends an email message with an optional attachment to the specified recipient with a custom content type. + *+ * This method configures the SMTP session using port 587 with STARTTLS enabled. + *
* - * @param subject the subject of the email - * @param content the content of the email - * @param receiver the email address of the recipient - * @param ID the username for authenticating with the SMTP server - * @param password the password for authenticating with the SMTP server - * @param attachment the optional multipart attachment to include in the email + * @param subject the subject of the email + * @param content the body content of the email + * @param contentType the MIME type of the content (e.g., "text/html; charset=utf-8") + * @param receiver the email address of the recipient + * @param ID the username (sender email) for authenticating with the SMTP server + * @param password the password (or application password) for authenticating with the SMTP server + * @param attachment the optional multipart attachment to include in the email; can be null * @return true if the email was sent successfully, false otherwise */ public Boolean sendEmail(String subject, String content, String contentType, String receiver, String ID, String password, Multipart attachment) { - - // Get system properties Properties properties = new Properties(); properties.putAll(System.getProperties()); - // Setup mail server 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"); - // Get the Session object.// and pass username and password Session session = Session.getInstance(properties, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(ID, password); } }); - // Used to debug SMTP issues session.setDebug(keepLogs); try { - // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); - - // Set From: header field of the header. message.setFrom(new InternetAddress(ID)); - - // Set To: header field of the header. message.addRecipient(Message.RecipientType.TO, new InternetAddress(receiver)); - - // Set Subject: header field message.setSubject(subject); - - // Now set the actual message message.setContent(content, contentType); if (attachment != null) message.setContent(attachment); if (keepLogs) log.info("Sending..."); - Transport.send(message);// Send message + Transport.send(message); if (keepLogs) log.success("Sent message successfully!"); return true; } catch (MessagingException mex) { @@ -127,6 +126,12 @@ private void setHost(String host) { this.host = host; } + /** + * Represents an email inbox and provides methods to retrieve, filter, and delete messages. + *+ * Supports both IMAP and POP3 protocols via the {@link EmailProtocol} enum. + *
+ */ public static class Inbox { private final Printer log = new Printer(Inbox.class); private final String host; @@ -134,27 +139,104 @@ public static class Inbox { private final String userName; private final String password; private final String secureCon; + private final EmailProtocol protocol; /** - * List of email messages represented as a list of maps where each map contains email fields as keys - * and their corresponding values as values. + * List of email messages retrieved from the server. */ - public static List+ * This method waits for the expected number of messages to appear before filtering. + *
+ * + * @param inbox the Inbox instance to use + * @param timeout timeout in seconds to wait for messages + * @param expectedMessageCount the number of messages expected in the inbox + * @param print whether to print message details to the log + * @param save whether to save the message body to a file + * @param saveAttachments whether to download attachments + * @param filterPairs filters to identify the specific message to return + * @return the matching EmailMessage + */ + public static EmailMessage getEmail(Inbox inbox, int timeout, int expectedMessageCount, boolean print, boolean save, boolean saveAttachments, List+ * Note: This opens the Inbox folder in {@code READ_WRITE} mode. + * It preserves the original {@code SEEN} (read) state of messages that do not match the filter. + * Messages that match the filter are processed and implicitly marked as {@code SEEN}. + *
* - * @param print boolean flag indicating whether to print the emails - * @param save boolean flag indicating whether to save the emails - * @param saveAttachments boolean flag indicating whether to save email attachments - * @param filterPairs a list of pairs consisting of email fields and corresponding filter strings + * @param print whether to print message details + * @param save whether to save message content + * @param saveAttachments whether to save attachments + * @param filterPairs filters to apply */ public void load(boolean print, boolean save, boolean saveAttachments, ListEach filter pair consists of an {@link EmailField} and a string value.
- * Messages are deleted only if they match all provided filter criteria.
- *
- * @param filterPairs List of key-value pairs where:
- * - Key: {@link EmailField} (e.g., SUBJECT, FROM)
- * - Value: String to match against the corresponding field
- * @throws MessagingException if there's an error connecting to the email server
- * or performing mailbox operations
+ * @param filterPairs filters to identify messages to delete
*/
public void clearInbox(List Messages are filtered based on key-value pairs (EmailField + String value)
- * and the specified flag is applied to matching messages.
+ * Applies a specific EmailFlag (e.g., DELETED, SEEN) to messages matching the provided filters.
+ * Messages that do not match are restored to their original flags.
*
- * @param flag The flag to apply to matching messages (e.g., Flags.Flag.DELETED)
- * @param filterPairs Variable arguments of key-value pairs where:
- * - Key: {@link EmailField} (e.g., SUBJECT, FROM)
- * - Value: String to match against the corresponding field
+ * @param flag the flag to apply
+ * @param filterPairs variable arguments of filters
*/
@SafeVarargs
public final void clearInbox(EmailFlag flag, Pair This method does not apply any filters and will delete every message in the inbox.
- *
+ * Deletes all messages in the inbox.
*/
public void clearInbox() {
try {
- Store store = getImapStore();
+ Store store = createStoreConnection();
Folder folderInbox = store.getFolder("INBOX");
folderInbox.open(Folder.READ_WRITE);
- // fetches new messages from server
log.info("Getting inbox..");
List