Sunday, September 15, 2019

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.   
  6. class SendAttachment{  
  7.  public static void main(String [] args){  
  8.   
  9.   String to="sonoojaiswal1987@gmail.com";//change accordingly  
  10.   final String user="sonoojaiswal@javatpoint.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.  }  
  59. }  

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 filec:\> set classpath=mail.jar;activation.jar;.;
compile the source filec:\> javac SendAttachment.java
run byc:\> java SendAttachment

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...