Deleting email utilizing JavaMail API

As we send, forward and get the messages, we can erase it as well. The setFlag strategy for Message class is utilized to erase a specific message.

For better comprehension of this case, take in the progressions of sending email utilizing JavaMail API first.

For accepting or sending the email utilizing JavaMail API, you have to stack the two container records:

  • mail.jar
  • activation.jar

download these jar files go to the Oracle site to download the latest version.

Steps for deleting the email using JavaMail API

There are downright 5 stages for erasing the email. They are:

  1. Get the session object
  2. make the store question and join with the present host
  3. make the folder object and open it
  4. Get the message to erase
  5. erase the message utilizing setFlag technique

Example of deleting email using JavaMail API

import java.util.*;  
import javax.mail.*;  
import javax.mail.internet.*; 
import com.sun.mail.imap.protocol.FLAGS;  

public class DeleteMail {  
 public static void main(String args[]) throws Exception {  
 String user= "";//change as needs be   
 String password="******";//change as needs be   
 //1. get the session object  
 Properties properties = System.getProperties();  
 Session session = Session.getDefaultInstance(properties);  
 //2. create the store object and connect to the current host   
 Store store = session.getStore("pop3");  
 //3. create the folder object and open it  
 Folder folder = store.getFolder("inbox");  
 if (!folder.exists()) {  
 System.out.println("inbox not found");  
 //4. Get the message to delete  
 Message[] msg = folder.getMessages();  
 //System.out.println((messages.length+1)+" message found");  
 for (int i = 0; i < msg.length; i++) {  
   System.out.println("--------- " + (i + 1) + "------------");  
   String from = InternetAddress.toString(msg[i].getFrom());  
   if (from != null) {  
     System.out.println("From: " + from);  
   String replyTo = InternetAddress.toString(  
   if (replyTo != null) {  
    System.out.println("Reply-to: " + replyTo);  
   String to = InternetAddress.toString(  
   if (to != null) {  
    System.out.println("To: " + to);  
   String subject = msg[i].getSubject();  
   if (subject != null) {  
    System.out.println("Subject: " + subject);  
   Date sent = msg[i].getSentDate();  
   if (sent != null) {  
    System.out.println("Sent: " + sent);  
   System.out.println("Message : ");  
 }//end of for loop  
  // get the message number to delete (optional)  
  System.out.println("Enter message number to delete :");  
  BufferedReader br = new BufferedReader(new InputStreamReader(;  
  String no = br.readLine();  
  //5) delete the message using setFlag method  
  msg[Integer.parseInt(no) - 1].setFlag(FLAGS.Flag.DELETED, true);  
  System.out.println("Message Deleted .....");  

As you can see in the above example, we are able to delete the email from the user mailbox. Now run this program by :

Load the jar file c:\> set classpath=mail.jar;activation.jar;.;
compile the source file c:\> javac
run by c:\> java DeleteMail