首页 > 代码库 > Spring 发送邮件
Spring 发送邮件
这是很久之前的代码了,最近需要使用了,了就记录一下。
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="http://www.mamicode.com/#{configproperties[‘baojing.mail.host‘]}" /> <property name="protocol" value="http://www.mamicode.com/#{configproperties[‘baojing.mail.protocol‘]}"></property> <!-- 端口号 --> <property name="port" value="http://www.mamicode.com/#{configproperties[‘baojing.mail.port‘]}" /> <!-- 用户名 --> <!-- <property name="username" value="http://www.mamicode.com/#{configproperties[‘baojing.mail.username‘]}" /> --> <!-- 密码 --> <!-- <property name="password" value="http://www.mamicode.com/#{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="http://www.mamicode.com/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(); } } }
本文出自 “梦朝思夕” 博客,请务必保留此出处http://qiangmzsx.blog.51cto.com/2052549/1859044
Spring 发送邮件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。