/********************************************************************************************************************************* * Autorskie Prawa Majątkowe - Moose Spółka z ograniczoną odpowiedzialnością * * Copyright 2017 Moose Spółka z ograniczoną odpowiedzialnością ********************************************************************************************************************************/ package eu.mooseinc.dell.form.service.impl; import com.google.common.base.Preconditions; import eu.mooseinc.dell.form.exception.ServiceException; import eu.mooseinc.dell.form.model.Attachment; import eu.mooseinc.dell.form.service.MailService; import java.io.File; import java.util.List; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; /** * Email Service implementation */ @Service public class MailServiceImpl implements MailService { /** * legger */ private static final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class); /** * codding standard for e-mail. */ private static final String ENCODING = "UTF-8"; /** * Email sender address */ @Value("${mail.from}") private String from; @Autowired @Qualifier("mailSender") private JavaMailSender mailSender; @Override public void send(String[] to, String subject, String text, List attachments) throws ServiceException { Preconditions.checkArgument(to != null && to.length > 0); Preconditions.checkArgument(StringUtils.isNotEmpty(subject)); Preconditions.checkArgument(StringUtils.isNotEmpty(text)); Preconditions.checkArgument(attachments != null); sendMail(to, null, subject, text, attachments); } @Override public void send(String[] to, String cc, String subject, String text, List attachments) throws ServiceException { Preconditions.checkArgument(to != null && to.length > 0); Preconditions.checkArgument(StringUtils.isNotEmpty(subject)); Preconditions.checkArgument(StringUtils.isNotEmpty(text)); sendMail(to, cc, subject, text, attachments); } /** * Sending email message * * @param to message recipient * @param cc meessage copy recipient * @param subject message subject * @param text message body * @param attachments list of attachments * * @throws ServiceException when ther is a problem with sending email */ private void sendMail(String[] to, String cc, String subject, String text, List attachments) throws ServiceException { final MimeMessage message = mailSender.createMimeMessage(); try { final MimeMessageHelper helper = new MimeMessageHelper(message, true, ENCODING); helper.setTo(to); helper.setFrom(from); helper.setSubject(subject); helper.setText(text, true); if (StringUtils.isNotEmpty(cc)) helper.setCc(cc); if (attachments != null) { for (final Attachment attachment : attachments) { final File file = new File(attachment.getFilePath()); helper.addAttachment(attachment.getFileName(), file); } } } catch (final MessagingException e) { logger.error("Wystąpił błąd podczas tworzenia wiadomości", e); throw new ServiceException("Wystąpił błąd podczas tworzenia wiadomości", e); } mailSender.send(message); } }