Skip to main content

Send a mail through core java by using Gmail authentication

In the current post, I am trying to explain how to send a mail through the core java program by using Gmail authentication. The below mentioned only one core program which will contain the source code of mail sender. In the below source you have to place your Gmail username, password and from address and to address.  

As we know mail.jar is not present in J2SE, we have to download externally and place it into CLASSPATH. Along with mail.jar download the below list of jars and add it into CLASSPATH to avoid compilation error.

  • dsn-1.5.5.jar
  • gimap-1.5.5.jar
  • imap-1.5.5.jar
  • javax.mail.jar
  • javax.mail-api-1.5.5.jar
  • logging-mailhandler-1.5.5.jar
  • mailapi-1.5.5.jar
  • pop3-1.5.5.jar
  • smtp-1.5.5.jar
GoogleMail.java
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class GoogleMail {

 public static void main(String... args) {
  final String username = "username@gmail.com";
  final String password = "password";

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

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

  try {

   Message finalMessage = new MimeMessage(session);
   finalMessage.setFrom(new InternetAddress("from@gmail.com"));
   finalMessage.setRecipients(Message.RecipientType.TO,
     InternetAddress.parse("to-mail@gmail.com"));
   finalMessage.setSubject("Testing Email");
   finalMessage.setText("Dear Crawler,"
     + "\n\n This mail your getting as part of testing, please ignore! ");

   Transport.send(finalMessage);

   System.out.println("COOOLLL....Successfully sent email to your TO address. Please verify it. :)");

  } catch (MessagingException e) {
   throw new RuntimeException(e);
  }
 }

}

Please clear if you have got any compilation errors and run the program to send an email. But, before running the program you make sure you have placed proper username, password, from address and to address.

Hurrreeeyyyyy………did you got an exception which is related to the Authentication. Now, you’re in the right way. You have to log in to your Gmail to modify authentication settings by clicking on this link.


When you will get login into your Gmail, it will show you the below image screen. Just you have to select the ‘Turn On’ radio button and logout from Gmail.


Then you run once again the GmailSender.java program to send an email. If there are no exceptions you are getting, you will get the below message on your console.

COOOLLL....Successfully sent email to your TO address. Please verify it. :)

If you got that message in your console, you login into the Gmail which you have placed into TO mail and verify you got the mail or not. 

Popular posts from this blog

JNDI configuration for Tomcat 9 with Oracle

In this article, I am going to place the required source code to get data from the table by using the JNDI configuration. Below are the environment details that I have configured currently. Windows - 7 Oracle - 10g Tomcat - 9 JDK - 8 Eclipse Oxygen Ojdbc6 jar required First, we need to create the Dynamic Web Project. If you don't know how to do <Click Here>. I have faced a lot of issues before getting the output like 405, No driver class to load, etc. I am using JSP & Servlets in the current explanation. Before started writing the application logic, we need to do the below configuration in the installed tomcat directory. Place OJDBC6.jar in the Tomcat LIB directory. Add this Resource under <GlobalNamingResources> in Server.xml file which is present under the conf directory in Tomcat. < Resource name = "jdbc/myoracle" global= "jdbc/myoracle" auth = "Container" type= "javax.sql.DataSource" driverClass...

Prime, Fibonacci and Factorial number with example in java

Prime number, Fibonacci series and Factorial number programs are most commonly asked questions in interview. Read this article to know what is and how to write programs for prime number, fibonacci series and factorial number. Prime Number: prime number is natural number greater than 1 that has no positive divisor other than 1 and itself. A natural number greater than 1 is not a prime number, is called Composite number . For example, 7 is a prime number. Because it can divide with 1 and 7 only. Where as 8 is composite number. Since it has the divisor 2 and 4 in addition to the 1 and 8. The below example represents the finding the passing number is prime number or not. If the passing number is prime number it will print true otherwise it will print false. package com . javatbrains . practice ; public class PrimeNumber { public boolean isPrimeNumber ( int number ) { if ( number <= 1 ) return false ; // There's only one ...

JVM, JRE and JDK in Java

JVM, JRE and JDK are the most basic common concepts to know in java. These are the basic features to understand how Java architecture works? JVM stands for Java Virtual Machine, which doesn't have any physical directories created in java installation. JRE stands for Java Runtime Environment, which creates the directory under Java installation path and also present in JDK. JDK stands for Java Development Kit, which creates the directory in Java installation path and also it has it's own JRE. Since we have already learn that Java is platform independent means if we have implemented any of the java class in one environment, it will be executed in any other environment and provides the same output. But, JVM, JRE and JDK all are platform dependent . So that, for windows, linux, unix, mac, solaris..etc has it's own JVM, JRE and JDK. One will be not compatible with other environments. While installing the Java, we might come to know a bit about JRE and JDK. But, JVM is the other...