JavaMail API - SPLessons

JavaMail Send Attachment

Home > Lesson > Chapter 6
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JavaMail Send Attachment

JavaMail Send Attachment

shape Introduction

JavaMail Send Attachment, The JavaMail API is the concept related to email through the JavaMail. There are so many ways to send an email using JavaMail, but here by using SMTP server should send the emails.JavaMail API also provides core classes for to define objects and used that objects to maintain a mail system. By making use of MineBodyPart, BodyPart classes from JavaMail API sending electronic mail with attachment is possible. The sending email with attachment is similar to sending email but here the additional functionality is with message sending a file or document by making use of MineBodyPart, BodyPart classes.

shape Conceptual figure

Steps to send email with attachments: JavaMail Send Attachment, The process for sending mail with attachment involves session object, MineBody, MultiPart objects. Here the MineBody is used to set the text message and it is carried by MultiPart object. Because of MultiPart object here sending attachment.

shape Examples

Let us see the example how to send email with attachments. [c] package firstmail; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class sendingattachments { public static void main(String[] args) { // Recipient's email ID needs to be mentioned. String to = "9saireddy@gmail.com"; // Sender's email ID needs to be mentioned String from = "9saireddy@gmail.com"; final String username = "9saireddy@gmail.com";//change accordingly final String password = "splessons";//change accordingly // Assuming you are sending email through relay.jangosmtp.net Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); // Get the Session object. Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { // Create a default MimeMessage object. Message message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); // Set Subject: header field message.setSubject("sendinngattachments.java"); // Create the message part BodyPart messageBodyPart = new MimeBodyPart(); // Now set the actual message messageBodyPart.setText("hey u, follwong is the message find it."); // Create a multipar message Multipart multipart = new MimeMultipart(); // Set text message part multipart.addBodyPart(messageBodyPart); // Part two is attachment messageBodyPart = new MimeBodyPart(); String filename = "E:/SaiReddy-CV.PDF"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); // Send the complete message parts message.setContent(multipart); // Send message Transport.send(message); System.out.println("Sent message successfully...."); } catch (MessagingException e) { throw new RuntimeException(e); } } }[/c] Output When compile the program following is the output will be displayed in the console. [c]Sent message successfully....[/c] Now check the mail. Open the mail and find the attachment.

Summary

shape Key Points

  • JavaMail Send Attachment - By making use of MineBodyPart, BodyPart classes from JavaMail API sending electronic mail with attachment is possible.
  • JavaMail Send Attachment - The process for sending mail with attachment involves session object, MineBody, MultiPart objects.
  • JavaMail Send Attachment - Here the MineBody is used to set the text message and it is carried by MultiPart object.