You are on page 1of 3

package com.core; import java.util.Properties; import import import import import import import javax.mail.Address; javax.mail.Authenticator; javax.mail.Folder; javax.mail.

Message; javax.mail.PasswordAuthentication; javax.mail.Session; javax.mail.Store;

public class MailReceiving { public static String activationLink; public static String recoveringPasswordLink; public static String ReciveEmail(String email, String password) throws Exception { Properties props = new Properties(); String host = "pop.gmail.com"; String provider = "pop3s"; Session session = Session.getDefaultInstance(props, new MailAuthenticator(em ail, password)); Store store = session.getStore(provider); store.connect(host, null, null); Folder inbox = store.getFolder("INBOX"); if (inbox == null) { System.out.println("No INBOX"); System.exit(1); } inbox.open(Folder.READ_ONLY); Message[] messages = inbox.getMessages(); for (int i = 0; i < messages.length; i++) { String subject = messages[i].getSubject().toString().toLowerCase().trim( ); int subjectExpendedLength = 34; // . " int subjectLength = subject.length(); if (subjectLength != subjectExpendedLength){ System.err.println(" ...."); continue; } Object contentMessage = messages[i].getContent(); String content = contentMessage.toString(); CharSequence searchPhrase = "http://www.bt-lady.com.ua/user/confirm/"; if(content.contains(searchPhrase)){ System.out.println(" ."); } else { System.err.println("! !"); continue; }

int indexOf = content.lastIndexOf("http://www.bt-lady.com.ua/user/confir m/"); int lastIndexOf = content.lastIndexOf("--"); activationLink = content.substring(indexOf, lastIndexOf).trim(); /* System.out.println("indexOf: " + indexOf); System.out.println("lastIndexOf: " + lastIndexOf); System.out.println(" : " + (i + 1)); System.out.println(": " + subject); System.out.println(" : " + subjectLength); System.out.println(" : " + activationLink); System.out.println(": " + content); messages[i].writeTo(System.out); */ } inbox.close(false); store.close(); return activationLink; }

/*============================================================================== =============================== ==================================== ======================= ================================================================================ =============================*/ public static String ReciveEmailRecoveringPassword(String email, String password ) throws Exception { Properties props = new Properties(); String host = "pop.gmail.com"; String provider = "pop3s"; Session session = Session.getDefaultInstance(props, new MailAuthenticator(em ail, password)); Store store = session.getStore(provider); store.connect(host, null, null); Folder inbox = store.getFolder("INBOX"); if (inbox == null) { System.out.println("No INBOX"); System.exit(1); } inbox.open(Folder.READ_ONLY); Message[] messages = inbox.getMessages(); for (int i = 0; i < messages.length; i++) { String subject = messages[i].getSubject().toString().toLowerCase().trim( ); int subjectExpendedLength = 12; // . " " int subjectLength = subject.length(); if (subjectLength != subjectExpendedLength){ System.err.println(" ....");

continue; } Object contentMessage = messages[i].getContent(); String content = contentMessage.toString(); CharSequence searchPhrase = "http://www.bt-lady.com.ua/user/reset/"; if(content.contains(searchPhrase)){ System.out.println(" ."); } else { System.err.println("! !"); continue; } int indexOf = content.lastIndexOf("http://www.bt-lady.com.ua/user/reset/ "); int lastIndexOf = content.lastIndexOf("--"); recoveringPasswordLink = content.substring(indexOf, lastIndexOf).trim(); /**/ System.out.println("indexOf: " + indexOf); System.out.println("lastIndexOf: " + lastIndexOf); System.out.println(" : " + (i + 1)); System.out.println(": " + subject); System.out.println(" : " + subjectLength); System.out.println(" : " + activationLink); System.out.println(": " + content); messages[i].writeTo(System.out); /**/ } inbox.close(false); store.close(); return recoveringPasswordLink; }

class MailAuthenticator extends Authenticator { private String email; // private String password; // MailAuthenticator(String email, String password){ this.email = email; this.password = password; } public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(this.email, this.password); } }

You might also like