Skip to main content

HOW TO SEND EMAIL WITH ATTACHMENT IN JAVA APPLICATION

Simple application to send an email using java application

Example :

  1. import javax.activation.DataHandler;
  2. import javax.activation.FileDataSource;
  3. import javax.mail.*;
  4. import javax.mail.internet.InternetAddress;
  5. import javax.mail.internet.MimeBodyPart;
  6. import javax.mail.internet.MimeMessage;
  7. import javax.mail.internet.MimeMultipart;
  8. import java.util.Date;
  9. import java.util.Properties;
  10.  
  11. public class EmailAttachmentDemo {
  12.      public static void main(String[] args) {
  13.         EmailAttachmentDemo demo = new EmailAttachmentDemo();
  14.         demo.sendEmail();
  15.     } 
  16.     public void sendEmail() {
  17.         String from = "email@example.com";
  18.         String to = "email@example.com";
  19.         String subject = "Important Message";
  20.         String bodyText = "This is a important message with attachment";
  21.         String filename = "jee-sample/src/main/resources/data.txt";
  22.  
  23.         Properties properties = new Properties();
  24.         properties.put("mail.smtp.host", "smtp.example.com");
  25.         properties.put("mail.smtp.port", "25");
  26.         Session session = Session.getDefaultInstance(properties, null);
  27.  
  28.         try {
  29.             MimeMessage message = new MimeMessage(session);
  30.             message.setFrom(new InternetAddress(from));
  31.             message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
  32.             message.setSubject(subject);
  33.             message.setSentDate(new Date());
  34.  
  35.             // Set the email message text.
  36.            
  37.             MimeBodyPart messagePart = new MimeBodyPart();
  38.             messagePart.setText(bodyText);
  39.  
  40.             // Set the email attachment file
  41.             
  42.             MimeBodyPart attachmentPart = new MimeBodyPart();
  43.             FileDataSource fileDataSource = new FileDataSource(filename) {
  44.                 @Override
  45.                 public String getContentType() {
  46.                     return "application/octet-stream";
  47.                 }
  48.             };
  49.             attachmentPart.setDataHandler(new DataHandler(fileDataSource));
  50.             attachmentPart.setFileName(fileDataSource.getName());
  51.  
  52.             Multipart multipart = new MimeMultipart();
  53.             multipart.addBodyPart(messagePart);
  54.             multipart.addBodyPart(attachmentPart);
  55.  
  56.             message.setContent(multipart);
  57.  
  58.             Transport.send(message);
  59.         } catch (MessagingException e) {
  60.             e.printStackTrace();
  61.         }
  62.     }
  63.  }

Description:

Step 1 - 9 : All files are simply imported.
Step 11: Class is defined.
Step 12 -15: Here object of class is created and "sendEmail()" is called.
Step 18 - 22: Variables themselves describe their role.
Step 16: Here starts the "sendEmail()"