JavaMail API – Sending email via Gmail SMTP

                      How to send a mail through SMTP server using java programming, it's bulk process to send test cases(Screenshots, description. text ) to many emails by manually so using JavaMail API it can be possible to send many users at a time.

This article will show you how to send a mail through SMTP server using java programming and follow below steps.

1. Create Maven Project
2. For sending the email using JavaMail API, you need to load the two jar files:
  • mail.jar
  • activation.jar
Download these zip file

3. Project structure



MonitoringMail.java

package com.nbos.utility;

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MonitoringMail {

// public static void sendMail(String mailServer, String from,String
// username, String password,String port, String[] to, String subject,
// String messageBody, String text) throws
// MessagingException, AddressException

public void sendMail(String mailServer, String from, String[] to, String subject, String messageBody, String text)
throws MessagingException, AddressException {
boolean debug = false;
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.EnableSSL.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", mailServer);
props.put("mail.debug", "true");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");

Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);

session.setDebug(debug);

try {

Transport bus = session.getTransport("smtp");
bus.connect();
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] addressTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++)
addressTo[i] = new InternetAddress(to[i]);
message.setRecipients(Message.RecipientType.TO, addressTo);
message.setSubject(subject);

            BodyPart body = new MimeBodyPart();
            body.setText(text);
            MimeMultipart multipart = new MimeMultipart();
            multipart.addBodyPart(body);
            message.setContent(multipart);
Transport.send(message);
System.out.println("Sucessfully Sent mail to All Users");
bus.close();

} catch (MessagingException mex) {
mex.printStackTrace();
}
}

private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = TestConfig.from;
String password = TestConfig.password;
return new PasswordAuthentication(username, password);
}
}
}

TestConfig.java

package com.nbos.utility;

public class TestConfig {
public static String server = "smtp.gmail.com";
public static String from = "kumar.abdasu5912@gmail.com";
public static String password = "*********";
public static String[] to = { "kumar5912@gmail.com", "abdasukumar@gmail.com" };
public static String subject = "Test Report";
public static String messageBody = "TestMessage";
public static String text = "Hi Hello";
}

TestMail.java

package com.nbos.JavaMail;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import com.nbos.utility.MonitoringMail;
import com.nbos.utility.TestConfig;

public class TestMail {
public static void main(String[] args) throws AddressException, MessagingException {
MonitoringMail mail = new MonitoringMail();
mail.sendMail(TestConfig.server, TestConfig.from, TestConfig.to, TestConfig.subject, TestConfig.messageBody, TestConfig.text);
}
}

IMP: Before running the script we need to enable one option, follow below steps.

Note: This is only for FROM Mail address. Where we can send emails FROM and TO addresses.

Click on My account > Sign-in & security
Scroll down the page and then we should enable this " Allow less secure apps: ON " option otherwise, we will get exceptions.


One more point is will see this option only for new emails, not for old emails. it means email should be created recently.

Note: All set for running the code, right click on " TestMail.java " and run as JavaApplication.

You have successfully sent the mail.

Thanks for reading this article!

Comments

Popular posts from this blog

ReportPortal With Selenium, TestNG

Appium - Getting android app Package name and appActivity

Native Apps - Inspect elements by using UI Automator Viewer and Appium Desktop Inspector