1、pom.xml
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
2、设置调度任务
package com.zhiyou.job;
import java.util.List;
import javax.annotation.Resource;
import java.security.GeneralSecurityException;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import com.sun.mail.util.MailSSLSocketFactory;
public class JobA {
public void SayHello() throws Exception {
Properties prop = new Properties();
// 开启debug调试,以便在控制台查看
prop.setProperty("mail.debug", "true");
// 设置邮件服务器主机名
prop.setProperty("mail.host", "smtp.163.com");
// 发送服务器需要身份验证
prop.setProperty("mail.smtp.auth", "true");
// 发送邮件协议名称
prop.setProperty("mail.transport.protocol", "smtp");
// 开启SSL加密,否则会失败
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
// 创建session
Session session = Session.getInstance(prop);
// 通过session得到transport对象
Transport ts = session.getTransport();
// 连接邮件服务器:邮箱类型,帐号,授权码代替密码(更安全)
ts.connect("smtp.163.com","213454643@163.com", "fdslfjsdsd");
// 创建邮件
// 创建邮件对象
MimeMessage message = new MimeMessage(session);
// 指明邮件的发件人
message.setFrom(new InternetAddress("213454643@163.com"));
// 指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
message.setRecipient(Message.RecipientType.TO, new InternetAddress("766546345@163.com"));
// 邮件的标题
message.setSubject("京东邀请函");
// 邮件的文本内容 可以写html代码
message.setContent(""
+ "<h1><font color=black>"
+ "京东邀请你参加京东9月20日,京东商品节!更多优惠商品等你来买,买的越多优惠越多!赶快行动吧!"
+ "</font></h1>"
+ "", "text/html;charset=UTF-8");
// 发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
}
3、ApplicationContext.xml
约束文件:
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
<bean id="jobA" class="com.zhiyou.job.JobA"></bean>
<task:scheduled-tasks>
<task:scheduled ref="jobA" method="SayHello" cron="*/1 * * * * ?"/>
</task:scheduled-tasks>
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/Address
把这个删掉