Thursday, 1 October 2015

How to receive Mail from Gmail in java ?

import com.jscape.inet.email.EmailMessage;
import com.jscape.inet.popssl.PopSsl;

public class SecurePOPMail {

   public static void main(String[] args) {
   
   
   // and other services will be similar.
      String hostname = "pop.gmail.com";
      String username = "sky33616@gmail.com";   // These lines are your Gmail or Hotmail login
      String password = "XXXXXXXXXX";//Your Password 

   String msgSubject; // Various parts of an EmailMessage
   String msgFrom;
   String msgTo;
   String msgDate;
   String msgBody;
   // Other receivers of this message. "CC" is "carbon copy", if you didn't know, and 
   // "BCC" is "blind carbon copy". When was the last time you saw carbon paper?
   String msgCC; 
   String msgBCC;
   
      try {
         // Create new PopSsl instance and establish the connection to the POP3 server. "995" is 
   // the port number and triggers "implicit" SSL/TLS encryption for the whole message.
         PopSsl ssl = new PopSsl(hostname, 995, username, password);
         ssl.setDebug(true);
         ssl.connect();

         // Get the message count, then print selected parts of each message.
         int count = ssl.getMessageCount();
         for(int i = 1; i <= count; ++i) {
            // Create an EmailMessage instance, then get all the parts separately.
   EmailMessage msg = ssl.getMessage(i);
   msgSubject = msg.getSubject();
   msgFrom = msg.getFrom();
   msgTo = msg.getTo();
   msgDate = msg.getDate();
   msgBody = msg.getBody();
   msgCC = msg.getCc();
   msgBCC = msg.getBcc();

   // This is just to show that you can manipulate message parts. If the
   // Subject includes the word "work", don't print it, because
   // it's sure to be trouble for somebody!
   if (msgSubject.indexOf("work") <= 0) {
    System.out.println("Subject: " + msg.getSubject()); 
   } else {
    System.out.println("Subject: [You don't want to know]");
   }
   
   // Now print the message parts.
   System.out.println("------------------------");
   System.out.println("From: " + msg.getFrom());
   System.out.println("Date: " + msg.getDate());
   System.out.println("CC: " + msg.getCc());
   System.out.println("BCC: " + msg.getBcc());
   System.out.println("Body: " + msg.getBody());
   System.out.println("------------------------");
         }

         // Disconnect
         ssl.disconnect();     
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

Download sinetfactory.jar from here

Set it on Your Classpath

Compile it & Run it As JAVA className

No comments:

Post a Comment