Javatpoint Logo
Javatpoint Logo

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
download these jar files (or) go to the Oracle site to download the latest version.

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

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







Please Share

facebook twitter google plus pinterest tumbler

Learn Latest Tutorials