What is the proverbial "Hello, World" program for JavaMail?

John Zukowski

The following program will send a mail message. It requires three command line arguments:

  • SMTP Server
  • From Email Address
  • To Email Address

An example start follows:

java MailExample smtp.mailserver from@from.com to@to.com


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

public class MailExample {
  public static void main (String args[]) 
      throws Exception {
    String host = args[0];
    String from = args[1];
    String to = args[2];

    // Get system properties
    Properties props = System.getProperties();

    // Setup mail server
    props.put("mail.smtp.host", host);

    // Get session
    Session session = Session.getInstance(props, null);

    // Define message
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
      new InternetAddress(to));
    message.setSubject("Hello JavaMail");
    message.setText("Welcome to JavaMail");

    // Send message