minte9
LearnRemember



SETUP

JavaMail API is an optional package for Java SE (it is included in Java EE). JavaBeans Activation Framework is also an extension, not part of the Java core API.
 
Add libraries:
    - lib/javax.mail.jar
    - lib/activation.jar

Add /lib folder to source path

HOST

JavaMail API provides everything you need to send emails. The only property you have to set in order to send mail is mail.smtp.host
 
import java.util.Properties;

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

class App {

    public static void main(String[] args) {
        
        // Set SMTP server
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.mail.yahoo.com");
        Session session = Session.getDefaultInstance(props, null);

        // Set message
        String to = "myuser@yahoo.com";
        String from = "myuser@yahoo.com";
        String subject = "Java Mail Api - test";
        Message msg = new MimeMessage(session);
        try{
            msg.setFrom(new InternetAddress(from));
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            msg.setSubject(subject);
            msg.setText("Hello JavaMail Api!");
            
            // Send message
            Transport.send(msg);
            System.out.println("Message sent");
        } catch (MessagingException e) {e.printStackTrace();}
    }
}

AUTH

Most of the time, you do not need to provide username and password to send email ... when using your local SMTP server.
 
import java.util.Properties;

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

class App {

    public static void main(String[] args) {
        
        // Set SMTP server - Simple (1)
        /*
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.mail.yahoo.com");
        Session session = Session.getDefaultInstance(props, null);
        */

        // Set SMTP server - Auth (2)
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.mail.yahoo.com");
        props.put("mail.smtp.port", "587"); // TSL (465 SSLis deprecated)
        props.put("mail.smtp.starttls.enable", "true"); // TSL
        props.put("mail.smtp.connectiontimeout", "10000"); // 10s
        props.put("mail.smtp.timeout", "10000");
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "true");   
        Session session = Session.getInstance(props, 
                        new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("myuser", "xxxxxxxxxxx");
                    // yahoo account / security / generate app password
            }
        });

        // Set message
        String to = "myuser@yahoo.com";
        String from = "myuser@yahoo.com";
        String subject = "Java Mail Api - test";
        Message msg = new MimeMessage(session);
        try{
            msg.setFrom(new InternetAddress(from));
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            msg.setSubject(subject);
            msg.setText("Hello JavaMail Api!");
            
            // Send message
            Transport.send(msg);
            System.out.println("Message sent");
        } catch (MessagingException e) {e.printStackTrace();}
    }
}



  Last update: 303 days ago