Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Java Mail POP3Client Example 
 

This Example shows you how to POP3Client using javamail api. All messages are stored in Folder objects.

 

Java Mail POP3Client Example

                         

This Example shows you how to POP3Client using javamail api. All messages are stored in Folder objects. folders can contain folders or messages. The Folder class declares methods that fetch, append, copy and delete messages. These are some of the methods used in the program.

System.getProperties() this method get the  system properties.
Session.getDefaultInstance(properties) this method get the default Session object.
session.getStore("pop3") this method get a Store object that implements the pop3 protocol.
store.connect(host, user, password) Connect to the current host using the specified username and password.
store.getFolder("inbox") create a Folder object of the inbox.
folder.open(Folder.READ_ONLY) open the Folder.

folder.getMessages() get all messages for the folde.

POP3Clint.java


import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class POP3Clint {

    public static void main(String[] argsthrows Exception {

        String host = "192.168.10.205";
        String user = "test";
        String password = "test";

        // Get system properties
        Properties properties = System.getProperties();

        // Get the default Session object.
        Session session = Session.getDefaultInstance(properties);

        // Get a Store object that implements the specified protocol.
        Store store = session.getStore("pop3");

        //Connect to the current host using the specified username and password.
        store.connect(host, user, password);

        //Create a Folder object corresponding to the given name.
        Folder folder = store.getFolder("inbox");

        // Open the Folder.
        folder.open(Folder.READ_ONLY);

        // Get the messages from the server
        Message[] messages = folder.getMessages();

        // Display message.
        for (int i = 0; i < messages.length; i++) {
            System.out.println("------------ Message " (i + 1" ------------");
            // Here's the big change...
            String from = InternetAddress.toString(messages[i].getFrom());
            if (from != null) {
                System.out.println("From: " + from);
            }
            String replyTo = InternetAddress.toString(
                    messages[i].getReplyTo());
            if (replyTo != null) {
                System.out.println("Reply-to: " + replyTo);
            }
            String to = InternetAddress.toString(
                    messages[i].getRecipients(Message.RecipientType.TO));
            if (to != null) {
                System.out.println("To: " + to);
            }
            String cc = InternetAddress.toString(
                    messages[i].getRecipients(Message.RecipientType.CC));
            if (cc != null) {
                System.out.println("Cc: " + cc);
            }
            String bcc = InternetAddress.toString(
                    messages[i].getRecipients(Message.RecipientType.BCC));
            if (bcc != null) {
                System.out.println("Bcc: " + to);
            }
            String subject = messages[i].getSubject();
            if (subject != null) {
                System.out.println("Subject: " + subject);
            }
            Date sent = messages[i].getSentDate();
            if (sent != null) {
                System.out.println("Sent: " + sent);
            }
            Date received = messages[i].getReceivedDate();
            if (received != null) {
                System.out.println("Received: " + received);
            }
            System.out.println();
        }

        folder.close(true);
        store.close();
    }
}

Output:

------------ Message 1 ------------
From: test@localhost
Reply-to: test@localhost
To: test@localhost
Subject: hi..!
Sent: Tue Jul 15 18:44:41 IST 2008

 ------------ Message 2 ------------
From: test <test@localhost>
Reply-to: test <test@localhost>
To: test@localhost
Subject: Resistance is futile.
Sent: Tue Jul 15 17:57:32 IST 2008 

------------ Message 3 ------------
From: test
Reply-to: test
To: test@localhost
Subject: hi whts goig on ..!
Sent: Tue Jul 15 17:26:47 IST 2008 

------------ Message 4 ------------
From: test@localhost
Reply-to: test@localhost
To: test@localhost
Subject: hi..!
Sent: Mon Jul 14 21:54:28 IST 2008

Download code

                         

» View all related tutorials
Related Tags: java c api class mail method ip new read using this message ai config show if ie example add javamail

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

Dear sir/Madam
is their is any way to download the attachment isn a mail using javamail api if it possible means please send the code regarding downloading the attachment

Regards
R.Raja

Posted by Raja on Monday, 08.25.08 @ 15:42pm | #75017

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.