Send email in Java using Gmail smtp server

Today we will learn how to send email in Java using gmail. It’s very easy and it will be a very quick tutorial.

We are gonna use a library called Javax mail api. Below you can download the library based your project type.


If your project built on maven:

<!-- -->


If your project built on gradle:

implementation 'com.sun.mail:javax.mail:1.6.2'


If you like download jar file of this library:


Execute the code below in order to send email.

private void sendEmail(){

        Properties props = new Properties();
        props.put("mail.smtp.starttls.enable", true);
        props.put("mail.smtp.auth", true);
        props.put("", "");
        props.put("mail.smtp.port", 587);

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("Sender email address", "Password");

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(Constant.getSharedPreferences().getEmailAddress()));
                    InternetAddress.parse("receiver email address"));
            message.setSubject("your subject");
            message.setText("The body text here");

        } catch (Exception e) {
            Log.e("exception", e.getMessage());

Now you are done. Execute this code and email will be sent.
In next post we will learn how to send email in spring boot using freemarker.