这是很久之前的代码了,最近需要使用了,了就记录一下。
maven:
<dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.5.5</version> </dependency>
spring配置:
<!-- 邮件 --> <bean id="baojingMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <!-- 服务器 --> <property name="host" value="#{configproperties[‘baojing.mail.host‘]}" /> <property name="protocol" value="#{configproperties[‘baojing.mail.protocol‘]}"></property> <!-- 端口号 --> <property name="port" value="#{configproperties[‘baojing.mail.port‘]}" /> <!-- 用户名 --> <!-- <property name="username" value="#{configproperties[‘baojing.mail.username‘]}" /> --> <!-- 密码 --> <!-- <property name="password" value="#{configproperties[‘baojing.mail.password‘]}" /> --> <!-- SMTP服务器验证 --> <property name="javaMailProperties"> <props> <!-- 验证身份 --> <prop key="mail.smtp.auth">{configproperties[‘baojing.mail.smtp.auth‘]}</prop> <prop key="mail.smtp.timeout">#{configproperties[‘baojing.mail.smtp.timeout‘]}</prop> </props> </property> </bean>
配置:
baojing.mail.host = mail2-in.XXXX.com baojing.mail.port = 25 baojing.mail.protocol = smtp baojing.mail.username = "" baojing.mail.password = "" baojing.mail.smtp.auth = false baojing.mail.smtp.timeout = 25000
代码:
@Autowired @Qualifier(value="baojingMailSender") private JavaMailSender baojingMailSender; /** * 发邮件 * **/ @SuppressWarnings("static-access") public void sendMail(final Map<String, Map<String, String>> message){ Map<String, String> dayDiffMap = message.get("day"); Map<String, String> hourDiffMap = message.get("hour"); Map<String, String> defaultMap = new HashMap<String, String>(); defaultMap.put("ratio", "0"); defaultMap.put("diffCount", "0"); defaultMap.put("totleCount", "0"); defaultMap.put("todayFile", "0"); defaultMap.put("diffFile", "0"); //避免空指向 if (dayDiffMap == null || dayDiffMap.isEmpty()) { dayDiffMap = defaultMap; } if (hourDiffMap == null || hourDiffMap.isEmpty() ) { hourDiffMap = defaultMap; } if (VoiceConstans.isSendMail && (Double.valueOf(dayDiffMap.get("ratio")) >= VoiceConstans.dayThreshold || Double.valueOf(hourDiffMap.get("ratio")) >= VoiceConstans.hourThreshold)) { String dayRatio = new String().format("%.2f", (Double.valueOf(dayDiffMap.get("ratio")) *100)) + "%"; String hourRatio = new String().format("%.2f", (Double.valueOf(hourDiffMap.get("ratio")) *100)) + "%"; try { //HTML MimeMessage mimeMessage = baojingMailSender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8"); String [] mailto = new String[VoiceConstans.mailTo.size()]; messageHelper.setTo(VoiceConstans.mailTo.toArray(mailto)); messageHelper.setFrom(VoiceConstans.mailFrom); messageHelper.setSubject(VoiceConstans.mailSubject+" ("+DateUtil.format(new Date(), VoiceConstans.HOURFORMAT)+")"); StringBuffer htmlBuffer = new StringBuffer(); htmlBuffer.append("HI,ALL:<br>"); htmlBuffer.append(DateUtil.format(new Date(), "yyyy-MM-dd HH时")); htmlBuffer.append("数据如下:<br>"); htmlBuffer.append("总数:"+dayDiffMap.get("totleCount")+"<br>"); htmlBuffer.append("<table border=\"1\"><thead><tr><th>同日DIFF数</th><th>同日比</th><th>相邻DIFF数</th><th>相邻比</th></thead><tbody><tr>"); htmlBuffer.append("<td>"+dayDiffMap.get("diffCount")+"</td>");//同日当前diff数 htmlBuffer.append("<td>"+dayRatio+"</td>");//同日比 htmlBuffer.append("<td>"+hourDiffMap.get("diffCount")+"</td>");//相邻当前diff数 htmlBuffer.append("<td>"+hourRatio+"</td>");//相邻比 htmlBuffer.append("</tr></tbody></table><br><br>"); LOGGER.info("发邮件: "+ htmlBuffer.toString()); messageHelper.setText(htmlBuffer.toString(),true); baojingMailSender.send(mimeMessage); } catch (MessagingException e) { e.printStackTrace(); } } }
时间: 2024-10-11 18:04:19