1.新建web project。
2.需要的jar包
activation-1.1.jar
commons-logging-1.0.4.jar
mail-1.4.jar
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.transaction-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
quartz-1.6.6.jar
spring-context-support-3.0.5.RELEASE.jar
spring-core-3.0.5.RELEASE.jar
3.web.xml中的配置如下内容:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:resources/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
4.新建包名resources,在此包下新建applicationContext.xml和mail.properties文件
applicationContext.xml内容如下:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:resources/mail.properties</value>
</list>
</property>
</bean>
<bean id="javaMailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<!-- 指发送者 -->
<property name="host">
<value>${mail.host}</value>
</property>
<property name="username">
<value>${mail.username}</value>
</property>
<property name="password">
<value>${mail.password}</value>
</property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
</props>
</property>
</bean>
<!-- 定义目标bean和bean中的方法 -->
<bean id="QuzJobMailSend"
class="com.java.mail.job.QuzJobMailSend">
<property name="javaMailSender">
<ref bean="javaMailSender" />
</property>
</bean>
<bean id="SpringQtzJobMethod"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="QuzJobMailSend" />
</property>
<property name="targetMethod"><!-- 要执行的方法名称 -->
<value>execute</value>
</property>
</bean>
<!-- 调度器触发 -->
<bean id="CronTriggerBean"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="SpringQtzJobMethod"></property>
<property name="cronExpression" value="0 0/5 * * * ?"></property>
</bean>
<!-- 调度工厂 -->
<bean id="SpringJobSchedulerFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="CronTriggerBean" />
</list>
</property>
</bean>
mail.properties内容如下:
mail.host=smtp.163.com
mail.username=邮件发送人地址
mail.password=邮件发送人密码
5.新建com.java.mail.job包,在此包下新建QuzJobMailSend类
private JavaMailSender javaMailSender;
public JavaMailSender getJavaMailSender() {
return javaMailSender;
}
public void setJavaMailSender(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
public void execute() {
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
// 设置utf-8或GBK编码,否则邮件会有乱码,true表示为multipart邮件
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8");
messageHelper.setFrom("邮件发送人地址 要与配置文件中设置的相同
messageHelper.setTo(“邮件接受人地址”);
// messageHelper.setBcc(new InternetAddress("设置抄送的邮箱", "接收人", "utf-8"));// 设置抄送
messageHelper.setSubject("测试");// 邮件标题
messageHelper.setText("请查收附件!", true);// 邮件类容,ture 表示启用html
//带附件
File file = new File("D:/11888.log");
messageHelper.addAttachment(MimeUtility.encodeText(file.getName()), file);
javaMailSender.send(mimeMessage);
System.out.println("发送成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
6.目录如下:
cronExpression表达式需要慢慢理解。
爱生活,爱分享,爱康宝!