JangoMail FAQs

JavaMail

This example uses JavaMail (mail.jar). Replace the to and from addresses, and the username and password. Compile the class and run it. Ports can be 25, 2525, 587 or 465. The authenticator class is inline versus being from an imported package to show its usage. Plus, you can authenticate via IP address versus username/password.

import java.util.Properties;

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;
import javax.mail.internet.MimeMessage.RecipientType;

public class JangoSMTPDemo {
	public static void main(String[] args) throws MessagingException {
		new JangoSMTPDemo().run();
	}

	private void run() throws MessagingException {
		Message message = new MimeMessage(getSession());

		message.addRecipient(RecipientType.TO, new InternetAddress("to_name@wherever.com"));
		message.addFrom(new InternetAddress[] { new InternetAddress("your_name@somewhere.com") });

		message.setSubject("Hello from JangoSMTP using JavaMail");
		message.setContent("This message use JavaMail", "text/plain");

		Transport.send(message);
	}

	private Session getSession() {
		Authenticator authenticator = new Authenticator();

		Properties properties = new Properties();
		properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
		properties.setProperty("mail.smtp.auth", "true");

		properties.setProperty("mail.smtp.host", "express-relay.jangosmtp.net");
		properties.setProperty("mail.smtp.port", "25");

		return Session.getInstance(properties, authenticator);
	}

	private class Authenticator extends javax.mail.Authenticator {
		private PasswordAuthentication authentication;

		public Authenticator() {
			String username = "your JangoSMTP username";
			String password = "your JangoSMTP password";
			authentication = new PasswordAuthentication(username, password);
		}

		protected PasswordAuthentication getPasswordAuthentication() {
			return authentication;
		}
	}
}