Program to demonstrate how to attach a file to a Email in Java using Java Mail API
Output of the program :
package com.hubberspot.email.examples; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import com.hubberspot.email.examples.SendEmail.LoginAuthenticator; import java.io.IOException; import java.util.Properties; import java.util.Scanner; public class SendAttachmentEmailDemo { public static void main(String[] args) { // Create a public Class SendAttachmentEmailDemo object // and call sendAttachmentEmail() on it ... SendAttachmentEmailDemo sendAttachmentEmailDemo = new SendAttachmentEmailDemo(); sendAttachmentEmailDemo.sendAttachmentEmail(); } private void sendAttachmentEmail() { // For establishment of email client with // Google's gmail use below properties. // For TLS Connection use below properties // Create a Properties object Properties props = new Properties(); // these properties are required // providing smtp auth property to true props.put("mail.smtp.auth", "true"); // providing tls enability props.put("mail.smtp.starttls.enable", "true"); // providing the smtp host i.e gmail.com props.put("mail.smtp.host", "smtp.gmail.com"); // providing smtp port as 587 props.put("mail.smtp.port", "587"); // For SSL Connection use below 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");*/ // Create Scanner object to take necessary // values from the user. Scanner scanner = new Scanner(System.in); System.out.println("Please provide your Username for Authentication ..."); final String Username = scanner.nextLine(); System.out.println("Please provide your Password for Authentication ..."); final String Password = scanner.nextLine(); System.out.println("Please provide Email Address from which you want to send Email ..."); final String fromEmailAddress = scanner.nextLine(); System.out.println("Please provide Email Address to which you want to send Email ..."); final String toEmailAddress = scanner.nextLine(); System.out.println("Please provide Subject for your Email ... "); final String subject = scanner.nextLine(); System.out.println("Please provide Text Message for your Email ... "); final String textMessage = scanner.nextLine(); System.out.println("Please provide the location of file to attach ..."); final String attachment = scanner.nextLine(); // Create a Session object based on the properties and // Authenticator object Session session = Session.getDefaultInstance(props, new LoginAuthenticator(Username,Password)); try { // Create a Message object using the session created above Message message = new MimeMessage(session); // setting email address to Message from where message is being sent message.setFrom(new InternetAddress(fromEmailAddress)); // setting the email address to which user wants to send message message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmailAddress)); // setting the subject for the email message.setSubject(subject); // Multipart messages contains different formats of files // images and text messages combined together in a email. // Such messages are also called as Multipurpose Internet Mail // Extensions (MIME) messages // Lets create first message body part , which will point // to our body of email message. Create a MimeBodyPart // object calling its setContent() method to set our // email body MimeBodyPart msgBodyPart = new MimeBodyPart(); msgBodyPart.setContent(textMessage, "text/plain"); // Create a MimeBodyPart object calling its attachFile() // method to enable email to sent attachments such as text files , images etc. MimeBodyPart attachmentBodypart = new MimeBodyPart(); attachmentBodypart.attachFile(attachment); // Create a Multipart object and assign the parts created above to it // using the addBodyPart() method. Multipart multipart = new MimeMultipart(); multipart.addBodyPart(msgBodyPart); multipart.addBodyPart(attachmentBodypart); // Setting the content of message to be the // multipart created above message.setContent(multipart); // Sending the attachment mail by Transport class send() message Transport.send(message); //System.out.println("\nYour Message delivered successfully ...."); } catch (MessagingException e) { throw new RuntimeException(e); } catch (IOException e) { e.printStackTrace(); } } // Creating a class for Username and Password authentication // provided by the user.java.hubberspot@gmail.com varyanidinesh@yahoo.com class LoginAuthenticator extends Authenticator { PasswordAuthentication authentication = null; public LoginAuthenticator(String username, String password) { authentication = new PasswordAuthentication(username,password); } @Override protected PasswordAuthentication getPasswordAuthentication() { return authentication; } } }
Output of the program :