Blame | Last modification | View Log | RSS feed
//@Value("${prod}")package com.spice.profitmandi.common.services;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.beans.factory.annotation.Value;import org.springframework.core.io.InputStreamSource;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.MimeMessageHelper;import org.springframework.stereotype.Service;import javax.mail.Multipart;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;import java.io.File;import java.io.Serializable;import java.util.Arrays;import java.util.List;import java.util.Map;@Servicepublic class EmailServiceImpl implements EmailService {private static final Logger logger =LogManager.getLogger(EmailServiceImpl.class);@Value("${prod}")private boolean isProd;private final JavaMailSender mailSender;public EmailServiceImpl(JavaMailSender mailSender) {this.mailSender = mailSender;}@Overridepublic void sendMailWithAttachments(JavaMailSender mailSender,String[] emailTo,String[] cc,String subject,String body) throws Exception {MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setTo(emailTo);helper.setSubject(subject);helper.setText(body, true);if (cc != null) {helper.setCc(cc);}helper.setFrom(new InternetAddress("noreply@smartdukaan.com","SmartDukaan Care"));sendIfProd(message, emailTo, subject);}@Overridepublic void sendMailWithAttachments(JavaMailSender mailSender,String[] emailTo,String[] cc,String subject,String body,List<File> attachments) throws Exception {MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setTo(emailTo);helper.setSubject(subject);helper.setText(body, true);if (cc != null) {helper.setCc(cc);}helper.setFrom(new InternetAddress("noreply@smartdukaan.com","SmartDukaan Care"));if (attachments != null) {for (File file : attachments) {helper.addAttachment(file.getName(), file);}}sendIfProd(message, emailTo, subject);}@Overridepublic void sendMailWithAttachments(JavaMailSender mailSender,String emailTo,String[] cc,String subject,String body,List<File> attachments) throws Exception {// Delegate to array version for Utils.java compatibilitysendMailWithAttachments(mailSender,new String[]{emailTo},cc,subject,body,attachments);}@Overridepublic void sendMailWithAttachment(JavaMailSender mailSender,String[] emailTo,String[] cc,String subject,String body,String fileName,InputStreamSource inputStreamSource) throws Exception {MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setTo(emailTo);helper.setSubject(subject);helper.setText(body, true);if (cc != null) {helper.setCc(cc);}helper.addAttachment(fileName, inputStreamSource);helper.setFrom(new InternetAddress("noreply@smartdukaan.com","SmartDukaan Care"));sendIfProd(message, emailTo, subject);}@Overridepublic void sendMailWithAttachments(JavaMailSender mailSender,String[] emailTo,String[] cc,String[] bcc,String subject,String body,boolean html,Attachment... attachments) throws Exception {MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setTo(emailTo);helper.setSubject(subject);helper.setText(body, html);if (cc != null) helper.setCc(cc);if (bcc != null) helper.setBcc(bcc);helper.setFrom(new InternetAddress("noreply@smartdukaan.com","SmartDukaan Care"));if (attachments != null) {for (Attachment attachment : attachments) {if (attachment != null) {helper.addAttachment(attachment.getFileName(),attachment.getInputStreamSource());}}}sendIfProd(message, emailTo, subject);}@Overridepublic void sendHtmlMailWithAttachments(JavaMailSender mailSender,String[] emailTo,String[] cc,String subject,String body,Attachment... attachments) throws Exception {sendMailWithAttachments(mailSender,emailTo,cc,null,subject,body,true,attachments);}@Overridepublic void sendEmbeddedHtmlMail(JavaMailSender mailSender,String[] emailTo,String[] cc,String subject,String body,Map<? extends Serializable, File> inlineImages) throws Exception {MimeMessage message = mailSender.createMimeMessage();Multipart multipart = new MimeMultipart();MimeBodyPart bodyPart = new MimeBodyPart();bodyPart.setContent(body, "text/html");multipart.addBodyPart(bodyPart);if (inlineImages != null) {for (Map.Entry<? extends Serializable, File> entry : inlineImages.entrySet()) {MimeBodyPart imagePart = new MimeBodyPart();imagePart.setHeader("Content-ID", entry.getKey().toString());imagePart.setDisposition(MimeBodyPart.INLINE);imagePart.attachFile(entry.getValue());multipart.addBodyPart(imagePart);}}message.setContent(multipart);message.setSubject(subject);message.setRecipients(MimeMessage.RecipientType.TO,InternetAddress.parse(String.join(",", emailTo)));if (cc != null) {message.addRecipients(MimeMessage.RecipientType.CC,InternetAddress.parse(String.join(",", cc)));}message.setFrom(new InternetAddress("noreply@smartdukaan.com","SmartDukaan Care"));sendIfProd(message, emailTo, subject);}private void sendIfProd(MimeMessage message,String[] emailTo,String subject) {if (isProd) {mailSender.send(message);} else {logger.info("[MAIL BLOCKED - NON PROD] to={}, subject={}",Arrays.toString(emailTo),subject);}}}