Email with attachment

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

download these jar files (or) go to the Oracle site to download the latest version.

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s