Spring Email HTML Attachment and Plain Text Email

 

In this tutorial, we will learn how to use Spring Email to generate HTML body along with HTML file as attachment. We will also learn how to send a plain text email.


HTML Template which we will use in the email: htmlTemplate.html
<body>

<style>
 .header{
	background-color: Coral;
	text-align: center;
	font-size: 20px;
}

 .tr1{
 	background-color: DarkSeaGreen;
 	text-align: center;
	font-size: 15px;
	font-style: italic;
	
 }
 
 .tr2{
 	background-color:Thistle;
 	text-align: center;
	font-size: 15px;
	font-style: italic;
 }
 
</style>

<table width="965" border="1">
	<tr class="header">
		<td style="width:25%">Header 1</td>
		<td style="width:25%">Header 2</td>
		<td style="width:25%">Header 3</td>
		<td style="width:25%">Header 4</td>
	</tr>
	
 
 
</table>
</body>
	


Plain text template: plainTextTemplate.txt
Hello there!

This is a new line.

This is third line.


Emailsender.java
package com.visionjava.SpringEmail;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Component;

@Component("emailSender")
public class EmailSender {

	@Autowired
	private JavaMailSender mailSender;	

	public void sendEmailAttachment(final String to, final String from, final String subject, final String path) {

		MimeMessagePreparator preparator = new MimeMessagePreparator() {

			public void prepare(MimeMessage mimeMessage) throws Exception {
				
				byte[] encoded = Files.readAllBytes(Paths.get(path));
				String body =  new String(encoded, StandardCharsets.UTF_8);
				
				//Sending Message
				MimeMessageHelper message = new MimeMessageHelper(mimeMessage,	true);
				message.setTo(to);
				message.setFrom(from);
				message.setSubject(subject);				
				message.setText(body, true); //true sets the content to be of type html
				File file = new File("c:\\visionjavaemail\\htmlTemplate.html");
				message.addAttachment("attachment.html", file);
			}
		};
		this.mailSender.send(preparator);
	}
	
	public void sendEmailPlainText(final String to, final String from, final String subject, final String path) {

		MimeMessagePreparator preparator = new MimeMessagePreparator() {

			public void prepare(MimeMessage mimeMessage) throws Exception {
				
				byte[] encoded = Files.readAllBytes(Paths.get(path));
				String body =  new String(encoded, StandardCharsets.UTF_8);
				
				//Sending Message
				MimeMessageHelper message = new MimeMessageHelper(mimeMessage,	true);
				message.setTo(to);
				message.setFrom(from);
				message.setSubject(subject);				
				message.setText(body, false); //false sets the content to be of type plain text
				File file = new File("c:\\visionjavaemail\\plainTextTemplate.txt");
			}
		};
		this.mailSender.send(preparator);
	}
}


application-context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p" 
	xsi:schemaLocation="
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
     http://www.springframework.org/schema/context     
     http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<context:annotation-config />
	<context:component-scan base-package="com.visionjava.SpringEmail" />
	<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="javaMailProperties">
			<props>
				<prop key="mail.smtp.auth">false</prop>
				<prop key="mail.smtp.starttls.enable">true</prop>
				<prop key="mail.smtp.host">localhost</prop>
				<prop key="mail.smtp.port">1025</prop>
			</props>
		</property>
		<!-- 
		<property name="username" value="email" /> 
		<property name="password" value="password" />
		-->
	</bean>

</beans>


MainCaller.java
package com.visionjava.SpringEmail;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainCaller {

  public static void main(String[] args) {
	
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
	EmailSender emailSender = (EmailSender)context.getBean("emailSender");
	
	emailSender.sendEmailAttachment("test@visionjava.com", "test@visionjava.com", "HTML EMAIL", "c:\\visionjavaemail\\htmlTemplate.html");
	
	emailSender.sendEmailPlainText("test@visionjava.com", "test@visionjava.com", "PLAIN TEXT EMAIL", "c:\\visionjavaemail\\plainTextTemplate.txt");
  }

}



POM file:

<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.visionjava</groupId>
	<artifactId>SpringVelocityTemplateEmail</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>SpringVelocityTemplateEmail</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>javax.mail</groupId>
			<artifactId>mail</artifactId>
			<version>1.4.7</version>
		</dependency>

	</dependencies>
</project>



OUTPUT :

html email











Source Code : SpringEmail.rar
Web Analytics