<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>As we receive the email, we can receive the attachment also by using Multipart and BodyPart classes.<br />
For better understanding of this example, learn the steps of sending email using JavaMail API first.</p>
<p>For receiving or sending the email using JavaMail API, you need to load the two jar files:</p>
<ul style="text-align: left;">
<li>mail.jar</li>
<li>activation.jar</li>
</ul>
<h2><b>Example of receiving email with attachment using JavaMail API</b></h2>
<pre class="highlight">import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.*; 
import javax.activation.*; 
import java.io.*; 
public class ReadMailAttachment{ 
 
 public static void receiveEmail(String pop3Host, String storeType, 
 final String user, final String password) { 
 try { 
 //1) get the session object 
 Properties properties = new Properties(); 
 properties.put("mail.store.protocol", "imaps"); 
 //Session emailSession = Session.getDefaultInstance(properties); 
 Session emailSession = Session.getDefaultInstance(properties, 
 new javax.mail.Authenticator() { 
 protected PasswordAuthentication getPasswordAuthentication() { 
 return new PasswordAuthentication(user,password); 
 } 
 }); 
 //2) create the POP3 store object and connect with the pop server 
 Store emailStore = emailSession.getStore("imaps"); 
 emailStore.connect("imap.gmail.com","dineshonjava@gmail.com", password); 
 
 
 //3) create the folder object and open it 
 Folder emailFolder = emailStore.getFolder("INBOX"); 
 emailFolder.open(Folder.READ_WRITE); 
 // folder.open(Folder.READ_WRITE); 
 //4) retrieve the messages from the folder in an array and print it 
 Message[] messages = emailFolder.getMessages(); 
 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]); 
 System.out.println("Text: " + message.getContent().toString()); 
 System.out.println("Sent Date: " +message.getSentDate()); 
 
 Multipart multipart = (Multipart) message.getContent(); 
 
 for (int j = 0; j <; multipart.getCount(); j++) { 
 BodyPart bodyPart = multipart.getBodyPart(j); 
 InputStream stream = bodyPart.getInputStream(); 
 BufferedReader br = new BufferedReader(new InputStreamReader(stream)); 
 
 while (br.ready()) { 
 System.out.println(br.readLine()); 
 } 
 System.out.println(); 
 } 
 } 
 //5) close the store and folder objects 
 emailFolder.close(false); 
 emailStore.close(); 
 
 } catch (NoSuchProviderException e) {e.printStackTrace();} 
 catch (MessagingException e) {e.printStackTrace();} 
 catch (IOException e) {e.printStackTrace();} 
 } 
 
 public static void main(String[] args) { 
 
 String host = "smtp.gmail.com";//change accordingly 
 String mailStoreType = "pop3"; 
 final String username= "dineshonjava@gmail.com"; 
 final String password= "******";//change accordingly 
 
 receiveEmail(host, mailStoreType, username, password); 
 
 } 
} 
</pre>
<p> ;</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2014/01/1111-11.png" border="0" /></div>
</div>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/sending-email-with-attachments-using/">Previous</a> <;<; || <a href="https://dineshonjava.com/java-mail-api-tutorial/">Index </a>|| >;>;<a href="https://dineshonjava.com/javamail-api-sending-html-email/" target="_blank" rel="noopener">Next</a> >;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/sending-email-with-attachments-using/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/javamail-api-sending-html-email/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '244'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…