menu


Subscribe to the blog

Social networks

My page on vk.com
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

lesnih.info


Russia

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: Email.java)

2. Create another class in this package Email: Send.java.

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: https://javaee.github.io/javamail/

And also you can read more about the API of this library here https://ru.wikipedia.org/wiki/JavaMail

And now, the code for the classes is: 

Email.java

package email;

public class Email {

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

 
 

Send.java

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("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.socketFactory.port", "465");
        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        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
            message.setSubject(subject_email);
           // The text of the letter itself
            message.setText(text_email);

          // we send the message
            Transport.send(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




Commets:







    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

    Views:266

    http request and read the html page Java

    http request and read the html page Java
    Topic: Programming

    Tags: java,http,html read

    Views:143

    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

    Views:339