|
6 | 6 | import org.apache.commons.lang3.StringUtils; |
7 | 7 | import org.apache.commons.mail.DefaultAuthenticator; |
8 | 8 | import org.apache.commons.mail.Email; |
| 9 | +import org.apache.commons.mail.EmailAttachment; |
9 | 10 | import org.apache.commons.mail.EmailException; |
10 | 11 | import org.apache.commons.mail.HtmlEmail; |
| 12 | +import org.apache.commons.mail.MultiPartEmail; |
11 | 13 | import org.apache.commons.mail.SimpleEmail; |
12 | 14 | import org.slf4j.Logger; |
13 | 15 | import org.slf4j.LoggerFactory; |
@@ -114,6 +116,122 @@ public static void sendContextEmail(String context, String subject, |
114 | 116 |
|
115 | 117 | } |
116 | 118 |
|
| 119 | + /** |
| 120 | + * 发送带符件邮件的简单文本邮件 |
| 121 | + * |
| 122 | + * @param context |
| 123 | + * 邮件文本 |
| 124 | + * @param subject |
| 125 | + * 主题 |
| 126 | + * @param toEmail |
| 127 | + * 发送给谁的邮件地址 |
| 128 | + * @param toEmailName |
| 129 | + * 名称 |
| 130 | + * @throws EmailException |
| 131 | + */ |
| 132 | + public static void sendMultiPartEmail(String context, String subject, |
| 133 | + String toEmail, String toEmailName, EmailAttachment emailAttachment) |
| 134 | + throws EmailException { |
| 135 | + if (StringUtils.isBlank(context)) { |
| 136 | + throw new EmailException("邮件文本不能为空"); |
| 137 | + } |
| 138 | + if (StringUtils.isBlank(toEmail)) { |
| 139 | + throw new EmailException("邮件接收人不能为空"); |
| 140 | + } |
| 141 | + if (StringUtils.isBlank(subject)) { |
| 142 | + throw new EmailException("邮件主题不能为空"); |
| 143 | + } |
| 144 | + if (emailAttachment != null) { |
| 145 | + throw new EmailException("附件不能为空"); |
| 146 | + } |
| 147 | + |
| 148 | + try { |
| 149 | + MultiPartEmail email = (MultiPartEmail) getDefaultEmailConfig(new MultiPartEmail()); |
| 150 | + email.setSubject(subject); |
| 151 | + email.setMsg(toEmail); |
| 152 | + if (StringUtils.isBlank(toEmailName)) { |
| 153 | + // 接收人 |
| 154 | + email.addTo(toEmail); |
| 155 | + } else { |
| 156 | + // 接收人 |
| 157 | + email.addTo(toEmail, toEmailName);// 1067165280@qq.com |
| 158 | + // rui_dev@126.com |
| 159 | + } |
| 160 | + email.attach(emailAttachment); |
| 161 | + email.send(); |
| 162 | + } catch (EmailException e) { |
| 163 | + logger.error("邮件发送异常!{}" + e.getMessage()); |
| 164 | + e.printStackTrace(); |
| 165 | + } |
| 166 | + |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * 发送带符件邮件和ftl模板邮件 |
| 171 | + * |
| 172 | + * @param map |
| 173 | + * ftl 对应数据 |
| 174 | + * @param subject |
| 175 | + * 主题 |
| 176 | + * @param toEmail |
| 177 | + * 发送给谁的邮件地址 |
| 178 | + * @param toEmailName |
| 179 | + * 名称 |
| 180 | + * @param EmailAttachment |
| 181 | + * 附件 |
| 182 | + * @throws EmailException |
| 183 | + */ |
| 184 | + public static void sendMultiPartEmailAndFtl(Map<String, Object> map, |
| 185 | + String ftlName, String subject, String toEmail, String toEmailName, |
| 186 | + EmailAttachment emailAttachment) throws EmailException { |
| 187 | + if (StringUtils.isBlank(toEmail)) { |
| 188 | + throw new EmailException("邮件接收人不能为空"); |
| 189 | + } |
| 190 | + if (StringUtils.isBlank(subject)) { |
| 191 | + throw new EmailException("邮件主题不能为空"); |
| 192 | + } |
| 193 | + if (emailAttachment != null) { |
| 194 | + throw new EmailException("附件不能为空"); |
| 195 | + } |
| 196 | + |
| 197 | + try { |
| 198 | + FreeMarkerConfigurer configurer = FreeMarkerUtil |
| 199 | + .getFreeMarkerConfigurer(); |
| 200 | + Template tpl = configurer.getConfiguration().getTemplate(ftlName); |
| 201 | + // if (map==null||map.size()<=0) { |
| 202 | + // } |
| 203 | + String ftlContext = FreeMarkerTemplateUtils |
| 204 | + .processTemplateIntoString(tpl, map); |
| 205 | + System.out.println(ftlContext); |
| 206 | + HtmlEmail email = (HtmlEmail) getDefaultEmailConfig(new HtmlEmail()); |
| 207 | + // MimeMultipart multipart = new MimeMultipart("related"); |
| 208 | + email.setHtmlMsg(ftlContext); |
| 209 | + // email.setTextMsg("xx");//文本消息的一部分。如果这将被用作替代文本 电子邮件客户端不支持HTML消息。 |
| 210 | + email.setSubject(subject); |
| 211 | + // email.setMsg(toEmail); |
| 212 | + if (StringUtils.isBlank(toEmailName)) { |
| 213 | + // 接收人 |
| 214 | + email.addTo(toEmail); |
| 215 | + } else { |
| 216 | + // 接收人 |
| 217 | + email.addTo(toEmail, toEmailName);// 1067165280@qq.com |
| 218 | + // rui_dev@126.com |
| 219 | + } |
| 220 | + email.attach(emailAttachment); |
| 221 | + email.send(); |
| 222 | + } catch (IOException e) { |
| 223 | + logger.error("freeMaraker模板加载异常!{}" + e.getMessage()); |
| 224 | + e.printStackTrace(); |
| 225 | + } catch (EmailException e) { |
| 226 | + logger.error("邮件发送异常!{}" + e.getMessage()); |
| 227 | + e.printStackTrace(); |
| 228 | + } catch (TemplateException e) { |
| 229 | + logger.error("processTemplateIntoString处理异常!{}" + e.getMessage()); |
| 230 | + e.printStackTrace(); |
| 231 | + } |
| 232 | + |
| 233 | + } |
| 234 | + |
117 | 235 | /** |
118 | 236 | * 发送简单文本邮件 |
119 | 237 | * |
@@ -175,7 +293,7 @@ public static void sendFtlEmail(Map<String, Object> map, String ftlName, |
175 | 293 | email.setHtmlMsg(ftlContext); |
176 | 294 | // email.setTextMsg("xx");//文本消息的一部分。如果这将被用作替代文本 电子邮件客户端不支持HTML消息。 |
177 | 295 | email.setSubject(subject); |
178 | | - //email.setMsg(toEmail); |
| 296 | + // email.setMsg(toEmail); |
179 | 297 | if (StringUtils.isBlank(toEmailName)) { |
180 | 298 | // 接收人 |
181 | 299 | email.addTo(toEmail); |
|
0 commit comments