Skip to main content
Updated With Output
Source Link
Phill Alexakis
  • 1.5k
  • 1
  • 16
  • 32

You can use Gmail SMTP via TLSGmail SMTP via TLS

You can use Gmail SMTP via TLS

You can use Gmail SMTP via TLS

Updated With Output
Source Link
Phill Alexakis
  • 1.5k
  • 1
  • 16
  • 32
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:

enter image description here

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!

final String username = "username@gmail.com";
final String password = "password";

Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true"); //TLS

Session session = Session.getInstance(prop,
 new javax.mail.Authenticator() {
  protected PasswordAuthentication getPasswordAuthentication() {
   return new PasswordAuthentication(username, password);
  }
 });

try {

 Message message = new MimeMessage(session);
 message.setFrom(new InternetAddress("from@gmail.com"));
 message.setRecipients(
  Message.RecipientType.TO,
  InternetAddress.parse("[email protected], [email protected]")
 );
 message.setSubject("Testing Gmail TLS");
 message.setText("Dear Mail Crawler," +
  "\n\n Please do not spam my email!");
 
 Transport.send(message);

 System.out.println("Done");
 
} catch (MessagingException e) {
 e.printStackTrace();
}

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

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;
}

Output:

enter image description here

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!

Source Link
Phill Alexakis
  • 1.5k
  • 1
  • 16
  • 32

You can use Gmail SMTP via TLS

final String username = "[email protected]";
final String password = "password";

Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true"); //TLS

Session session = Session.getInstance(prop,
 new javax.mail.Authenticator() {
  protected PasswordAuthentication getPasswordAuthentication() {
   return new PasswordAuthentication(username, password);
  }
 });

try {

 Message message = new MimeMessage(session);
 message.setFrom(new InternetAddress("[email protected]"));
 message.setRecipients(
  Message.RecipientType.TO,
  InternetAddress.parse("[email protected], [email protected]")
 );
 message.setSubject("Testing Gmail TLS");
 message.setText("Dear Mail Crawler," +
  "\n\n Please do not spam my email!");

 Transport.send(message);

 System.out.println("Done");

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

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");

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