get EMail Session - Java Network

Java examples for Network:EMail

Description

get EMail Session

Demo Code


//package com.java2s;
import javax.mail.*;

import java.util.Properties;

public class Main {
    private static Session getSession() {

        final String username = "from@gmail.com";
        final String password = "password";

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

        return Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }/*  w w  w. j a v a2 s  . co m*/
        });

    }
}

Related Tutorials