You can use Gmail SMTP via TLS
private static String USER_NAME = "Gmail Username"; // GMail user name (just the part before "@gmail.com")
private static String PASSWORD = "gbxxfgfhujdfqndd"; // GMail password
public static boolean sendEmail(String RECIPIENT, String sub, String title, String body, String under_line_text,
String end_text) {
String from = USER_NAME;
String pass = PASSWORD;
String[] to = {
RECIPIENT
}; // list of recipient email addresses
String subject = sub;
Properties props = System.getProperties();
String host = "smtp.gmail.com";
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for (int i = 0; i < to.length; i++) {
toAddress[i] = new InternetAddress(to[i]);
}
for (int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<div style=\" background-color: white;width: 25vw;height:auto;border: 20px solid grey;padding: 50px;margin:100 auto;\">\n" +
"<h1 style=\"text-align: center;font-size:1.5vw\">" + title + "</h1>\n" + "<div align=\"center\">" +
"<h2 style=\"text-align: center;font-size:1.0vw\">" + body + "</h2>" +
"<h3 style=\"text-align: center;text-decoration: underline;text-decoration-color: red;font-size:0.9vw\">" +
under_line_text + "</h3><br><h4 style=\"text-align: center;font-size:0.7vw\">" + end_text +
" </h4></div>";
messageBodyPart.setContent(htmlText, "text/html");
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (AddressException ae) {
ae.printStackTrace();
} catch (MessagingException me) {
me.printStackTrace();
}
return true;
}
Or Gmail via SSL in which you should add
//change port number
prop.put("mail.smtp.port", "465");
prop.put("mail.smtp.socketFactory.port", "465");
prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Output:
![enter image description here](https://cdn.statically.io/img/i.sstatic.net/YQDEC.jpg)
Also you can change BodyPart
accordingly i have included an html
responsive template
I recommend you to visit your Google Account and generate a new Application Password , this allows your password field to be encoded and not used as plain text
Otherwise you will come up with this exception
javax.mail.AuthenticationFailedException: 534-5.7.9 Application-specific password required.
Hope it helped!