How to send test email in Java

Introduction - sending email in Java

If you're writing software using Java, you're probably sending emails, either as part of sign up flows, or for sending notifications or maybe you just like sending pictures of Chameleons!

The standard way to send email is via an SMTP server, usually hosted by a specialist email service provider but can also be hosted yourself.

Sending email in Java is built in to the standard libraries.

The following code illustrates how to send an email:

To compile and run this program 'javax.mail.jar' will need to be present on your system and added to your classpath. 

// version 1.1.4-3

import java.util.Properties
import javax.mail.Authenticator
import javax.mail.PasswordAuthentication
import javax.mail.Session
import javax.mail.internet.MimeMessage
import javax.mail.internet.InternetAddress
import javax.mail.Message.RecipientType
import javax.mail.Transport

fun sendEmail(user: String, tos: Array<String>, ccs: Array<String>, title: String,
              body: String, password: String) {
    val props = Properties()
    val host = ""
    with (props) {
        put("", host)
        put("mail.smtp.port", "587") // for TLS
        put("mail.smtp.auth", "true")
        put("mail.smtp.starttls.enable", "true")
    val auth = object: Authenticator() {
        protected override fun getPasswordAuthentication() =
            PasswordAuthentication(user, password)
    val session = Session.getInstance(props, auth)
    val message = MimeMessage(session)
    with (message) {
        for (to in tos) addRecipient(RecipientType.TO, InternetAddress(to))
        for (cc in ccs) addRecipient(RecipientType.TO, InternetAddress(cc))
    val transport = session.getTransport("smtp")
    with (transport) {
        connect(host, user, password)
        sendMessage(message, message.allRecipients)

fun main(args: Array<String>) {
    val user = ""
    val tos = arrayOf("")
    val ccs = arrayOf<String>()
    val title = "Hello from Imitate Email"
    val body = "This is just a test email"
    val password = "secret"
    sendEmail(user, tos, ccs, title, body, password)

Sending Test Email: Set up Imitate Email

If you've not already done so please sign up to Imitate Email.

For Imitate Email, when sending test emails you use the following server details:

Port: 587

For your username and password you need to decide if you're using your personal mailbox or have set up a project in Imitate Email.

Locate your mailbox credentials

Your username

To locate your username go to Settings > My Mailbox where you will find it (it is the same for all mailboxes in your account)

For the password

If you're on the Free or Developer plan or you're setting this up for your personal mailbox copy the password from the same place as the username.

If you're setting this up for a team mailbox go to Settings > Projects and you will find the password next to the symbol for your mailbox. It will look like the following 6bcb69b2-08ac-4c67-911a-10442f7d84b3

Screenshot of Imitate Email personal settings

That's it!