Deleting email in Java

Example of deleting email in Java provides examples of sending, receiving, forwarding and deleting emails, Learn how to delete email in java

Deleting email in Java

Deleting email in Java

As we send, forward and receive the emails, we can delete it too. The setFlag method of Message class is used to delete a particular message.

For better understanding of this example, learn the steps of sending email using JavaMail API first.

For receiving or sending the email using JavaMail API, you need to load the two jar files:

  • mail.jar
  • activation.jar

Steps for deleting the email using JavaMail API

There are total 5 steps for deleting the email. They are:

  1. Get the session object
  2. create the store object and connect to the current host
  3. create the folder object and open it
  4. Get the message to delete
  5. delete the message using setFlag method

Example of deleting email in Java

  1. import com.sun.mail.imap.protocol.FLAGS;  
  2. import java.io.*;  
  3. import java.util.*;  
  4. import javax.mail.*;  
  5. import javax.mail.internet.*;  
  6.   
  7. public class DeleteMail {  
  8.   
  9.  public static void main(String args[]) throws Exception {  
  10.   
  11.  String user= "sam@sampoint.com";//change accordingly  
  12.  String password="xxxxx";//change accordingly  
  13.   
  14.  //1) get the session object  
  15.  Properties properties = System.getProperties();  
  16.  Session session = Session.getDefaultInstance(properties);  
  17.   
  18.  //2) create the store object and connect to the current host   
  19.  Store store = session.getStore("pop3");  
  20.  store.connect("mail.javatpoint.com",user,password);  
  21.   
  22.  //3) create the folder object and open it  
  23.  Folder folder = store.getFolder("inbox");  
  24.   
  25.  if (!folder.exists()) {  
  26.  System.out.println("inbox not found");  
  27.  System.exit(0);  
  28.  }  
  29.   
  30.  folder.open(Folder.READ_WRITE);  
  31.   
  32.  //4) Get the message to delete  
  33.  Message[] msg = folder.getMessages();  
  34.   
  35.  //System.out.println((messages.length+1)+" message found");  
  36.  for (int i = 0; i < msg.length; i++) {  
  37.    System.out.println("--------- " + (i + 1) + "------------");  
  38.    String from = InternetAddress.toString(msg[i].getFrom());  
  39.    
  40.    if (from != null) {  
  41.      System.out.println("From: " + from);  
  42.    }  
  43.   
  44.    String replyTo = InternetAddress.toString(  
  45.    msg[i].getReplyTo());  
  46.    if (replyTo != null) {  
  47.     System.out.println("Reply-to: " + replyTo);  
  48.    }  
  49.   
  50.    String to = InternetAddress.toString(  
  51.    msg[i].getRecipients(Message.RecipientType.TO));  
  52.     
  53.    if (to != null) {  
  54.     System.out.println("To: " + to);  
  55.    }  
  56.   
  57.    String subject = msg[i].getSubject();  
  58.    if (subject != null) {  
  59.     System.out.println("Subject: " + subject);  
  60.    }  
  61.    Date sent = msg[i].getSentDate();  
  62.    if (sent != null) {  
  63.     System.out.println("Sent: " + sent);  
  64.    }  
  65.    System.out.println("Message : ");  
  66.    System.out.println(msg[i].getContent());  
  67.   
  68.  }//end of for loop  
  69.   
  70.   // get the message number to delete (optional)  
  71.   System.out.println("Enter message number to delete :");  
  72.   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
  73.   String no = br.readLine();  
  74.   //5) delete the message using setFlag method  
  75.   msg[Integer.parseInt(no) - 1].setFlag(FLAGS.Flag.DELETED, true);  
  76.     
  77.   System.out.println("Message Deleted .....");  
  78.   
  79.   folder.close(true);  
  80.   store.close();  
  81.   }  
  82. }    

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 DeleteMail.java
run by c:\> java DeleteMail