Sending email with attachment in Java

Example of sending attachment with email in Java provides examples of sending, receiving, forwarding and deleting emails, Learn how to send attachment in java

Sending email with attachment in Java

Sending email with attachment in Java

For sending email with attachment, JavaMail API provides some useful classes like BodyPart, MimeBodyPart etc.

For better understanding of this example, learn the steps of sending email using JavaMail API first.

For sending the email using JavaMail API, you need to load the two jar files:

  • mail.jar
  • activation.jar

Sending email with attachment using JavaMail API

There are total 7 steps for sending attachment with email. They are:

  1. Get the session object
  2. compose message
  3. create MimeBodyPart object and set your message text
  4. create new MimeBodyPart object and set DataHandler object to this object
  5. create Multipart object and add MimeBodyPart objects to this object
  6. set the multiplart object to the message object
  7. send message

Example of sending email with attachment in Java

  1. import java.util.*;  
  2. import javax.mail.*;  
  3. import javax.mail.internet.*;  
  4. import javax.activation.*;  
  5.   
  6. class SendAttachment{  
  7.  public static void main(String [] args){  
  8.   
  9.   String to="sam@gmail.com";//change accordingly  
  10.   final String user="sam@hpnmaratt.com";//change accordingly  
  11.   final String password="xxxxx";//change accordingly  
  12.    
  13.   //1) get the session object     
  14.   Properties properties = System.getProperties();  
  15.   properties.setProperty("mail.smtp.host""mail.javatpoint.com");  
  16.   properties.put("mail.smtp.auth""true");  
  17.   
  18.   Session session = Session.getDefaultInstance(properties,  
  19.    new javax.mail.Authenticator() {  
  20.    protected PasswordAuthentication getPasswordAuthentication() {  
  21.    return new PasswordAuthentication(user,password);  
  22.    }  
  23.   });  
  24.      
  25.   //2) compose message     
  26.   try{  
  27.     MimeMessage message = new MimeMessage(session);  
  28.     message.setFrom(new InternetAddress(user));  
  29.     message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
  30.     message.setSubject("Message Aleart");  
  31.       
  32.     //3) create MimeBodyPart object and set your message text     
  33.     BodyPart messageBodyPart1 = new MimeBodyPart();  
  34.     messageBodyPart1.setText("This is message body");  
  35.       
  36.     //4) create new MimeBodyPart object and set DataHandler object to this object      
  37.     MimeBodyPart messageBodyPart2 = new MimeBodyPart();  
  38.   
  39.     String filename = "SendAttachment.java";//change accordingly  
  40.     DataSource source = new FileDataSource(filename);  
  41.     messageBodyPart2.setDataHandler(new DataHandler(source));  
  42.     messageBodyPart2.setFileName(filename);  
  43.      
  44.      
  45.     //5) create Multipart object and add MimeBodyPart objects to this object      
  46.     Multipart multipart = new MimeMultipart();  
  47.     multipart.addBodyPart(messageBodyPart1);  
  48.     multipart.addBodyPart(messageBodyPart2);  
  49.   
  50.     //6) set the multiplart object to the message object  
  51.     message.setContent(multipart );  
  52.      
  53.     //7) send message  
  54.     Transport.send(message);  
  55.    
  56.    System.out.println("message sent....");  
  57.    }catch (MessagingException ex) {ex.printStackTrace();}  
  58.  }  

As you can see in the above example, total 7 steps are followed to send email with attachment. Now run this program by :

Load the jar file c:\> set classpath=mail.jar;activation.jar;.;
compile the source file c:\> javac SendAttachment.java
run by c:\> java SendAttachment