Example to send an e-mail from your Java application (with or without TLS authentication)
In this post I just wanted to share a small example to send an e-mail from your Java application, using TLS authentication. You’ll need to include the javax.mail.jar library from JavaMail in your project as a dependency…
I think all steps are pretty much self-explaining, like how to set the host, username and password, and how to add more recipients…
import java.util.*; import javax.mail.*; import javax.mail.internet.*; public class SendMail { public static void main(String[] args) { new SendMail(); } public SendMail() { String toAddress = "recipient@you.com"; String fromAddress = "from@me.com"; Properties properties = System.getProperties(); properties.setProperty("mail.smtp.host", "smtp.website.net"); properties.setProperty("mail.smtp.port", "25"); properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.smtp.starttls.enable", "true"); // To override the Authenticator in the session Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("username_or_email", "THE_P@SSWORD"); } }; // Create a session object with our above properties and the authenticator Session session = Session.getInstance(properties, authenticator); try { // Create a MimeMessage object. MimeMessage message = new MimeMessage(session); message.setFrom(fromAddress); message.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress)); // Add TO, CC or BCC addresses as needed... message.setSubject("This is a subject for my e-mail..."); message.setText("This is the body of my message...\n\nSincerely yours,\n\n\nTom"); // Actually end the message... Transport.send(message); System.out.println("Message has been sent"); } catch (MessagingException e) { e.printStackTrace(System.err); } } }
If you don’t need authentication, your example becomes a lot shorter:
import java.util.*; import javax.mail.*; import javax.mail.internet.*; public class SendMail { public static void main(String[] args) { new SendMail(); } public SendMail() { String toAddress = "recipient@you.com"; String fromAddress = "from@me.com"; Properties properties = System.getProperties(); properties.setProperty("mail.smtp.host", "smtp.website.net"); // Create a session object with our above properties Session session = Session.getDefaultInstance(properties); try { // Create a MimeMessage object. MimeMessage message = new MimeMessage(session); message.setFrom(fromAddress); message.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress)); // Add TO, CC or BCC addresses as needed... message.setSubject("This is a subject for my e-mail..."); message.setText("This is the body of my message...\n\nSincerely yours,\n\n\nTom"); // Actually end the message... Transport.send(message); System.out.println("Message has been sent"); } catch (MessagingException e) { e.printStackTrace(System.err); } } }
Enjoy!