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

Receiving Email With Attachment In Java

Receiving email with attachment in Java

As we receive the email, we can receive the attachment also by using Multipart and BodyPart classes found in JavaMail API.

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 image receiving email with attachment

Fig: Receiving Email With Attachment  

Example of receiving email with attachment in Java

   import java.util.*;  
   import javax.mail.*;  
   import javax.mail.internet.*;  
  import javax.activation.*;  
   import java.io.*;  
  
  class ReadAttachment{  
  public static void main(String [] args)throws Exception{  
     
   String host="mail.javatpoint.com";  
   final String user="completejavaschool.blogspot.com";  
   final String password="xxxxx";//change accordingly  
  
   Properties properties = System.getProperties();  
   properties.setProperty("mail.smtp.host",host );  
   properties.put("mail.smtp.auth", "true");  
  
   Session session = Session.getDefaultInstance(properties,  
    new javax.mail.Authenticator() {  
    protected PasswordAuthentication getPasswordAuthentication() {  
     return new PasswordAuthentication(user,password);  
    }  
   });  
        
     Store store = session.getStore("pop3");  
     store.connect(host,user,password);  
  
     Folder folder = store.getFolder("inbox");  
     folder.open(Folder.READ_WRITE);  
  
     Message[] message = folder.getMessages();  
  
  
  for (int a = 0; a < message.length; a++) {  
    System.out.println("-------------" + (a + 1) + "-----------");  
    System.out.println(message[a].getSentDate());  
  
    Multipart multipart = (Multipart) message[a].getContent();  
   
    for (int i = 0; i < multipart.getCount(); i++) {  
     BodyPart bodyPart = multipart.getBodyPart(i);  
     InputStream stream = bodyPart.getInputStream();  
     BufferedReader br = new BufferedReader(new InputStreamReader(str    eam));  
  
      while (br.ready()) {  
       System.out.println(br.readLine());  
      }  
     System.out.println();  
    }  
   System.out.println();  
  }  
  
  folder.close(true);  
  store.close();  
  }  
}     

 


No comments:

Post a Comment