一、QQ邮箱–>设置–>账户–>开启POP3/SMTP服务–>短信验证–>获取授权码
二、SSM项目代码(Spring、SpringMVC、Mybatis)
1.pom.xml
<!-- mail begin --> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>activation</artifactId> <version>1.0.2</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency> <!-- mail end --> <!-- excel begin --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> <!-- excel end --> <!-- 定时任务begin --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.3.0</version> </dependency> <!-- 定时任务end -->
2.springmvc-context.xml
<!-- 定时器begin --> <!-- 调度业务对象 即自己的定时任务类--> <bean id = "spiderJob" class="com.dahata.controller.JobController"/> <!-- 调度业务 --> <bean id = "jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name= "targetObject" ref="spiderJob"/> <property name ="targetMethod" value="startWork"/> </bean> <!-- 调度器触发器 每天早上08:00执行一次 --> <bean id="taskTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="jobtask"/> <property name="cronExpression" value="0 00 08 * * ? *"/> </bean> <!-- 设置调度 list里面可以添加多个调度触发器 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="taskTrigger" /> </list> </property> </bean> <!-- 定时器end -->
3.Java代码
package com.dahata.controller; import java.security.GeneralSecurityException; import java.util.List; import java.util.Properties; import javax.activation.DataHandler; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; 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 org.springframework.beans.factory.annotation.Autowired; import com.dahata.bean.HeJiaProcess; import com.dahata.service.ProcessService; import com.sun.mail.util.MailSSLSocketFactory; public class JobController { @Autowired private ProcessService processService; public void startWork() throws GeneralSecurityException, MessagingException { List<HeJiaProcess> pros = processService.getHeJiaProcess(); Properties props = new Properties(); // 开启debug调试 //props.setProperty("mail.debug", "true"); // 发送服务器需要身份验证 props.setProperty("mail.smtp.auth", "true"); // 设置邮件服务器主机名 props.setProperty("mail.host", "smtp.qq.com"); // 发送邮件协议名称 props.setProperty("mail.transport.protocol", "smtp"); // 开启加密,否则会失败(SSL) MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.ssl.socketFactory", sf); // 创建session Session session = Session.getInstance(props); // 创建邮件 Message msg = new MimeMessage(session); // 设置标题 msg.setSubject("PLM核价进度和SAP资料维护通知"); // 编辑内容 StringBuilder content = new StringBuilder(); content.append("<html><head>"); content.append("<style type="text/css">"); content.append("table.gridtable {font-family: verdana,arial,sans-serif;font-size:11px;color:#333333;border- 1px;border-color: #666666;border-collapse: collapse;}"); content.append("table.gridtable th {border- 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #dedede;}"); content.append("table.gridtable td {border- 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;}"); content.append("</style>"); content.append("</head>"); content.append("<body>"); content.append("<p>你好:</p>"); content.append("<p> 以下是料号申请未处理的流程,请查阅,谢谢!</p>"); content.append("<table border="1" cellspacing="0" class="gridtable""); content.append("<tr><th>流程编码</th><th>流程名称</th><th>节点名称</th><th>是否签核</th><th>节点开始时间</th><th>停留时间(天)</th></tr>"); for(HeJiaProcess pro : pros) { content.append("<tr>"); content.append("<td>"+pro.getBaseCode()+"</td>"); content.append("<td>"+pro.getBaseName()+"</td>"); content.append("<td>"+pro.getText()+"</td>"); content.append("<td>"+pro.getYesOrNo()+"</td>"); content.append("<td>"+pro.getStartDate()+"</td>"); content.append("<td>"+pro.getStopDate()+"</td>"); content.append("</tr>"); } content.append("</table>"); content.append("</body>"); content.append("</html>"); // 设置内容 Multipart mp = new MimeMultipart("related"); BodyPart bodyPart = new MimeBodyPart(); bodyPart.setDataHandler(new DataHandler(content.toString(),"text/html;charset=UTF-8")); mp.addBodyPart(bodyPart); msg.setContent(mp); // 发送的邮箱地址 msg.setFrom(new InternetAddress("发送者邮箱")); // 收件的邮箱地址 InternetAddress[] sendTo = InternetAddress.parse("收件人邮箱1,收件人邮箱2"); msg.setRecipients(Message.RecipientType.TO, sendTo); // 通过session得到transport对象 Transport transport = session.getTransport(); // 连接邮件服务器(邮箱类型,帐号,授权码) transport.connect("smtp.qq.com", "发送者邮箱", "授权码"); // 发送邮件 transport.sendMessage(msg, msg.getAllRecipients()); // 关闭连接 transport.close(); } }
运行效果: