Hello Guys !! 我开发了一个小应用程序,用于在单击提交按钮时将邮件发送到特定ID。现在根据我的需要:
所以我需要的是自动完成我的过程。
任何建议都将受到高度赞赏..
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {final String err = "/error.jsp";
final String succ = "/success.jsp";
String to = request.getParameter("to");
String subject = request.getParameter("subject");
String message = request.getParameter("message");
String login = request.getParameter("login");
String password = request.getParameter("password");
try {
Properties props = new Properties();
props.setProperty("mail.host", "smtp.gmail.com");
props.setProperty("mail.smtp.port", "587");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.starttls.enable", "true");
Authenticator auth = new SMTPAuthenticator(login, password);
Session session = Session.getInstance(props, auth);
MimeMessage msg = new MimeMessage(session);
msg.setText(message);
msg.setSubject(subject);
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
Transport.send(msg);
} catch (AuthenticationFailedException ex) {
request.setAttribute("ErrorMessage", "Authentication failed");
RequestDispatcher dispatcher = request.getRequestDispatcher(err);
dispatcher.forward(request, response);
} catch (AddressException ex) {
request.setAttribute("ErrorMessage", "Wrong email address");
RequestDispatcher dispatcher = request.getRequestDispatcher(err);
dispatcher.forward(request, response);
} catch (MessagingException ex) {
request.setAttribute("ErrorMessage", ex.getMessage());
RequestDispatcher dispatcher = request.getRequestDispatcher(err);
dispatcher.forward(request, response);
}
RequestDispatcher dispatcher = request.getRequestDispatcher(succ);
dispatcher.forward(request, response);
}
private class SMTPAuthenticator extends Authenticator {
private PasswordAuthentication authentication;
public SMTPAuthenticator(String login, String password) {
authentication = new PasswordAuthentication(login, password);
}
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
答案 0 :(得分:2)
查看Quartz调度程序Java库。它具有广泛的配置和设置选项,并且将涵盖从最简单(例如类似于标准Java Timer
)到复杂的类似cron行为的用例。
Quartz是一个功能齐全的开源作业调度服务 与几乎任何Java EE或Java集成或一起使用 SE应用程序 - 从最小的独立应用程序到 最大的电子商务系统。 Quartz可用于创建简单或 执行数十,数百甚至甚至的复杂计划 成千上万的工作;任务被定义为标准的作业 Java组件,可以执行您可能编程的几乎任何内容 他们这样做。
答案 1 :(得分:0)
除Quartz外,还有Timer API(http://java.sun.com/j2se/1.4.2/docs/api/java/util/Timer.html)
A facility for threads to schedule tasks for future execution in a background thread. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals.
您将计时器代码放入添加到您的新servlet中 Web应用程序。或者将您的代码添加到webapp中的现有servlet中。
还有很多其他开源调度API ......
答案 2 :(得分:0)
维卡斯,
您必须使用计划机制来启动电子邮件作业。另外,我建议你将mail-job程序保存在一个单独的java类中,并使用servlet中的Scheduling代码。
答案 3 :(得分:0)
public class ClassExecutingTask {
long delayfornextstart = 60*60*24*7*1000; // delay in ms : 10 * 1000 ms = 10 sec.
LoopTask tasktoexecute = new LoopTask();
Timer timer = new Timer("TaskName");
public void start() throws ParseException, InterruptedException {
timer.cancel();
timer = new Timer("TaskName");
//@SuppressWarnings("deprecation")
//Date executionDate = new Date(2013-05-04); // no params = now
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("waiting for the rght day to come");
Date executionDate = sdf.parse("2014-04-03");
Date date1 = sdf.parse("2014-04-07");
Date date2 = sdf.parse("2014-04-07");
System.out.println(date1+"and"+date2);
long waitTill=getTimeDiff(date2,date1);
if(date2==date1)
{
waitTill=0;
System.out.println(waitTill);
}
System.out.println(waitTill);
Thread.sleep(waitTill);
timer.scheduleAtFixedRate(tasktoexecute, executionDate, delayfornextstart);
}
private class LoopTask extends TimerTask {
public void run() {
ExcelReadExample EE=new ExcelReadExample();
try {
EE.ReadFile();//ur Mail code or the method which send the MAil
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws ParseException, InterruptedException {
ClassExecutingTask executingTask = new ClassExecutingTask();
executingTask.start();
}
public static long getTimeDiff(Date dateOne, Date dateTwo) {
String diff = "";
long timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime());
diff = String.format("%d hour(s) %d min(s)", TimeUnit.MILLISECONDS.toHours(timeDiff),
TimeUnit.MILLISECONDS.toMinutes(timeDiff) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(timeDiff)));
return timeDiff;
}
}