0 votes
1 view
in Java by (3.5k points)

Is it possible to send an email from my Java application using a GMail account? I have configured my company mail server with Java app to send email, but that's not going to cut it when I distribute the application. Answers with any of using Hotmail, Yahoo or GMail are acceptable.

1 Answer

0 votes
by (46.1k points)

First download the JavaMail API and make sure the relevant jar files are in your classpath.

Here's a full working example using GMail.

import java.util.*;

import javax.mail.*;

import javax.mail.internet.*;

public class Main {

    private static String USER_NAME = "*****"; // GMail user name (just the part before "@gmail.com")

    private static String PASSWORD = "********"; // GMail password

    private static String RECIPIENT = "[email protected]";

    public static void main(String[] args) {

        String from = USER_NAME;

        String pass = PASSWORD;

        String[] to = { RECIPIENT }; // list of recipient email addresses

        String subject = "Java send mail example";

        String body = "Welcome to JavaMail!";

        sendFromGMail(from, pass, to, subject, body);

    }

    private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {

        Properties props = System.getProperties();

        String host = "smtp.gmail.com";

        props.put("mail.smtp.starttls.enable", "true");

        props.put("mail.smtp.host", host);

        props.put("mail.smtp.user", from);

        props.put("mail.smtp.password", pass);

        props.put("mail.smtp.port", "587");

        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props);

        MimeMessage message = new MimeMessage(session);

        try {

            message.setFrom(new InternetAddress(from));

            InternetAddress[] toAddress = new InternetAddress[to.length];

            // To get the array of addresses

            for( int i = 0; i < to.length; i++ ) {

                toAddress[i] = new InternetAddress(to[i]);

            }

            for( int i = 0; i < toAddress.length; i++) {

                message.addRecipient(Message.RecipientType.TO, toAddress[i]);

            }

            message.setSubject(subject);

            message.setText(body);

            Transport transport = session.getTransport("smtp");

            transport.connect(host, from, pass);

            transport.sendMessage(message, message.getAllRecipients());

            transport.close();

        }

        catch (AddressException ae) {

            ae.printStackTrace();

        }

        catch (MessagingException me) {

            me.printStackTrace();

        }

    }

}

Naturally, you'll want to do more in the catch blocks than print the stack trace as I did in the example code above. (Remove the catch blocks to see which method calls from the JavaMail API throw exceptions so you can better see how to properly handle them.)

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...