JavaMail – send email

Using Java to send email messages is very easy with JavaMail API. Our sample code sends email message through the server that uses StartTLS and SSL so it is… rather well secured :)

JavaMail send email – the code

package com.itcuties.java;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * itcuties mail example
 * 
 * @author itcuties
 *
 */
public class JavaMailSender {

	// Run the mail example
	public static void main(String[] args) {
		// Send email
		sendEmail();
		
	}
	
	/**
	 * Send the email via SMTP using StartTLS and SSL
	 */
	private static void sendEmail() {
 
		// Create all the needed properties
		Properties connectionProperties = new Properties();
		// SMTP host
		connectionProperties.put("mail.smtp.host", "smtp.itcuties.com");
		// Is authentication enabled
		connectionProperties.put("mail.smtp.auth", "true");
		// Is StartTLS enabled
		connectionProperties.put("mail.smtp.starttls.enable", "true");
		// SSL Port
		connectionProperties.put("mail.smtp.socketFactory.port", "465");
		// SSL Socket Factory class
		connectionProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		// SMTP port, the same as SSL port :)
		connectionProperties.put("mail.smtp.port", "465");
		
		System.out.print("Creating the session...");
		
		// Create the session
		Session session = Session.getDefaultInstance(connectionProperties,
				new javax.mail.Authenticator() {	// Define the authenticator
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication("coding@itcuties.com","P@ssw0rd");
					}
				});
		
		System.out.println("done!");
		
		// Create and send the message
		try {
			// Create the message 
			Message message = new MimeMessage(session);
			// Set sender
			message.setFrom(new InternetAddress("coding@itcuties.com"));
			// Set the recipients
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("team@itcuties.com"));
			// Set message subject
			message.setSubject("Hello from Team ITCuties");
			// Set message text
			message.setText("Java is easy when you watch our tutorials ;)");
			
			System.out.print("Sending message...");
			// Send the message
			Transport.send(message);
			
			System.out.println("done!");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
}

If your case is sending email through the server that uses only StartTLS use this properties set:

// Create all the needed properties
Properties connectionProperties = new Properties();
// SMTP host
connectionProperties.put("mail.smtp.host", "smtp.itcuties.com");
// Is authentication enabled
connectionProperties.put("mail.smtp.auth", "true");
// Is TLS enabled
connectionProperties.put("mail.smtp.starttls.enable", "true");
// SMTP port
connectionProperties.put("mail.smtp.port", "587");

When no StartTLS nor SSL is used use this code:

// Create all the needed properties
Properties connectionProperties = new Properties();
// SMTP host
connectionProperties.put("mail.smtp.host", "smtp.itcuties.com");
// Is authentication enabled
connectionProperties.put("mail.smtp.auth", "true");
// SMTP port
connectionProperties.put("mail.smtp.port", "587");	// or maybe port 25, which is the other default one

If you are using maven like we do, this is the pom.xml file with all the needed artifacts the javamail, javax.activation and the javax.mail extensions.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.itcuties.java</groupId>
  <artifactId>java-mail-sender</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
    <!-- JavaMail API -->  
  	<dependency>
  		<groupId>javamail</groupId>
  		<artifactId>javamail</artifactId>
  		<version>1.3.3</version>
  	</dependency>
  	<!-- Java Activation -->
  	<dependency>
  		<groupId>javax.activation</groupId>
  		<artifactId>activation</artifactId>
  		<version>1.1.1</version>
  	</dependency>
  	<!-- JavaMail extension -->
  	<dependency>
  		<groupId>javax.mail</groupId>
  		<artifactId>mail</artifactId>
  		<version>1.4.6-rc1</version>
  	</dependency>
  </dependencies>
  
</project>

Download this sample code here.

This code is available on our GitHub repository as well.

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Java by Example App is available at Google Play Store NOW