TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Monday, 27 April 2020

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:

o    mail.jar
o    activation.jar

download image deleting email

Fig: Deleting Email



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

   import com.sun.mail.imap.protocol.FLAGS;  
   import java.io.*;  
   import java.util.*;  
   import javax.mail.*;  
   import javax.mail.internet.*;  
  
  public class DeleteMail {  
  
   public static void main(String args[]) throws Exception {  
  
 String user= "dwivedicyberdairy@completejavaschool.blogspot.com";//change accordingly  
   String password="xxxxx";//change accordingly  
  
    //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");  
   store.connect("mail.completejavaschool.blogspot.com",user,password);  
  
   //3) create the folder object and open it  
    Folder folder = store.getFolder("inbox");  
  
   if (!folder.exists()) {  
   System.out.println("inbox not found");  
   System.exit(0);  
   }  
  
   folder.open(Folder.READ_WRITE);  
  
   //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(  
   msg[i].getReplyTo());  
   if (replyTo != null) {  
    System.out.println("Reply-to: " + replyTo);  
   }  
  
   String to = InternetAddress.toString(  
   msg[i].getRecipients(Message.RecipientType.TO));  
    
   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 : ");  
   System.out.println(msg[i].getContent());  
  
   }//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(System.in  ));  
  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 .....");  
  
  folder.close(true);  
  store.close();  
  }  
}     

No comments:

Post a Comment