Skip to content

Instantly share code, notes, and snippets.

@adityasatrio
Last active February 19, 2018 12:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adityasatrio/7e08674c5820c217d601 to your computer and use it in GitHub Desktop.
Save adityasatrio/7e08674c5820c217d601 to your computer and use it in GitHub Desktop.
Email using java email, velocity engine, spring
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.csl.cms.util.email;
import java.io.File;
import java.util.Map;
import org.springframework.core.io.InputStreamSource;
/**
*
* @author asn
*/
public class EmailMessages {
private String to;
private String from;
private String subject;
private String cc;
private String[] ccArray;
private String bcc;
private String[] bccArray;
private String replyTo;
private File file;
private String fileName;
private InputStreamSource inputStreamSource;
private Map<String, String> attributesTemplate;
private String contentType;
public void addAttachment(String fileName, File file) {
this.file = file;
this.fileName = fileName;
}
public void addAttachment(String fileName, InputStreamSource inputStreamSource) {
this.fileName = fileName;
this.inputStreamSource = inputStreamSource;
}
public void addAttachment(String fileName, InputStreamSource inputStreamSource, String contentType) {
this.fileName = fileName;
this.inputStreamSource = inputStreamSource;
this.contentType = contentType;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String[] getCcArray() {
return ccArray;
}
public void setCcArray(String[] ccArray) {
this.ccArray = ccArray;
}
public String getCc() {
return cc;
}
public void setCc(String cc) {
this.cc = cc;
}
public String getBcc() {
return bcc;
}
public void setBcc(String bcc) {
this.bcc = bcc;
}
public String[] getBccArray() {
return bccArray;
}
public void setBccArray(String[] bccArray) {
this.bccArray = bccArray;
}
public String getReplyTo() {
return replyTo;
}
public void setReplyTo(String replyTo) {
this.replyTo = replyTo;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public Map<String, String> getAttributesTemplate() {
return attributesTemplate;
}
public void setAttributesTemplate(Map<String, String> attributesTemplate) {
this.attributesTemplate = attributesTemplate;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public InputStreamSource getInputStreamSource() {
return inputStreamSource;
}
public void setInputStreamSource(InputStreamSource inputStreamSource) {
this.inputStreamSource = inputStreamSource;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.csl.cms.util.email;
import java.util.HashMap;
import java.util.Map;
import javax.mail.internet.MimeMessage;
import org.apache.velocity.app.VelocityEngine;
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;
import org.springframework.ui.velocity.VelocityEngineUtils;
/**
*
* @author asn
*
* A class that wrapped up java mail sender
*
*/
//annotate as component so spring will create the bean
@Component
public class EmailSender {
@Autowired
private JavaMailSender javaMailSender;
@Autowired
private VelocityEngine velocityEngine;
/**
*
* @param EmailMessage Object, you can set some attribute for send a mail
* message
* @param velocityTemplate a velocity template email, html - css and the
* type of file is *.vm
* @return void
*/
public void send(final EmailMessages emailmsg, final String velocityTemplate) {
MimeMessagePreparator mimeMessagePreparator = new MimeMessagePreparator() {
@Override
public void prepare(MimeMessage mm) throws Exception {
MimeMessageHelper messages;
if(emailmsg.getFileName() != null && !emailmsg.getFileName().isEmpty() ){
messages = new MimeMessageHelper(mm, true);
}else{
messages = new MimeMessageHelper(mm);
}
String text = null;
messages.setTo(emailmsg.getTo());
messages.setFrom(emailmsg.getFrom());
messages.setSubject(emailmsg.getSubject());
if (emailmsg.getCc() != null) {
messages.setCc(emailmsg.getCc().trim());
} else if (emailmsg.getCcArray() != null) {
messages.setCc(emailmsg.getCcArray());
} else if (emailmsg.getBcc() != null) {
messages.setBcc(emailmsg.getBcc().trim());
} else if (emailmsg.getBccArray() != null) {
messages.setBcc(emailmsg.getBccArray());
} else if (emailmsg.getReplyTo() != null) {
messages.setReplyTo(emailmsg.getReplyTo().trim());
}
if (emailmsg.getFileName() != null && (emailmsg.getFile() != null || emailmsg.getInputStreamSource() != null)) {
if (emailmsg.getFile() != null) {
messages.addAttachment(emailmsg.getFileName(), emailmsg.getFile());
} else if (emailmsg.getInputStreamSource() != null) {
messages.addAttachment(emailmsg.getFileName(), emailmsg.getInputStreamSource());
} else if (emailmsg.getInputStreamSource() != null && emailmsg.getContentType() != null) {
messages.addAttachment(emailmsg.getFileName(), emailmsg.getInputStreamSource(), emailmsg.getContentType());
}
}
text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, velocityTemplate, emailmsg.getAttributesTemplate());
messages.setText(text, true);
}
};
this.javaMailSender.send(mimeMessagePreparator);
}
}
@Autowired
private EmailSender emailSender;
//autowired email sender instance class
EmailMessages messages = new EmailMessages();
//set standard email attributes
messages.setTo("aa@aa.com");
messages.setFrom("bb@bb.com");
messages.setSubject("ERROR LOGS ");
//set value for template email contents
Map<String, String> map = new HashMap<>();
map.put("templateContent1", "aaa");
map.put("templateContent2","bbb");
//add map into emailMessages object
messages.setAttributesTemplate(map);
//create attachment
//String will be converted as email file attachment in memory
String stringMessage = "this string will be converted as file attachment"
InputStream inputStream = new ByteArrayInputStream(stringMessage.getBytes());
//IOUtils => import org.apache.commons.io.IOUtils;
messages.addAttachment("attachmentLogs_" + sdf.format(date.toDate())+".txt", new ByteArrayResource(IOUtils.toByteArray(inputStream)), "text/plain");
emailSender.send(messages, "emailErrorLogs.vm");
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
<!--Spring Mail-Velocity Template-->
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl" >
<!--<property name="host" value="smtp.gmail.com"/>-->
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">false</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.smtp.host">smtp.gmail.com</prop>
<prop key="mail.smtp.port">587</prop>
<!-- for ssl trust, bypass scan virus -->
<prop key="mail.smtp.ssl.trust">smtp.gmail.com</prop>
</props>
</property>
<property name="username" value="xxx@xxx.co.id" />
<property name="password" value="xxx" />
</bean>
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean" >
<property name="velocityProperties" >
<value>
resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</value>
</property>
</bean>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment