Sending email in Java using Gmail, Yahoo, Hotmail and so on

In this tutorial, I’ve created simple maven web application to send email using gmail or any other mail. I’ve used JSF for frontend. Make sure you’ve installed glassfish server before beginning. You can download and install glassfish server from this.

When you’re done, you’ll get something like this :


First of all, create a simple maven web application EmailWithJSF. Then in pom.xml include the following dependency. The dependency is required for sending email in java.


Now create a jsf page named index.xhtml and insert the following code:


<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns=""
        <title>Sending Email using Gmail, Hotmail, yahoo...</title>
                <p>Sender From : <h:inputText value ="#{bean.user.from}"/></p>
                <p>Password : <h:inputSecret value ="#{bean.user.password}"/></p>
                <p>Send To : <h:inputText value ="#{}"/></p>
                <p>Subject : <h:inputText value ="#{bean.user.sub}"/></p>
                <p>Message : <h:inputText value ="#{bean.user.msg}"/></p>

                <h:commandButton value="submit" action="#{bean.send()}"/>

After that create a landing page i.e success.xhtml.

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns=""
        <title>Message Sent</title>
        Message Sent Successfully.

Inside WEB-INF, make sure your web.xml look like this

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-name>Faces Servlet</servlet-name>

Now include the following java classes, and

public class User {

    private String from;
    private String password;
    private String to;
    private String sub;
    private String msg;

    public User() {

    public User(String from, String password, String to, String sub, String msg) {
        this.from = from;
        this.password = password; = to;
        this.sub = sub;
        this.msg = msg;

    //getter and setters


import java.util.Properties;
import javax.faces.bean.ManagedBean;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

@ManagedBean(name = "bean")
public class EmailBean {

    private String file;

    public String getFile() {
        return file;

    public void setFile(String file) {
        this.file = file;

    User user = new User();

    public User getUser() {
        return user;

    public void setUser(User user) {
        this.user = user;

    public String send() {
//Get properties object
        System.out.println("Send button click vayo");
        Properties props = new Properties();
        props.put("", "");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
//get Session
        Session session = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user.getFrom(), user.getPassword());
//compose message
        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(user.getFrom()));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getTo()));
            System.out.println("message sent....");
            return "success.xhtml?faces-redirect=true";
        } catch (MessagingException ex) {
            return null;