|
|
@@ -1,8 +1,12 @@
|
|
|
package cn.reghao.devops.manager.notification.service.notifier.email;
|
|
|
|
|
|
+import cn.reghao.devops.manager.notification.db.repository.EmailAccountRepository;
|
|
|
import cn.reghao.devops.manager.notification.model.po.EmailAccount;
|
|
|
import cn.reghao.devops.manager.notification.service.notifier.Notify;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.mail.Address;
|
|
|
import javax.mail.MessagingException;
|
|
|
import javax.mail.Session;
|
|
|
import javax.mail.Transport;
|
|
|
@@ -10,6 +14,7 @@ import javax.mail.internet.InternetAddress;
|
|
|
import javax.mail.internet.MimeMessage;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
import java.util.Properties;
|
|
|
|
|
|
/**
|
|
|
@@ -18,46 +23,46 @@ import java.util.Properties;
|
|
|
* @author reghao
|
|
|
* @date 2021-02-25 19:23:16
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
public class EmailNotify implements Notify<EmailMsg> {
|
|
|
- private final String account;
|
|
|
- private final String password;
|
|
|
- private final InternetAddress from;
|
|
|
- private Session session;
|
|
|
+ private final EmailAccountRepository emailAccountRepository;
|
|
|
|
|
|
- public EmailNotify(EmailAccount emailAccount) throws UnsupportedEncodingException {
|
|
|
- this.account = emailAccount.getUsername();
|
|
|
- this.password = emailAccount.getPassword();
|
|
|
- this.from = new InternetAddress(account, emailAccount.getPersonal(), "UTF-8");
|
|
|
- initSessioin(emailAccount.getSmtp());
|
|
|
+ public EmailNotify(EmailAccountRepository emailAccountRepository) {
|
|
|
+ this.emailAccountRepository = emailAccountRepository;
|
|
|
}
|
|
|
|
|
|
- private void initSessioin(String smtp) {
|
|
|
+ @Override
|
|
|
+ public void send(String receiver, EmailMsg msg) throws Exception {
|
|
|
+ List<EmailAccount> list = emailAccountRepository.findAll();
|
|
|
+ if (list.isEmpty()) {
|
|
|
+ log.info("没有可用于发送通知的邮箱配置");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ EmailAccount emailAccount = list.get(0);
|
|
|
+ String myEmail = emailAccount.getUsername();
|
|
|
+ String password = emailAccount.getPassword();
|
|
|
+ String personal = emailAccount.getPersonal();
|
|
|
+
|
|
|
+ String smtp = emailAccount.getSmtp();
|
|
|
Properties props = new Properties();
|
|
|
props.setProperty("mail.transport.protocol", "smtp");
|
|
|
props.setProperty("mail.smtp.host", smtp);
|
|
|
props.setProperty("mail.smtp.auth", "true");
|
|
|
- session = Session.getInstance(props);
|
|
|
- session.setDebug(true);
|
|
|
- }
|
|
|
+ Session session = Session.getInstance(props);
|
|
|
|
|
|
- @Override
|
|
|
- public void send(String receiver, EmailMsg msg) throws Exception {
|
|
|
- MimeMessage message = message(emailDestinations(receiver), msg.getSubject(), msg.getContent());
|
|
|
- Transport transport = session.getTransport();
|
|
|
- transport.connect(account, password);
|
|
|
- transport.sendMessage(message, message.getAllRecipients());
|
|
|
- transport.close();
|
|
|
- }
|
|
|
+ InternetAddress from = new InternetAddress(myEmail, personal, "UTF-8");
|
|
|
+ InternetAddress[] dest = getDestinations(receiver);
|
|
|
+ MimeMessage message = getMessage(session, from, dest, msg);
|
|
|
|
|
|
- public void send(String receiver, String subject, String msg) throws Exception {
|
|
|
- MimeMessage message = message(emailDestinations(receiver), subject, msg);
|
|
|
Transport transport = session.getTransport();
|
|
|
- transport.connect(account, password);
|
|
|
+ transport.connect(myEmail, password);
|
|
|
transport.sendMessage(message, message.getAllRecipients());
|
|
|
transport.close();
|
|
|
}
|
|
|
|
|
|
- private InternetAddress[] emailDestinations(String receiver) throws UnsupportedEncodingException {
|
|
|
+ private InternetAddress[] getDestinations(String receiver) throws UnsupportedEncodingException {
|
|
|
String[] strs = receiver.split(",");
|
|
|
int len = strs.length;
|
|
|
InternetAddress[] addresses = new InternetAddress[len];
|
|
|
@@ -67,12 +72,13 @@ public class EmailNotify implements Notify<EmailMsg> {
|
|
|
return addresses;
|
|
|
}
|
|
|
|
|
|
- private MimeMessage message(InternetAddress[] addresses, String subject, String content) throws MessagingException {
|
|
|
+ private MimeMessage getMessage(Session session, Address from, InternetAddress[] dest, EmailMsg msg)
|
|
|
+ throws MessagingException {
|
|
|
MimeMessage message = new MimeMessage(session);
|
|
|
message.setFrom(from);
|
|
|
- message.setRecipients(MimeMessage.RecipientType.TO, addresses);
|
|
|
- message.setSubject(subject, "UTF-8");
|
|
|
- message.setContent(content, "text/html;charset=UTF-8");
|
|
|
+ message.setRecipients(MimeMessage.RecipientType.TO, dest);
|
|
|
+ message.setSubject(msg.getSubject(), "UTF-8");
|
|
|
+ message.setContent(msg.getContent(), "text/html;charset=UTF-8");
|
|
|
message.setSentDate(new Date());
|
|
|
return message;
|
|
|
}
|