Easy learning with example program codes

Deleting emails using JavaMail API

We are explaining here an example of deleting emails using JavaMail API.

Steps of deleting email using JavaMail API:

    1. Get a session instance from getDefaultInstance() or getInstance() method of Session class.
    2. Create the store object (POP3) and connect to the pop store.
    3. Create the folder object by calling getFolder() method on store object and open it in mailbox.
    4. Get the messages from the folder object.
    5. Iterate the emails and get all information like to, from, subject, content etc.
    6. Retrieve the email message to be deleted.
    7. Call the setFlag(Flags.Flag.DELETED, true) method on this message object.
    8. Set expunge to true and close the folder.
    Close store object.


import java.util.*;  
import javax.mail.*;
 * This class is used to delete email.
 * @author codesjava
public class DeleteEmail { 
public static void deleteEmail(String pop3Host,
		String storeType, String user, String password){
	Properties props = new Properties();
	props.put("", pop3Host);
	Session session = Session.getInstance(props);
	try {
	 Store store = session.getStore(storeType);
	 store.connect(pop3Host, user, password);
	 Folder folder = store.getFolder("INBOX");;
         Message[] messages = folder.getMessages();
         System.out.println("Total Message - " + messages.length);
         BufferedReader reader = 
          new BufferedReader(new InputStreamReader(
       for (int i = 0; i < messages.length; i++) {
    	   Message emailMessage = messages[i];
    	   System.out.println("Email " + (i+1) + " -");  
    	   System.out.println("Subject - " + 
    	   System.out.println("From - " + 
       System.out.print("Enter email number " +
       		"which you want to delete: ");
       String emailNo = reader.readLine();
       messages[Integer.parseInt(emailNo) - 1].
       setFlag(Flags.Flag.DELETED, true);
       System.out.println("Email" +
       		" deleted successfully.");
	} catch (Exception e) {
        System.err.println("Error in deleting email.");
public static void main(String[] args) {
	String pop3Host = "";
    String mailStoreType = "pop3s";	
    final String userName = "";
    final String password = "****";
   //call deleteEmail
   deleteEmail(pop3Host, mailStoreType, userName, password);


DEBUG POP3: server doesn't support TOP, disabling it
Total Message - 260
Email 1 -
Subject - Derek Banas added you back on Google+
From - Google+ <>
Email 2 -
Subject - javawithease, find your friends on Twitter!
From - Twitter <>
Enter email number which you want to delete: 2
Email deleted successfully.

Download this example.
Next Topic: Quartz scheduler Tutorial with example.
Previous Topic: Forwarding emails with example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.


Please follow and like us:

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki