package com.action.indices;import java.util.Timer;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class MyTimerTask implements ServletContextListener { private Timer timer = null; public void contextDestroyed(ServletContextEvent event) { timer.cancel(); event.getServletContext().log("定时器销毁"); } public void contextInitialized(ServletContextEvent event) { //在这里初始化监听器,在tomcat启动的时候监听器启动,可以在这里实现定时器功能 timer = new Timer(true); event.getServletContext().log("定时器已启动");//添加日志,可在tomcat日志中查看到 //调用exportHistoryBean,0表示任务无延迟,5*1000表示每隔5秒执行任务,60*60*1000表示一个小时; timer.schedule(new SendEmail(event.getServletContext()),0,10000); } }===================================================package com.action.indices;import java.util.Date;import java.util.Properties;import java.util.TimerTask;import javax.mail.BodyPart;import javax.mail.Message;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 javax.servlet.ServletContext;public class SendEmail extends TimerTask { private ServletContext context = null; public SendEmail(ServletContext context) { this.context = context; } @Override public void run() { try{ /* * 以下为javamail的邮件发送 */ System.out.println("正在发送邮件"); Properties props=new Properties(); props.put("mail.smtp.host","smtp.qq.com");//发件人使用发邮件的电子信箱服务器我使用的是163的服务器 props.put("mail.smtp.auth","true"); //这样才能通过验证 Session s=Session.getInstance(props); s.setDebug(true); MimeMessage message=new MimeMessage(s); //给消息对象设置发件人/收件人/主题/发信时间 InternetAddress from=new InternetAddress("799788525@qq.com"); //发邮件的出发地(发件人的信箱),这是我的邮箱地址,使用请改成你的有效地址 message.setFrom(from); String tto = "799788525@qq.com"; InternetAddress to=new InternetAddress(tto);// tto为发邮件的目的地(收件人信箱) System.out.println("正在发送邮件2"); message.setRecipient(Message.RecipientType.TO,to); String ttitle = "邮件测试使用"; message.setSubject(ttitle);// ttitle为邮件的标题 message.setSentDate(new Date()); BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象 System.out.println("正在发送邮件3"); String tcontent = "附件测试用"; mdp.setContent(tcontent,"text/html;charset=utf-8");//给BodyPart对象设置内容和格式/编码方式tcontent为邮件内容 Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对 //象(事实上可以存放多个) mm.addBodyPart(mdp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart) message.setContent(mm);//把mm作为消息对象的内容 System.out.println("正在发送邮件4"); message.saveChanges(); Transport transport=s.getTransport("smtp"); System.out.println("正在发送邮件5"); transport.connect("smtp.qq.com","799788525","15824619896");//发邮件人帐户密码,此外是我的帐户密码,使用时请修改。 System.out.println("正在发送邮件66"); transport.sendMessage(message,message.getAllRecipients()); System.out.println("正在发送邮件6"); transport.close(); }catch(Exception e){ e.printStackTrace(); } }}============================= <!-- 邮件使用 --> <listener> <listener-class>com.action.indices.MyTimerTask</listener-class> </listener> 哪里有错误,请大神指点!下面面是执行结果和错误信息:【做的是定时发送】============================正在发送邮件DEBUG: setDebug: JavaMail version 1.5.0-b01正在发送邮件2正在发送邮件3正在发送邮件4DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]正在发送邮件5DEBUG SMTP: useEhlo true, useAuth trueDEBUG SMTP: trying to connect to host "smtp.qq.com", port 25, isSSL false220 ********************************DEBUG SMTP: connected to host "smtp.qq.com", port: 25EHLO WIN-6EEE2PLGBOR250-smtp.qq.com250-PIPELINING250-SIZE 73400320250-XXXXXXXA250-AUTH LOGIN PLAIN250-AUTH=LOGIN250-XXXXXXXXXXXB250 8BITMIMEDEBUG SMTP: Found extension "PIPELINING", arg ""DEBUG SMTP: Found extension "SIZE", arg "73400320"DEBUG SMTP: Found extension "XXXXXXXA", arg ""DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"DEBUG SMTP: Found extension "AUTH=LOGIN", arg ""DEBUG SMTP: Found extension "XXXXXXXXXXXB", arg ""DEBUG SMTP: Found extension "8BITMIME", arg ""DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM DEBUG SMTP: AUTH LOGIN command trace suppressedDEBUG SMTP: EOF: [EOF]DEBUG SMTP: AUTH LOGIN failedjavax.mail.AuthenticationFailedException: [EOF] at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826) at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685) at javax.mail.Service.connect(Service.java:295) at javax.mail.Service.connect(Service.java:176) at com.cmcc.bdp.action.indices.SendEmail.run(SendEmail.java:63) at java.util.TimerThread.mainLoop(Unknown Source) at java.util.TimerThread.run(Unknown Source)============================
添加回答
举报
0/150
提交
取消