package email import javax.mail.* import javax.mail.search.SubjectTerm import java.util.Properties import java.util.regex.Pattern import java.util.regex.Matcher public class GmailReader { public static String getResetLink(String email, String appPassword) { Properties props = new Properties() props.put("mail.store.protocol", "imaps") props.put("mail.imaps.host", "imap.gmail.com") props.put("mail.imaps.port", "993") props.put("mail.imaps.ssl.enable", "true") Session session = Session.getDefaultInstance(props, null) Store store = session.getStore("imaps") store.connect("imap.gmail.com", email, appPassword) Folder inbox = store.getFolder("INBOX") inbox.open(Folder.READ_ONLY) Message[] messages = inbox.search( new SubjectTerm("Pemberitahuan Reset Password") ) if (messages.length == 0) { inbox.close(false) store.close() throw new Exception("Email reset password tidak ditemukan") } Message latestMessage = messages[messages.length - 1] String content = getTextFromMessage(latestMessage) Pattern pattern = Pattern.compile("(https?:\\/\\/[^\\s\"<>]+)") Matcher matcher = pattern.matcher(content) String resetLink = "" while (matcher.find()) { String link = matcher.group() if (link.contains("reset-password")) { resetLink = link break } } inbox.close(false) store.close() if (resetLink == "") { throw new Exception("Link reset-password tidak ditemukan di email") } return resetLink } private static String getTextFromMessage(Message message) { Object content = message.getContent() if (content instanceof String) { return content.toString() } if (content instanceof Multipart) { Multipart multipart = (Multipart) content String result = "" for (int i = 0; i < multipart.getCount(); i++) { BodyPart bodyPart = multipart.getBodyPart(i) if (bodyPart.getContent() instanceof String) { result += bodyPart.getContent().toString() } } return result } return content.toString() } }