How to read first 10 unread mails using java

To read the first 10 unread emails using Java, you'll need to use a Java Mail API library such as JavaMail or Apache Commons Email. Here's an example using JavaMail:

Step 1: Add the JavaMail library to your project

You can add the JavaMail library to your project using Maven:

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>

Or, you can download the JavaMail library and add it to your project manually.

Step 2: Set up your email account

Create a Properties object to store your email account settings:

Properties props = new Properties();
props.put("mail.store.protocol", "imaps"); // or "pop3" if you're using POP3
props.put("mail.imap.host", "imap.gmail.com"); // or "pop.gmail.com" for POP3
props.put("mail.imap.port", "993"); // or "110" for POP3
props.put("mail.imap.starttls.enable", "true");
props.put("mail.user", "your-email-address");
props.put("mail.password", "your-email-password");

Step 3: Connect to the email server and retrieve the emails

Create a Session object using the Properties object:

Session session = Session.getInstance(props, new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(props.getProperty("mail.user"), props.getProperty("mail.password"));
    }
});

Use the Store object to connect to the email server and retrieve the emails:

Store store = session.getStore();
store.connect();
Folder folder = store.getFolder("INBOX"); // or "INBOX" for POP3
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();

Step 4: Filter and read the first 10 unread emails

Use a loop to iterate through the messages and filter out the unread emails:

int unreadCount = 0;
for (Message message : messages) {
    if (!message.isSet(Flags.Flag.SEEN)) { // check if the message is unread
        unreadCount++;
        if (unreadCount <= 10) { // read the first 10 unread emails
            System.out.println("Subject: " + message.getSubject());
            System.out.println("From: " + message.getFrom()[0]);
            System.out.println("Text: " + message.getContent().toString());
        }
    }
}

Step 5: Close the connection

Close the connection to the email server:

folder.close(false);
store.close();

Here's the complete code:

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class ReadUnreadEmails {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("mail.store.protocol", "imaps");
        props.put("mail.imap.host", "imap.gmail.com");
        props.put("mail.imap.port", "993");
        props.put("mail.imap.starttls.enable", "true");
        props.put("mail.user", "your-email-address");
        props.put("mail.password", "your-email-password");

        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(props.getProperty("mail.user"), props.getProperty("mail.password"));
            }
        });

        Store store = session.getStore();
        store.connect();
        Folder folder = store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);
        Message[] messages = folder.getMessages();

        int unreadCount = 0;
        for (Message message : messages) {
            if (!message.isSet(Flags.Flag.SEEN)) {
                unreadCount++;
                if (unreadCount <= 10) {
                    System.out.println("Subject: " + message.getSubject());
                    System.out.println("From: " + message.getFrom()[0]);
                    System.out.println("Text: " + message.getContent().toString());
                }
            }
        }

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

Note that this code assumes you're using IMAP to connect to your email server. If you're using POP3, you'll need to modify the code accordingly. Additionally, you'll need to replace the placeholders (your-email-address and your-email-password) with your actual email account credentials.