一、定时器的实现(task)
1、在XML配置文件中引入约束
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
2、编写定时器类,和方法
3、使用xml配置文件
加载配置文件就可以执行定时任务了,图为两秒执行一次。
二、邮件发送
1、Java mail发送邮件
1) Maven项目引入java mail坐标,也可以手动安装
package com.shsxt.service; import org.apache.commons.lang3.StringUtils; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.*; import java.io.File; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.util.Properties; public class MailService { public void mail(String fromUser, String[] receivers, String setSubject, String content, String[] files) { // 判断参数是否为空 if (!judgment(fromUser, receivers, setSubject, content)){ return; } // 获取session Session session = init(fromUser); Message message = new MimeMessage(session); // 发送人 sender(fromUser, message); // 收件人 recipients(receivers, message); // 主题 subject(message,setSubject); // 设置html和附件 Multipart multipart = new MimeMultipart(); // html内容 Content(content, multipart); // 判断是否有附件 if (null != files && files.length > 0) { // 上传附件 fileUp(files, multipart); } settingContext(message, multipart); // 发送 send(message); } // 8、发送 private void send(Message message) { try { Transport.send(message); } catch (MessagingException e) { e.printStackTrace(); } } // 7、设置上下文 private void settingContext(Message message, Multipart multipart) { try { message.setContent(multipart); } catch (MessagingException e) { e.printStackTrace(); } } // 6、设置主题 private void subject(Message message,String setSubject) { try { message.setSubject(setSubject); } catch (MessagingException e) { e.printStackTrace(); } } // 5、上传附件 private void fileUp(String[] files, Multipart multipart) { try { for (String filePath : files) { BodyPart file = new MimeBodyPart(); File temp = new File(filePath); file.setDataHandler(new DataHandler(new FileDataSource(new File(filePath)))); file.setFileName(MimeUtility.encodeText(temp.getName())); multipart.addBodyPart(file); } } catch (Exception e) { e.printStackTrace(); } } // 4、设置html内容 private void Content(String content, Multipart multipart) { try { BodyPart html = new MimeBodyPart(); html.setContent(content, "text/html;charset=utf-8"); multipart.addBodyPart(html); } catch (MessagingException e) { e.printStackTrace(); } } // 3、设置收件人 private void recipients(String[] receivers, Message message) { try { Address[] addresses = new InternetAddress[receivers.length]; for (int i = 0; i < addresses.length; i++) { addresses[i] = new InternetAddress(receivers[i]); } message.setRecipients(Message.RecipientType.TO, addresses); } catch (MessagingException e) { e.printStackTrace(); } } // 2、设置发送人 private void sender(String fromUser, Message message) { try { Address from = new InternetAddress(fromUser); message.setFrom(from); } catch (Exception e) { e.printStackTrace(); } } // 1、初始化 private Session init(String fromUser) { Properties properties = new Properties(); // 邮箱服务器主机 properties.put("mail.smtp.host", "smtp.163.com"); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.port", 25); Session session = Session.getDefaultInstance(properties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("15657262398@163.com", "d140120"); } }); return session; } // 0、判断参数是否为空 private boolean judgment(String fromUser, String[] receivers, String setSubject, String content) { boolean flag = true; if (StringUtils.isBlank(fromUser) || StringUtils.isAnyBlank(receivers) || StringUtils.isBlank(setSubject) || StringUtils.isBlank(content)) { flag = false; } return flag; } }
2、spring环境下发送邮件
XML配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.mail"/> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.163.com"/><!--发送email的邮件服务器主机名--> <property name="username" value="test@163.com"/><!--用户名--> <property name="password" value="test"/><!--授权码--> </bean> <bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage"> <property name="subject" value="hello mail"/> <property name="from" value="15657262398@163.com"/> </bean> </beans>
装配和使用邮件发送器
package com.mail; import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class OrderManagerImpl implements OrderManager { @Resource private MailSender mailSender; @Resource private SimpleMailMessage simpleMailMessage; @Override public void placeOrder() { SimpleMailMessage msg=new SimpleMailMessage(this.simpleMailMessage); msg.setTo("test@163.com"); msg.setText("hello"); this.mailSender.send(msg); } }