Home » Example of sending email in Java through Gmail Server

Example of sending email in Java through Gmail Server

by Online Tutorials Library

Sending Email in Java through Gmail Server

We can send email by using the SMTP server of gmail. It is good if you are don’t have any SMTP server and reliable. Here we will learn how to send email through gmail server by SSL (Secured Socket Layer). SSL is basically used for security if you are sending email through gmail server.

For better understanding of this example, learn the steps of sending email using JavaMail API first.
For sending the email using JavaMail API, you need to load the two jar files:
  • mail.jar
  • activation.jar

download these jar files (or) go to the Oracle site to download the latest version.

Disable the antivirus e.g. avast etc. because it may interrupt your code to send emails.

Example of Sending Email through Gmail Server with SSL

As you can see in the above example, userid and password need to be authenticated. As, this program illustrates, you can send email easily but change the username and password accordingly. Let’s see how to run it once again by simple technique:

Load the jar file c:> set classpath=mail.jar;activation.jar;.;
compile the source file c:> javac SendMailSSL.java
run by c:> java SendMailSSL

Resolving AuthenticationFailedException

Click on this link and click on turn on radio button to allow users to send mail from unknown location. https://www.google.com/settings/security/lesssecureapps

You may also like