|
| 1 | +package com.baeldung.java.io.accessemailwithimap; |
| 2 | + |
| 3 | +import jakarta.mail.Store; |
| 4 | +import jakarta.mail.Folder; |
| 5 | +import jakarta.mail.Message; |
| 6 | +import jakarta.mail.MessagingException; |
| 7 | +import jakarta.mail.Session; |
| 8 | +import jakarta.mail.Flags; |
| 9 | +import jakarta.mail.search.FlagTerm; |
| 10 | +import jakarta.mail.search.FromStringTerm; |
| 11 | +import jakarta.mail.search.SearchTerm; |
| 12 | +import org.slf4j.Logger; |
| 13 | +import org.slf4j.LoggerFactory; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.Properties; |
| 18 | + |
| 19 | +public class GmailApp { |
| 20 | + |
| 21 | + private static final Logger LOGGER = LoggerFactory.getLogger(GmailApp.class); |
| 22 | + |
| 23 | + public static Store establishConnection() throws MessagingException { |
| 24 | + Properties props = System.getProperties(); |
| 25 | + props.setProperty("mail.store.protocol", "imaps"); |
| 26 | + |
| 27 | + Session session = Session.getDefaultInstance(props, null); |
| 28 | + |
| 29 | + Store store = session.getStore("imaps"); |
| 30 | + store.connect("imap.googlemail.com", "EMAIL", "APP PASSWORD"); |
| 31 | + return store; |
| 32 | + } |
| 33 | + |
| 34 | + public static void emailCount(Store store) throws MessagingException { |
| 35 | + Folder inbox = store.getFolder("inbox"); |
| 36 | + Folder spam = store.getFolder("[Gmail]/Spam"); |
| 37 | + inbox.open(Folder.READ_ONLY); |
| 38 | + LOGGER.info("No of Messages : " + inbox.getMessageCount()); |
| 39 | + LOGGER.info("No of Unread Messages : " + inbox.getUnreadMessageCount()); |
| 40 | + LOGGER.info("No of Messages in spam : " + spam.getMessageCount()); |
| 41 | + LOGGER.info("No of Unread Messages in spam : " + spam.getUnreadMessageCount()); |
| 42 | + inbox.close(true); |
| 43 | + } |
| 44 | + |
| 45 | + public static void readEmails(Store store) throws MessagingException, IOException { |
| 46 | + Folder inbox = store.getFolder("inbox"); |
| 47 | + inbox.open(Folder.READ_ONLY); |
| 48 | + Message[] messages = inbox.getMessages(); |
| 49 | + if (messages.length > 0) { |
| 50 | + Message message = messages[0]; |
| 51 | + LOGGER.info("Subject: " + message.getSubject()); |
| 52 | + LOGGER.info("From: " + Arrays.toString(message.getFrom())); |
| 53 | + LOGGER.info("Text: " + message.getContent()); |
| 54 | + } |
| 55 | + inbox.close(true); |
| 56 | + } |
| 57 | + |
| 58 | + public static void searchEmails(Store store, String from) throws MessagingException { |
| 59 | + Folder inbox = store.getFolder("inbox"); |
| 60 | + inbox.open(Folder.READ_ONLY); |
| 61 | + SearchTerm senderTerm = new FromStringTerm(from); |
| 62 | + Message[] messages = inbox.search(senderTerm); |
| 63 | + Message[] getFirstFiveEmails = Arrays.copyOfRange(messages, 0, 5); |
| 64 | + for (Message message : getFirstFiveEmails) { |
| 65 | + LOGGER.info("Subject: " + message.getSubject()); |
| 66 | + LOGGER.info("From: " + Arrays.toString(message.getFrom())); |
| 67 | + } |
| 68 | + inbox.close(true); |
| 69 | + } |
| 70 | + |
| 71 | + public static void deleteEmail(Store store) throws MessagingException { |
| 72 | + Folder inbox = store.getFolder("inbox"); |
| 73 | + inbox.open(Folder.READ_WRITE); |
| 74 | + Message[] messages = inbox.getMessages(); |
| 75 | + |
| 76 | + if (messages.length >= 7) { |
| 77 | + Message seventhLatestMessage = messages[messages.length - 7]; |
| 78 | + |
| 79 | + seventhLatestMessage.setFlag(Flags.Flag.DELETED, true); |
| 80 | + LOGGER.info("Delete the seventh message: " + seventhLatestMessage.getSubject()); |
| 81 | + } else { |
| 82 | + LOGGER.info("There are less than seven messages in the inbox."); |
| 83 | + } |
| 84 | + inbox.close(true); |
| 85 | + } |
| 86 | + |
| 87 | + public static void markLatestUnreadAsRead(Store store) throws MessagingException { |
| 88 | + Folder inbox = store.getFolder("inbox"); |
| 89 | + inbox.open(Folder.READ_WRITE); |
| 90 | + |
| 91 | + Message[] messages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false)); |
| 92 | + if (messages.length > 0) { |
| 93 | + Message latestUnreadMessage = messages[messages.length - 1]; |
| 94 | + latestUnreadMessage.setFlag(Flags.Flag.SEEN, true); |
| 95 | + } |
| 96 | + inbox.close(true); |
| 97 | + } |
| 98 | + |
| 99 | + public static void moveToFolder(Store store, Message message, String folderName) throws MessagingException { |
| 100 | + Folder destinationFolder = store.getFolder(folderName); |
| 101 | + if (!destinationFolder.exists()) { |
| 102 | + destinationFolder.create(Folder.HOLDS_MESSAGES); |
| 103 | + } |
| 104 | + Message[] messagesToMove = new Message[] { message }; |
| 105 | + message.getFolder() |
| 106 | + .copyMessages(messagesToMove, destinationFolder); |
| 107 | + message.setFlag(Flags.Flag.DELETED, true); |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments