Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Deleting email


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

  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. public class DeleteMail {
  7.  public static void main(String args[]) throws Exception {
  8.  String user= “sonoojaiswal@Kreationnext.com”;//change accordingly
  9.  String password=“xxxxx”;//change accordingly
  10.  //1) get the session object
  11.  Properties properties = System.getProperties();
  12.  Session session = Session.getDefaultInstance(properties);
  13.  //2) create the store object and connect to the current host 
  14.  Store store = session.getStore(“pop3”);
  15.  store.connect(“mail.Kreationnext.com”,user,password);
  16.  //3) create the folder object and open it
  17.  Folder folder = store.getFolder(“inbox”);
  18.  if (!folder.exists()) {
  19.  System.out.println(“inbox not found”);
  20.  System.exit(0);
  21.  }
  22.  folder.open(Folder.READ_WRITE);
  23.  //4) Get the message to delete
  24.  Message[] msg = folder.getMessages();
  25.  //System.out.println((messages.length+1)+” message found”);
  26.  for (int i = 0; i < msg.length; i++) {
  27.    System.out.println(“——— “ + (i + 1) + “————“);
  28.    String from = InternetAddress.toString(msg[i].getFrom());
  29.    if (from != null) {
  30.      System.out.println(“From: “ + from);
  31.    }
  32.    String replyTo = InternetAddress.toString(
  33.    msg[i].getReplyTo());
  34.    if (replyTo != null) {
  35.     System.out.println(“Reply-to: “ + replyTo);
  36.    }
  37.    String to = InternetAddress.toString(
  38.    msg[i].getRecipients(Message.RecipientType.TO));
  39.    if (to != null) {
  40.     System.out.println(“To: “ + to);
  41.    }
  42.    String subject = msg[i].getSubject();
  43.    if (subject != null) {
  44.     System.out.println(“Subject: “ + subject);
  45.    }
  46.    Date sent = msg[i].getSentDate();
  47.    if (sent != null) {
  48.     System.out.println(“Sent: “ + sent);
  49.    }
  50.    System.out.println(“Message : “);
  51.    System.out.println(msg[i].getContent());
  52.  }//end of for loop
  53.   // get the message number to delete (optional)
  54.   System.out.println(“Enter message number to delete :”);
  55.   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  56.   String no = br.readLine();
  57.   //5) delete the message using setFlag method
  58.   msg[Integer.parseInt(no) – 1].setFlag(FLAGS.Flag.DELETED, true);
  59.   System.out.println(“Message Deleted …..”);
  60.   folder.close(true);
  61.   store.close();
  62.   }
  63. }
download this example to delete the email

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

Comments

comments