Easy learning with example program codes

Send simple email through Gmail server using TLS connection

We are explaining here an example of sending simple email through gmail server with TLS connection using JavaMail API.

Steps of sending simple email through gmail server using JavaMail API:

    1. Get a session instance from getDefaultInstance() or getInstance() method of Session class.
    2. Create a message we have to pass session object in MimeMessage class constructor.
    3. Use the InternetAddress class to set the information of sender and recipient.
    4. Complose the message by using MimeMessage class methods like setFrom(Address address), addRecipients(Message.RecipientType type, String addresses), setSubject(String subject), setText(String textmessage) etc.
    5. Use send() method of javax.mail.Transport class to send the message.


import java.util.*;  
import javax.mail.*;  
import javax.mail.internet.*;  
 * This class is used to send simple email 
 * via Gmail server using SSL connection. 
 *   @author codesjava
public class SendEmailThroughGmail { 
final String senderEmailId = "";
final String senderPassword = "****";
final String emailSMTPserver = "";
final String emailSMTPPort = "465";
public SendEmailThroughGmail(String receiverEmail, 
		String subject, String messageText) {
	Properties props = new Properties();
	props.put("", emailSMTPserver);
	props.put("mail.smtp.socketFactory.port", emailSMTPPort);
	props.put("mail.smtp.auth", "true");
	props.put("mail.smtp.port", emailSMTPPort);
  try { 			
	Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getInstance(props, auth);
	Message message = new MimeMessage(session);
	message.setFrom(new InternetAddress(senderEmailId));
	System.out.println("Email send successfully."); 
  } catch (Exception e) {
    System.err.println("Error in sending email.");
private class SMTPAuthenticator extends 
 javax.mail.Authenticator {
    public PasswordAuthentication 
     getPasswordAuthentication() {
        return new PasswordAuthentication(senderEmailId, 
public static void main(String[] args) {
	new SendEmailThroughGmail("", 
		"Test Email", "Hi,\n\n This is a test email via " +
				"Gmail server using SSL connection.");


Email send successfully.

Download this example.
Next Topic: Send email through Gmail server using SSL connection with example.
Previous Topic: Send inline image with example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.


Sign Up/ Sign In
Ask a Question

Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2018 CodesJava Protection Status SiteMap Reference: Java Wiki