Subscribe to the blog

Social networks

My page on
My page on Facebook
My page on Google plus
My page on Twitter

Main - Blog - Java mail - Send an email from a java application

Programming Date of creation: Views: 397

Java mail - Send an email from a java application


Java mail - Send email from java via Gmail

In order to send an email from a java application, you must place the appropriate code that will execute this command, and also make the settings:

1. Create a project in the IDE, for example with the name Email (Automatically creates an email package, and in it the main class:

2. Create another class in this package Email:

3. Load a library with a similar library name into the project  javax.mail-1.6.1.jar

If you have already downloaded the library into the project, then you do not need to load it again.

You can download the library here:

And also you can read more about the API of this library here

And now, the code for the classes is:

package email;

public class Email {

    public static void main(String[] args) {        
     String to_email = "...кому_отправляем"; // We write Email to whom we send 
     String subject_email = "Новое письмо?"; // Email Subject
     String mail_email = "Проверяем код"; // Email Text
     Send sslSender = new Send("", "....your_password_of_mail....");   
     sslSender.send(subject_email, mail_email, to_email);    
     System.out.print("Email sent");     

A comment:
This code can be adapted to other mail servers, not just Gmail.

package email;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class Send {
    String username_gmail;
    String password_gmail;
    Properties properties;

    public Send(String username_gmail, String password_gmail) {
        this.username_gmail = username_gmail;
        this.password_gmail = password_gmail;

        properties = new Properties();
        properties.put("", "");
        properties.put("mail.smtp.socketFactory.port", "465");
        properties.put("mail.smtp.socketFactory.class", "");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.port", "465");

    public void send(String subject_email, String text_email, String to_email){
        Session current_session = Session.getDefaultInstance(properties, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username_gmail, password_gmail);

        try {
            Message message = new MimeMessage(current_session);
            // Sender of the letter
            message.setFrom(new InternetAddress(username_gmail));
           // To whom we send the letter
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to_email));
            // Subject of the message
           // The text of the letter itself

          // we send the message
        } catch (MessagingException e) {
            throw new RuntimeException(e);

Java mail - Send an email from a java application
Rating: 5 | Тotal votes: 3 | Please, rate this article


    Similar articles:

    => How to send email to JSP Java

    Subscribe to the blog

    Random articles from the blog:

    Installing Apache Tomcat to work in Java JSP

    Installing Apache Tomcat to work in Java JSP
    Topic: Programming

    Tags: java,jsp,сервер,apache tomcat


    http request and read the html page Java

    http request and read the html page Java
    Topic: Programming

    Tags: java,http,html read


    Java XML - Reading XML in a java application

    Java XML - Reading XML in a java application
    Topic: Programming

    Tags: java,xml,reading xml,integration,exchange