finalprivate static String usernameUSER_NAME = "username@gmail"Gmail Username"; // GMail user name (just the part before "@gmail.com";com")
finalprivate 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 = "password";USER_NAME;
String pass = PASSWORD;
String[] to = {
RECIPIENT
Properties prop}; // list of recipient email addresses
String subject = newsub;
Properties props = System.getProperties();
prop String host = "smtp.gmail.com";
props.put("mail.smtp.host"starttls.enable", "smtp"true");
props.gmailput("mail.com"smtp.host", host);
prop props.put("mail.smtp.port"user", "587"from);
prop props.put("mail.smtp.auth"password", "true"pass);
prop props.put("mail.smtp.starttlsport", "587");
props.enable"put("mail.smtp.auth", "true"); //TLS
Session session = Session.getInstancegetDefaultInstance(prop,props);
MimeMessage message = new javax.mail.AuthenticatorMimeMessage(session);
try {
protectedmessage.setFrom(new PasswordAuthenticationInternetAddress(from));
getPasswordAuthentication InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for (int i = 0; i < to.length; i++) {
returntoAddress[i] = new PasswordAuthenticationInternetAddress(username, passwordto[i]);
}
} for (int i = 0; i < toAddress.length; i++); {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
try { }
Message message.setSubject(subject);
BodyPart messageBodyPart = new MimeMessageMimeBodyPart(session);
message.setFrom(new InternetAddress("from@gmailString 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.com"));5vw\">" + title + "</h1>\n" + "<div align=\"center\">" +
message "<h2 style=\"text-align: center;font-size:1.setRecipients(0vw\">" + body + "</h2>" +
Message "<h3 style=\"text-align: center;text-decoration: underline;text-decoration-color: red;font-size:0.RecipientType9vw\">" +
under_line_text + "</h3><br><h4 style=\"text-align: center;font-size:0.TO,7vw\">" + end_text +
InternetAddress " </h4></div>";
messageBodyPart.parsesetContent("[email protected]htmlText, [email protected]""text/html");
Multipart multipart = new MimeMultipart();
// Set text message.setSubject("Testing Gmailpart
TLS" multipart.addBodyPart(messageBodyPart);
message.setTextsetContent("Dearmultipart);
Mail Crawler,"Transport +
transport = "\n\nsession.getTransport("smtp");
Please dotransport.connect(host, notfrom, spampass);
my email!"transport.sendMessage(message, message.getAllRecipients());
Transport transport.sendclose(message);
} catch (AddressException ae) {
System.out ae.printlnprintStackTrace("Done");
} catch (MessagingException eme) {
e me.printStackTrace();
}
return true;
}
Output:
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!