Showing
1 changed file
with
69 additions
and
77 deletions
@@ -45,42 +45,33 @@ public class TkMailServiceImpl implements TkMailService { | @@ -45,42 +45,33 @@ public class TkMailServiceImpl implements TkMailService { | ||
45 | 45 | ||
46 | @Override | 46 | @Override |
47 | public boolean sendEmail(EmailReqDTO emailReqDTO) { | 47 | public boolean sendEmail(EmailReqDTO emailReqDTO) { |
48 | - boolean result =false; | ||
49 | - | ||
50 | - | ||
51 | - | ||
52 | - /**消息模板是否可用*/ | ||
53 | - QueryWrapper<TkMessageTemplateEntity> messageTemplateQueryWrapper = new QueryWrapper<TkMessageTemplateEntity>(); | ||
54 | - messageTemplateQueryWrapper.lambda() | ||
55 | - .eq(TkMessageTemplateEntity::getId, emailReqDTO.getId()) | ||
56 | - .eq(TkMessageTemplateEntity::getStatus, AssetStatusEnum.ENABLE.ordinal()); | ||
57 | - TkMessageTemplateEntity messageTemplate = messageTemplateMapper.selectOne(messageTemplateQueryWrapper); | ||
58 | - if (null == messageTemplate) { | ||
59 | - throw new YtDataValidationException(ErrorMessage.MESSAGE_TEMPLATE_NOT_EXISTS.getMessage()); | ||
60 | - } | ||
61 | - | ||
62 | - /**消息配置是否可用*/ | ||
63 | - QueryWrapper<TkMessageConfigEntity> configQueryWrapper = new QueryWrapper<>(); | ||
64 | - configQueryWrapper.lambda() | ||
65 | - .eq(TkMessageConfigEntity::getId, messageTemplate.getMessageConfigId()) | ||
66 | - .eq(TkMessageConfigEntity::getStatus, AssetStatusEnum.ENABLE.ordinal()); | ||
67 | - TkMessageConfigEntity messageConfig = | ||
68 | - messageConfigMapper.selectOne(configQueryWrapper); | ||
69 | - if (null == messageConfig) { | ||
70 | - throw new YtDataValidationException(ErrorMessage.MESSAGE_CONFIG_NOT_EXISTS.getMessage()); | ||
71 | - } | ||
72 | - | ||
73 | - | ||
74 | - | ||
75 | - | ||
76 | - | ||
77 | - | ||
78 | - | ||
79 | - | 48 | + boolean result = false; |
49 | + // 消息模板是否可用 | ||
50 | + QueryWrapper<TkMessageTemplateEntity> messageTemplateQueryWrapper = new QueryWrapper<>(); | ||
51 | + messageTemplateQueryWrapper | ||
52 | + .lambda() | ||
53 | + .eq(TkMessageTemplateEntity::getId, emailReqDTO.getId()) | ||
54 | + .eq(TkMessageTemplateEntity::getStatus, AssetStatusEnum.ENABLE.ordinal()); | ||
55 | + TkMessageTemplateEntity messageTemplate = | ||
56 | + messageTemplateMapper.selectOne(messageTemplateQueryWrapper); | ||
57 | + if (null == messageTemplate) { | ||
58 | + throw new YtDataValidationException(ErrorMessage.MESSAGE_TEMPLATE_NOT_EXISTS.getMessage()); | ||
59 | + } | ||
60 | + // 消息配置是否可用 | ||
61 | + QueryWrapper<TkMessageConfigEntity> configQueryWrapper = new QueryWrapper<>(); | ||
62 | + configQueryWrapper | ||
63 | + .lambda() | ||
64 | + .eq(TkMessageConfigEntity::getId, messageTemplate.getMessageConfigId()) | ||
65 | + .eq(TkMessageConfigEntity::getStatus, AssetStatusEnum.ENABLE.ordinal()); | ||
66 | + TkMessageConfigEntity messageConfig = messageConfigMapper.selectOne(configQueryWrapper); | ||
67 | + if (null == messageConfig) { | ||
68 | + throw new YtDataValidationException(ErrorMessage.MESSAGE_CONFIG_NOT_EXISTS.getMessage()); | ||
69 | + } | ||
80 | 70 | ||
81 | JsonNode configNode = messageConfig.getConfig(); | 71 | JsonNode configNode = messageConfig.getConfig(); |
82 | - EmailConfiguration emailConfiguration = JacksonUtil.convertValue(configNode, EmailConfiguration.class); | ||
83 | - if(emailConfiguration !=null){ | 72 | + EmailConfiguration emailConfiguration = |
73 | + JacksonUtil.convertValue(configNode, EmailConfiguration.class); | ||
74 | + if (emailConfiguration != null) { | ||
84 | Properties props = new Properties(); | 75 | Properties props = new Properties(); |
85 | // 要连接的SMTP服务器 | 76 | // 要连接的SMTP服务器 |
86 | props.put("mail.smtp.host", emailConfiguration.getHost()); | 77 | props.put("mail.smtp.host", emailConfiguration.getHost()); |
@@ -93,29 +84,28 @@ public class TkMailServiceImpl implements TkMailService { | @@ -93,29 +84,28 @@ public class TkMailServiceImpl implements TkMailService { | ||
93 | props.put("mail.smtp.auth", "true"); | 84 | props.put("mail.smtp.auth", "true"); |
94 | // 使用ssl | 85 | // 使用ssl |
95 | props.put("mail.smtp.ssl.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); | 86 | props.put("mail.smtp.ssl.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); |
96 | - props.put("mail.smtp.socketFactory.port",emailConfiguration.getPort()); | ||
97 | - String remark = null; | 87 | + props.put("mail.smtp.socketFactory.port", emailConfiguration.getPort()); |
88 | + String remark; | ||
98 | try { | 89 | try { |
99 | Session session = | 90 | Session session = |
100 | - Session.getInstance( | ||
101 | - props, | ||
102 | - new Authenticator() { | ||
103 | - @Override | ||
104 | - protected PasswordAuthentication getPasswordAuthentication() { | ||
105 | - if (Boolean.parseBoolean(props.getProperty("mail.smtp.auth"))) { | ||
106 | - // 需要认证 | ||
107 | - return new PasswordAuthentication( | ||
108 | - props.getProperty("mail.smtp.user"), | ||
109 | - props.getProperty("mail.smtp.pass")); | ||
110 | - } | ||
111 | - return super.getPasswordAuthentication(); | ||
112 | - } | ||
113 | - }); | 91 | + Session.getInstance( |
92 | + props, | ||
93 | + new Authenticator() { | ||
94 | + @Override | ||
95 | + protected PasswordAuthentication getPasswordAuthentication() { | ||
96 | + if (Boolean.parseBoolean(props.getProperty("mail.smtp.auth"))) { | ||
97 | + // 需要认证 | ||
98 | + return new PasswordAuthentication( | ||
99 | + props.getProperty("mail.smtp.user"), props.getProperty("mail.smtp.pass")); | ||
100 | + } | ||
101 | + return super.getPasswordAuthentication(); | ||
102 | + } | ||
103 | + }); | ||
114 | 104 | ||
115 | MimeMessage mimeMessage = new MimeMessage(session); | 105 | MimeMessage mimeMessage = new MimeMessage(session); |
116 | // 邮件发送人 | 106 | // 邮件发送人 |
117 | mimeMessage.setFrom(new InternetAddress(emailConfiguration.getUsername())); | 107 | mimeMessage.setFrom(new InternetAddress(emailConfiguration.getUsername())); |
118 | - setRecipients(emailReqDTO,mimeMessage); | 108 | + setRecipients(emailReqDTO, mimeMessage); |
119 | // 邮件主题 | 109 | // 邮件主题 |
120 | mimeMessage.setSubject(emailReqDTO.getSubject()); | 110 | mimeMessage.setSubject(emailReqDTO.getSubject()); |
121 | // 邮件内容部分 | 111 | // 邮件内容部分 |
@@ -129,40 +119,41 @@ public class TkMailServiceImpl implements TkMailService { | @@ -129,40 +119,41 @@ public class TkMailServiceImpl implements TkMailService { | ||
129 | Multipart multipart = new MimeMultipart(); | 119 | Multipart multipart = new MimeMultipart(); |
130 | multipart.addBodyPart(mimeBodyPart); | 120 | multipart.addBodyPart(mimeBodyPart); |
131 | 121 | ||
132 | - //如果有附件就添加附件 | ||
133 | - if(null != emailReqDTO.getFiles() && emailReqDTO.getFiles().length>0){ | ||
134 | - addFiles(multipart,emailReqDTO.getFiles()); | 122 | + // 如果有附件就添加附件 |
123 | + if (null != emailReqDTO.getFiles() && emailReqDTO.getFiles().length > 0) { | ||
124 | + addFiles(multipart, emailReqDTO.getFiles()); | ||
135 | } | 125 | } |
136 | mimeMessage.setContent(multipart); | 126 | mimeMessage.setContent(multipart); |
137 | mimeMessage.setSentDate(new Date()); | 127 | mimeMessage.setSentDate(new Date()); |
138 | Transport.send(mimeMessage); | 128 | Transport.send(mimeMessage); |
139 | remark = emailReqDTO.getRemark(); | 129 | remark = emailReqDTO.getRemark(); |
140 | result = true; | 130 | result = true; |
141 | - } catch (Exception exception){ | ||
142 | - log.error("send email failed",exception); | 131 | + } catch (Exception exception) { |
132 | + log.error("send email failed", exception); | ||
143 | remark = exception.getMessage(); | 133 | remark = exception.getMessage(); |
144 | } | 134 | } |
145 | - TkMailLogEntity mailLog = new TkMailLogEntity(); | ||
146 | - mailLog.setEmailFrom(emailConfiguration.getUsername()); | ||
147 | - mailLog.setEmailTo(JacksonUtil.toJsonNode(JacksonUtil.toString(emailReqDTO.getTo()))); | ||
148 | - mailLog.setEmailCc(JacksonUtil.toJsonNode(JacksonUtil.toString(emailReqDTO.getCc()))); | ||
149 | - mailLog.setEmailBcc(JacksonUtil.toJsonNode(JacksonUtil.toString(emailReqDTO.getBcc()))); | ||
150 | - mailLog.setEmailSubject(emailReqDTO.getSubject()); | ||
151 | - mailLog.setEmailBody(emailReqDTO.getBody()); | ||
152 | - mailLog.setTenantId(messageTemplate.getTenantId()); | ||
153 | - mailLog.setSendTime(LocalDateTime.now()); | ||
154 | - mailLog.setMessageTemplateId(messageTemplate.getId()); | ||
155 | - mailLog.setStatus(result ? ResponseCodeEnum.SUCCESS.name() : ResponseCodeEnum.FAILED.name()); | ||
156 | - mailLog.setRemark(remark); | ||
157 | - mailLog.setTemplatePurpose(emailReqDTO.getTemplatePurpose()); | ||
158 | - maiLogMapper.insert(mailLog); | ||
159 | - return result; | 135 | + TkMailLogEntity mailLog = new TkMailLogEntity(); |
136 | + mailLog.setEmailFrom(emailConfiguration.getUsername()); | ||
137 | + mailLog.setEmailTo(JacksonUtil.toJsonNode(JacksonUtil.toString(emailReqDTO.getTo()))); | ||
138 | + mailLog.setEmailCc(JacksonUtil.toJsonNode(JacksonUtil.toString(emailReqDTO.getCc()))); | ||
139 | + mailLog.setEmailBcc(JacksonUtil.toJsonNode(JacksonUtil.toString(emailReqDTO.getBcc()))); | ||
140 | + mailLog.setEmailSubject(emailReqDTO.getSubject()); | ||
141 | + mailLog.setEmailBody(emailReqDTO.getBody()); | ||
142 | + mailLog.setTenantId(messageTemplate.getTenantId()); | ||
143 | + mailLog.setSendTime(LocalDateTime.now()); | ||
144 | + mailLog.setMessageTemplateId(messageTemplate.getId()); | ||
145 | + mailLog.setStatus(result ? ResponseCodeEnum.SUCCESS.name() : ResponseCodeEnum.FAILED.name()); | ||
146 | + mailLog.setRemark(remark); | ||
147 | + mailLog.setTemplatePurpose(emailReqDTO.getTemplatePurpose()); | ||
148 | + maiLogMapper.insert(mailLog); | ||
149 | + return result; | ||
160 | } | 150 | } |
161 | return false; | 151 | return false; |
162 | } | 152 | } |
163 | 153 | ||
164 | /** | 154 | /** |
165 | * 添加接收人、抄送人、密送人 | 155 | * 添加接收人、抄送人、密送人 |
156 | + * | ||
166 | * @param emailReqDTO 邮件参数 | 157 | * @param emailReqDTO 邮件参数 |
167 | * @param mimeMessage 邮件消息体 | 158 | * @param mimeMessage 邮件消息体 |
168 | * @throws MessagingException 消息发送失败异常 | 159 | * @throws MessagingException 消息发送失败异常 |
@@ -173,12 +164,12 @@ public class TkMailServiceImpl implements TkMailService { | @@ -173,12 +164,12 @@ public class TkMailServiceImpl implements TkMailService { | ||
173 | InternetAddress[] toAddress = addInternetAddress(emailReqDTO.getTo()); | 164 | InternetAddress[] toAddress = addInternetAddress(emailReqDTO.getTo()); |
174 | mimeMessage.setRecipients(Message.RecipientType.TO, toAddress); | 165 | mimeMessage.setRecipients(Message.RecipientType.TO, toAddress); |
175 | 166 | ||
176 | - if(null != emailReqDTO.getCc() && emailReqDTO.getCc().length>0){ | 167 | + if (null != emailReqDTO.getCc() && emailReqDTO.getCc().length > 0) { |
177 | // 邮件抄送人多个 | 168 | // 邮件抄送人多个 |
178 | InternetAddress[] ccAddress = addInternetAddress(emailReqDTO.getCc()); | 169 | InternetAddress[] ccAddress = addInternetAddress(emailReqDTO.getCc()); |
179 | mimeMessage.setRecipients(Message.RecipientType.CC, ccAddress); | 170 | mimeMessage.setRecipients(Message.RecipientType.CC, ccAddress); |
180 | } | 171 | } |
181 | - if(null != emailReqDTO.getBcc() &&emailReqDTO.getBcc().length>0){ | 172 | + if (null != emailReqDTO.getBcc() && emailReqDTO.getBcc().length > 0) { |
182 | // 邮件密送人多个 | 173 | // 邮件密送人多个 |
183 | InternetAddress[] bccAddress = addInternetAddress(emailReqDTO.getBcc()); | 174 | InternetAddress[] bccAddress = addInternetAddress(emailReqDTO.getBcc()); |
184 | mimeMessage.setRecipients(Message.RecipientType.BCC, bccAddress); | 175 | mimeMessage.setRecipients(Message.RecipientType.BCC, bccAddress); |
@@ -187,14 +178,14 @@ public class TkMailServiceImpl implements TkMailService { | @@ -187,14 +178,14 @@ public class TkMailServiceImpl implements TkMailService { | ||
187 | 178 | ||
188 | /** | 179 | /** |
189 | * 添加收件人地址 | 180 | * 添加收件人地址 |
181 | + * | ||
190 | * @param content 接收人、抄送人、密送人数组 | 182 | * @param content 接收人、抄送人、密送人数组 |
191 | * @return 添加收件人地址 | 183 | * @return 添加收件人地址 |
192 | * @throws MessagingException 地址异常 | 184 | * @throws MessagingException 地址异常 |
193 | */ | 185 | */ |
194 | - private InternetAddress[] addInternetAddress(String[] content) | ||
195 | - throws MessagingException { | 186 | + private InternetAddress[] addInternetAddress(String[] content) throws MessagingException { |
196 | InternetAddress[] address = new InternetAddress[content.length]; | 187 | InternetAddress[] address = new InternetAddress[content.length]; |
197 | - for (int i = 0,j = content.length;i<j; i++) { | 188 | + for (int i = 0, j = content.length; i < j; i++) { |
198 | address[i] = new InternetAddress(content[i]); | 189 | address[i] = new InternetAddress(content[i]); |
199 | } | 190 | } |
200 | return address; | 191 | return address; |
@@ -202,12 +193,13 @@ public class TkMailServiceImpl implements TkMailService { | @@ -202,12 +193,13 @@ public class TkMailServiceImpl implements TkMailService { | ||
202 | 193 | ||
203 | /** | 194 | /** |
204 | * 添加附件 | 195 | * 添加附件 |
196 | + * | ||
205 | * @param multipart Multipart | 197 | * @param multipart Multipart |
206 | * @param files 附件 | 198 | * @param files 附件 |
207 | * @throws MessagingException 消息发送失败异常 | 199 | * @throws MessagingException 消息发送失败异常 |
208 | * @throws IOException IO流异常 | 200 | * @throws IOException IO流异常 |
209 | */ | 201 | */ |
210 | - private void addFiles(Multipart multipart,File[] files) throws MessagingException, IOException { | 202 | + private void addFiles(Multipart multipart, File[] files) throws MessagingException, IOException { |
211 | for (File file : files) { | 203 | for (File file : files) { |
212 | MimeBodyPart attachment = new MimeBodyPart(); | 204 | MimeBodyPart attachment = new MimeBodyPart(); |
213 | attachment.attachFile(file); | 205 | attachment.attachFile(file); |