Tag Archives: commons-email

Sending E-Mail Using GMail SMTP via Apache Commons Emails

GMail provides a handy and reliable SMTP mail server for your program / script. Following are GMail SMTP configuration settings:

  • SMTP Host Name: smtp.gmail.com
  • SMTP Port: 587
  • TLS Enabled: Yes
  • Username:
  • Password:

In Java you can use commons-email to simply send an E-Mail using your GMail account.

First add commons-email jar into your classpath. If you use Maven, simply add following dependency (or newer version if any):


  org.apache.commons
  commons-email
  1.3.1

Following example assumes your GMail email is and password abcd1234.

Email email = new SimpleEmail();
email.setSmtpPort(587);
email.setHostName("smtp.gmail.com");
email.setAuthentication("", "abcd1234");
email.setStartTLSEnabled(true);
email.setFrom("", "John Doe");
email.setSubject("Hi this is testing email only");
email.setMsg("Hello there testing to send email from GMail");
email.addTo("");
email.send();

Note that by default GMail only allows email to be sent from your address (), you cannot send as somebody else for security reason. You need to perform additional configuration to allow external email to be sent via your GMail account.