How to Send Email With Attachment in Java


This shows how to send an email, with an attachment, using the JavaMail API.  Included is an explanation and example.

About JavaMail API

In Java, there are many APIs. JavaMail is one of the APIs that is used to compose, write, and read emails.
JavaMail API provides many facilities and it can be used in many events. It can be applied during the registration of the user, (giving acknowledgement for registering, or for logging the website to the user's email id), resending the password, or sending notifications related to updates, job alerts, etc.
The JavaMail API provides a protocol-independent, platform-independent framework ,for sending and receiving emails.
There are some packages that we will use in sending the email. The Javax.mail and javax.mail.activation packages contain core classes.  The javax.mail.internet package is also used, since we are sending mail through the internet.
The preceding two packages conatin the core classes.  Some of the classes are the following:
  • javax.mail.Session class
  • javax.mail.Message class
  • javax.mail.internet.MimeMessage class
  • javax.mail.Address class
  • javax.mail.Authenticator class
  • javax.mail.PasswordAuthentication class
  • javax.mail.internet.InternetAddress class
  • javax.mail.Transport class
  • javax.mail.Store class
  • and so on.

Example of sending an email with JavaMail API

For sending mail with an attachment, the JavaMail API provides useful classes like BodyPart, MimeBodyPart, MessageBodyPart, and so on.
There are two jar files that play very important roles in sending mail through the JavaMail API.  It is necessary to load the jar files.
  • Mail.jar
  • Activation.jar
I have attached these jar files, with this article, but you can also download these files from the Oracle website.
Now let's have a look towards the example and understand all of the packages and classes.
  1. import javax.mail.*;  
  2. import javax.activation.*;  
  3. import javax.mail.internet.*;  
  4. import java.util.*;  
  5. class SendAttachment {  
  6.  public static void main(String agrs[]) {  
  7.   String to = "[email protected]";  
  8.   final String user = "[email protected]";  
  9.   final String password = "xxxxxxx";  
  10.   Properties properties = System.getProperties();  
  11.   properties.setProperty("""");  
  12.   properties.put("mail.smtp.auth""true");  
  13.   Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() {  
  14.    protected PasswordAuthentication getPassworAuthentication() {  
  15.     return new PasswordAuthentication(user, password);  
  16.    }  
  17.   });  
  18.   try {  
  19.    MimeMessage message = new MimeMessage(session);  
  20.    message.setFrom(new InternetAddress(user));  
  21.    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  
  22.    message.setSubject("Message Alert");  
  23.    BodyPart messageBodyPart1 = new MimeBodyPart();  
  24.    messageBodyPart1.setText("This is the message body");  
  25.    MimeBodyPart messageBodyPart2 = new MimeBodyPart();  
  26.    String filename = "";  
  27.    DataSource source = new FileDataSource(filename);  
  28.    messageBodyPart2.setDataHandler(new DataHandler(source));  
  29.    messageBodyPart2.setFileName(filename);  
  30.    Multipart multipart = new MimeMultipart();  
  31.    multipart.addBodyPart(messageBodyPart1);  
  32.    multipart.addBodyPart(messageBodyPart2);  
  33.    message.setContent(multipart);  
  34.    Transport.send(message);  
  35.    System.out.println("Message has been sent");  
  36.   } catch (MessagingException ex) {  
  37.    ex.printStackTrace();  
  38.   }  
  39.  }  
  40. }   
You can observe the comments, in the code, that can be treated as the procedure to be followed in sending mail using JavaMail API.

Protocols used in JavaMail API

There are some protocols that are used in the JavaMail API.
There are some other protocols also, like IMAP, SMIME and so on.

A brief explanation of these protocols

  • SMTP
    This is the Simple Mail Transfer Protocol that provides the mechanism to deliver the mail.
  • POP
    This is the Post Office Protocol, also known as POP3, that provides the mechanism to receive the mail. It also provides support for the single mailbox of each user.
  • IMAP
    This is the Internet Message Access Protocol, which also provides a mechanism to receive mail and support of multiple mailboxes, for each user, that can be sharable with multiple users.
  • MIME 
    This is the Multiple Internet Mail Extension that tells the browser what is being sent, either attachment, the format of the message and so on. It is also known as the mail transfer protocol and is used by your mail program.
  • NNTP 
    This is the Network News Transfer Protocol that is responsible for the network through which mail is being sent.