Search code examples
javaeclipseemailgmailjakarta-mail

Eclipse Java, Send email from Gmail SMTP programatically


I am trying to send an email programmatically using Java, in Eclipse. I have the project set up properly, using the most recent JDK 1.8.0_45. By which I mean I have included mailapi.jar and smtp.jar.

I have read numerous other posts on the topic, and I know my Java build path must include mail.jar. However, when I searched the folder, no mail.jar was found; mailapi.jar was the closest and seems to resolve the include errors I was getting without it.

Here is my code:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class emailer {

    private static String host = "smtp.gmail.com";
    private static String user = "**********";
    private static String pass = "***********";

    public static void sendEmail(String fromAddr, String toAddr, String subject, String body)
    {
        Properties prop = System.getProperties();
        prop.put("mail.smtp.starttls.enable", "true");
        prop.put("mail.smtp.host", host);
        prop.put("mail.smtp.user", user);
        prop.put("mail.smtp.password", pass);
        prop.put("mail.smtp.port", "465");
        prop.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(prop);
        MimeMessage message = new MimeMessage(session);

        try
        {
            message.setFrom(new InternetAddress(fromAddr));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddr));
            message.setSubject(subject);
            message.setText(body);
            Transport transport = session.getTransport("smtp");
            transport.connect(host, user, pass);
            transport.send(message, message.getAllRecipients()); 
            //The above line has a warning message
            //"The static method send(Message, Address[]) from the type Transport should be accessed in a static way"
            transport.close();
            System.out.println("done");
        }
        catch (AddressException e) {e.printStackTrace();}
        catch (MessagingException e) {e.printStackTrace();}
    }
}

The code compiles without errors, and upon running, continues to run indefinitely, never breaking nor stopping. So unfortunately I do not have a stack trace to show... Any help would be greatly appreciated.


Solution

  • Change the outgoing port to 587 and not 465. That get's rid of the infinite timeout (since Transport has no timeout value). Also, using the static Transport.send() will simplify your code a lot (I also did get some errors when using the non-static method). So something like this.

    public class Emailer  //Class names should always be capitalized
    {
    
    private static String host = "smtp.gmail.com";
    private static String user = "**********";
    private static String pass = "***********";
    
    public static void sendEmail(String fromAddr, String toAddr, String subject, String body)
    {
        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");
        Session.getInstance(props, null);
    
        MimeMessage message = new MimeMessage(session);
    
        try
        {
            message.setFrom(new InternetAddress(fromAddr));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddr));
            message.setSubject(subject);
            message.setText(body);
            Transport.send(message, user, pass);
        }
        catch (AddressException e) {e.printStackTrace();}
        catch (MessagingException e) {e.printStackTrace();}
    }