EXAMPLE FOR RECEIVING EMAIL
INTRODUCTION:
To Receive email using javamail API.We use POP3 server.
PROCEDURE:
Like sending email.
EXAMPLE:
To Receive email using javamail API.We use POP3 server.
PROCEDURE:
Like sending email.
EXAMPLE:
package p1;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import com.sun.mail.pop3.POP3SSLStore;
import java.util.*;
public class ReceiverExam
{
private static Session session = null;
private static Store store = null;
private static String username, password;
private static Folder folder;
public static void main(String args[])throws Exception
{
try
{
username="Email ID";
password="Password";
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties pop3Props = new Properties();
pop3Props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
pop3Props.setProperty("mail.pop3.socketFactory.fallback", "false");
pop3Props.setProperty("mail.pop3.port", "995");
pop3Props.setProperty("mail.pop3.socketFactory.port", "995");
URLName url = new URLName("pop3", "pop.gmail.com", 995, "",
username, password);
session = Session.getInstance(pop3Props, null);
store = new POP3SSLStore(session, url);
store.connect();
System.out.println("connected");
folder=store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message[] message=folder.getMessages();
for (int i = 0; i < message.length; i++)
{
System.out.println("------------ Message " + (i + 1) + " ------------");
System.out.println("SentDate : " + message[i].getSentDate());
System.out.println("From : " + message[i].getFrom()[0]);
System.out.println("Subject : " + message[i].getSubject());
System.out.print("Message : ");
InputStream stream = message[i].getInputStream();
while (stream.available() != 0)
{
System.out.print((char) stream.read());
}
System.out.println();
}
folder.close(true);
store.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
1 comment:
Thank you very much for this
Post a Comment