JavaMail API - SPLessons

JavaMail Deleting Email

Home > Lesson > Chapter 9
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JavaMail Deleting Email

JavaMail Deleting Email

shape Description

setFlag method is utilized to delete the email by using JavaMail API. There are two different types of flags available, they are user-defined and system-defined. Here by using Flags. Flag.DELETED approach deleting the email in JavaMail API and the pop protocol supports only the deleting of message. Following is the conceptual figure which describes more to deleting an email by using JavaMail API.

shape Conceptual figure

Follow the below flow steps for JavaMail Deleting Email. Here the process to delete an email involves session object, store object, folder, object, and setFalg() method.

shape Example

Following is an example which explains how to delete an email. [c]import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Properties; import javax.mail.Flags; import javax.mail.Folder; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.NoSuchProviderException; import javax.mail.Session; import javax.mail.Store; public class DeleteEmail { public static void delete(String pop3Host, String storeType, String user, String password) { try { // To get the session object Properties properties = new Properties(); properties.put("mail.store.protocol", "pop3"); properties.put("mail.pop3s.host", pop3Host); properties.put("mail.pop3s.port", "995"); properties.put("mail.pop3.starttls.enable", "true"); Session emailSession = Session.getDefaultInstance(properties); // emailSession.setDebug(true); // creating the POP3 store object and connect with the pop server Store store = emailSession.getStore("pop3s"); store.connect(pop3Host, user, password); // creating the folder object and open it Folder emailFolder = store.getFolder("INBOX"); emailFolder.open(Folder.READ_WRITE); BufferedReader reader = new BufferedReader(new InputStreamReader( System.in)); // To retrieve the messages from the folder in an array and print it Message[] messages = emailFolder.getMessages(); System.out.println("messages.length-" + messages.length); for (int i = 0; i < messages.length; i++) { Message message = messages[i]; System.out.println("---------------------------------"); System.out.println("Email Number " + (i + 1)); System.out.println("Subject: " + message.getSubject()); System.out.println("From: " + message.getFrom()[0]); String subject = message.getSubject(); System.out.print("Do you want to delete this message [y/n] ? "); String ans = reader.readLine(); if ("Y".equals(ans) || "y".equals(ans)) { // To set the DELETE flag to true message.setFlag(Flags.Flag.DELETED, true); System.out.println("Marked DELETE for message: " + subject); } else if ("n".equals(ans)) { break; } } // expunges the folder to remove messages which are marked deleted emailFolder.close(true); store.close(); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } catch (IOException io) { io.printStackTrace(); } } public static void main(String[] args) { String host = "pop.gmail.com";// change accordingly String mailStoreType = "pop3"; String username = "venkatanaresh35@gmail.com";// change accordingly String password = "*****";// change accordingly delete(host, mailStoreType, username, password); } } [/c] Output When compile the program following output will be come in the console.

Summary

shape Key Points

  • JavaMail Deleting Email - In JavaMail API deleting the email using setFlag method.
  • JavaMail Deleting Email - There are two different types of flags available, they are user-defined and system-defined.
  • JavaMail Deleting Email - Here by using Flags.Flag.DELETED approach deleting the email in JavaMail API.
  • JavaMail Deleting Email - The pop protocol supports the deleting of message.
  • JavaMail Deleting Email - Here the process to delete an email involves session object, store object, folder, object, and setFalg() method.