Skip to content

Instantly share code, notes, and snippets.

@tarun3kumar
Last active July 26, 2020 23:28
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save tarun3kumar/1558421 to your computer and use it in GitHub Desktop.
Save tarun3kumar/1558421 to your computer and use it in GitHub Desktop.
Selenium email verification
import javax.mail.Authenticator;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;
import javax.mail.search.FromTerm;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class MailHelper {
public static void main(String[] args) throws Exception {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "secretmail@gmail.com",
"qwerty!@#$%");
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
System.out.println("Total Message:" + folder.getMessageCount());
System.out.println("Unread Message:"
+ folder.getUnreadMessageCount());
Message[] messages = null;
boolean isMailFound = false;
Message mailFromGod= null;
//Search for mail from God
for (int i = 0; i < 5; i++) {
messages = folder.search(new SubjectTerm(
"Welcome to Heaven"),
folder.getMessages());
//Wait for 10 seconds
if (messages.length == 0) {
Thread.sleep(10000);
}
}
//Search for unread mail from God
//This is to avoid using the mail for which
//Registration is already done
for (Message mail : messages) {
if (!mail.isSet(Flags.Flag.SEEN)) {
mailFromGod = mail;
System.out.println("Message Count is: "
+ mailFromGod.getMessageNumber());
isMailFound = true;
}
}
//Test fails if no unread mail was found from God
if (!isMailFound) {
throw new Exception(
"Could not find new mail from God :-(");
//Read the content of mail and launch registration URL
} else {
String line;
StringBuffer buffer = new StringBuffer();
BufferedReader reader = new BufferedReader(
new InputStreamReader(mailFromGod
.getInputStream()));
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
System.out.println(buffer);
//Your logic to split the message and get the Registration URL goes here
String registrationURL = buffer.toString().split(">http://www.god.de/members/?")[0]
.split("href=")[1];
System.out.println(registrationURL);
}
}
}
@malam2
Copy link

malam2 commented Oct 14, 2017

Looks good, doesn't work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment