-
Notifications
You must be signed in to change notification settings - Fork 6.1k
/
EmailServiceImpl.java
54 lines (41 loc) · 1.8 KB
/
EmailServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.piggymetrics.notification.service;
import com.piggymetrics.notification.domain.NotificationType;
import com.piggymetrics.notification.domain.Recipient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.text.MessageFormat;
@Service
@RefreshScope
public class EmailServiceImpl implements EmailService {
private final Logger log = LoggerFactory.getLogger(getClass());
@Autowired
private JavaMailSender mailSender;
@Autowired
private Environment env;
@Override
public void send(NotificationType type, Recipient recipient, String attachment) throws MessagingException, IOException {
final String subject = env.getProperty(type.getSubject());
final String text = MessageFormat.format(env.getProperty(type.getText()), recipient.getAccountName());
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(recipient.getEmail());
helper.setSubject(subject);
helper.setText(text);
if (StringUtils.hasLength(attachment)) {
helper.addAttachment(env.getProperty(type.getAttachment()), new ByteArrayResource(attachment.getBytes()));
}
mailSender.send(message);
log.info("{} email notification has been send to {}", type, recipient.getEmail());
}
}