We can send email with attachment using Java Mail API library from Gmail.
Here is the very basic program to explain How to send email with attachment using Java Mail API library from Gmail.
Detailed explanation for the above program is as follows:
Following code is the required packages for Java and Java Mail API library.
Following code is to declare required variable for username, password, to, cc, bcc, etc.
Following code is to setup the Gmail server with SMTP details.
Following code is to get the session of Gmail account using provided username and password.
Following code is to set from email address of email message.
Following code is to set to email address of email message.
Following code is to set cc email address of email message.
Following code is to set bcc email address of email message.
Following code is to set subject of email message.
Following code is to set message text of email message.
Following code is to set attachment of email message.
Following code is to send email message.
Here is the very basic program to explain How to send email with attachment using Java Mail API library from Gmail.
Java Source code:
package com.helloselenium.selenium.test; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.*; public class SendEMailUsingJavaMailFromGmail{ public static void main(String[] args) { final String username = "testusername@gmail.com"; //change to your Gmail username final String password = "testpassword"; //change to your Gmail password final String from = "test.from.email@helloselenium.com"; //change to from email address final String to = "test.to.email@helloselenium.com"; //change to to email address final String cc = "test.cc.email@helloselenium.com"; //change to cc email address final String bcc = "test.bcc.email@helloselenium.com"; //change to bcc email address final String subject = "Test Email from Hello Selenium"; //change to your subject final String msg = "Test Email from Hello Selenium to learn the automation of email message sending using Java Mail API from Gmail."; //change to your message Properties props = new Properties(); props.put("mail.smtp.auth", true); props.put("mail.smtp.starttls.enable", true); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); //below code only requires if your want cc email address message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc)); //below code only requires if your want bcc email address message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc)); message.setSubject(subject); message.setText(msg); MimeBodyPart messageBodyPart = new MimeBodyPart(); Multipart multipart = new MimeMultipart(); messageBodyPart = new MimeBodyPart(); String file1 = "drive:\\folder1\\folder2\\file.txt"; //change to your attachment filepath String fileName1 = "file.txt"; //change to your attachment filename DataSource source1 = new FileDataSource(file1); messageBodyPart.setDataHandler(new DataHandler(source1)); messageBodyPart.setFileName(fileName1); multipart.addBodyPart(messageBodyPart); message.setContent(multipart); System.out.println("Sending"); Transport.send(message); System.out.println("Done"); } catch (MessagingException e) { e.printStackTrace(); } } }
Detailed explanation for the above program is as follows:
Following code is the required packages for Java and Java Mail API library.
import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.*;
final String username = "testusername@gmail.com"; //change to your Gmail username final String password = "testpassword"; //change to your Gmail password final String from = "test.from.email@helloselenium.com"; //change to from email address final String to = "test.to.email@helloselenium.com"; //change to to email address final String cc = "test.cc.email@helloselenium.com"; //change to cc email address final String bcc = "test.bcc.email@helloselenium.com"; //change to bcc email address final String subject = "Test Email from Hello Selenium"; //change to your subject final String msg = "Test Email from Hello Selenium to learn the automation of email message sending using Java Mail API from Gmail."; //change to your message
Properties props = new Properties(); props.put("mail.smtp.auth", true); props.put("mail.smtp.starttls.enable", true); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } });
Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
Following code is to set cc email address of email message.
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc));
message.setSubject(subject);
message.setText(msg);
MimeBodyPart messageBodyPart = new MimeBodyPart(); Multipart multipart = new MimeMultipart(); messageBodyPart = new MimeBodyPart(); String file1 = "drive:\\folder1\\folder2\\file.txt"; //change to your attachment filepath String fileName1 = "file.txt"; //change to your attachment filename DataSource source1 = new FileDataSource(file1); messageBodyPart.setDataHandler(new DataHandler(source1)); messageBodyPart.setFileName(fileName1); multipart.addBodyPart(messageBodyPart); message.setContent(multipart);
Transport.send(message);
0 Comments
What would you like to add in my list? I look forward to reading your comments below.