81,122
社区成员




public static void main(String[] args) {
Timer timer = new Timer();
Calendar calendar = Calendar.getInstance();
calendar.set(2014, 04, 9, 17, 16, 00);
Date date = calendar.getTime();
timer.schedule(new SendMail(), date);
}
执行这个main方法会再2014.05.09 17:16:00调用你的run方法
[/quote]
这里是不可能用servlet去接受网页传过来的信息的,你一定要用servlet吗?[/quote]
我做的是日程管理网站,希望做一个定时发送给用户发送提醒邮件的功能,只能从网页接收传过来的提醒时间和日程内容啊。[/quote] 以前做过一次[/quote]
能发给我看看,借鉴一下吗?
public static void main(String[] args) {
Timer timer = new Timer();
Calendar calendar = Calendar.getInstance();
calendar.set(2014, 04, 9, 17, 16, 00);
Date date = calendar.getTime();
timer.schedule(new SendMail(), date);
}
执行这个main方法会再2014.05.09 17:16:00调用你的run方法
[/quote]
这里是不可能用servlet去接受网页传过来的信息的,你一定要用servlet吗?[/quote]
我做的是日程管理网站,希望做一个定时发送给用户发送提醒邮件的功能,只能从网页接收传过来的提醒时间和日程内容啊。[/quote] 以前做过一次
public static void main(String[] args) {
Timer timer = new Timer();
Calendar calendar = Calendar.getInstance();
calendar.set(2014, 04, 9, 17, 16, 00);
Date date = calendar.getTime();
timer.schedule(new SendMail(), date);
}
执行这个main方法会再2014.05.09 17:16:00调用你的run方法
[/quote]
这里是不可能用servlet去接受网页传过来的信息的,你一定要用servlet吗?[/quote]
我做的是日程管理网站,希望做一个定时发送给用户发送提醒邮件的功能,只能从网页接收传过来的提醒时间和日程内容啊。
public static void main(String[] args) {
Timer timer = new Timer();
Calendar calendar = Calendar.getInstance();
calendar.set(2014, 04, 9, 17, 16, 00);
Date date = calendar.getTime();
timer.schedule(new SendMail(), date);
}
执行这个main方法会再2014.05.09 17:16:00调用你的run方法
[/quote]
我理解是这样的,按您写的,run()方法应该写在SendMail类里,
在run()方法里应该调用发送邮件的方法sendMail()
我的问题就是我通过servlet接收网页请求的数据,在servle里调用sendMail方法,
怎么传给run()方法呢。
public static void main(String[] args) {
Timer timer = new Timer();
Calendar calendar = Calendar.getInstance();
calendar.set(2014, 04, 9, 17, 16, 00);
Date date = calendar.getTime();
timer.schedule(new SendMail(), date);
}
执行这个main方法会再2014.05.09 17:16:00调用你的run方法
[/quote]
这里是不可能用servlet去接受网页传过来的信息的,你一定要用servlet吗?
public static void main(String[] args) {
Timer timer = new Timer();
Calendar calendar = Calendar.getInstance();
calendar.set(2014, 04, 9, 17, 16, 00);
Date date = calendar.getTime();
timer.schedule(new SendMail(), date);
}
执行这个main方法会再2014.05.09 17:16:00调用你的run方法
[/quote]
run()方法里应该写些什么,我需要用servlet接收网页传回的内容作为邮件的内容。
public static void main(String[] args) {
Timer timer = new Timer();
Calendar calendar = Calendar.getInstance();
calendar.set(2014, 04, 9, 17, 16, 00);
Date date = calendar.getTime();
timer.schedule(new SendMail(), date);
}
执行这个main方法会再2014.05.09 17:16:00调用你的run方法
public class SendMail extends TimerTask{
public void sendMail(String info, String mailAddress,
String remindTime) {
String month = remindTime.substring(0, remindTime.indexOf("/"));
String day = remindTime.substring(remindTime.indexOf("/")+1, remindTime.lastIndexOf("/"));
String hour = remindTime.substring(remindTime.lastIndexOf(" ")+1, remindTime.indexOf(":"));
String minute = remindTime.substring(remindTime.indexOf(":")+1, remindTime.lastIndexOf(""));
int m = Integer.parseInt(month) - 1;
int d = Integer.parseInt(day);
int h = Integer.parseInt(hour);
int mi = Integer.parseInt(minute);
Date date = new Date();
date.setMonth(m);
date.setDate(d);
date.setHours(h);
date.setMinutes(mi);
Properties props = new Properties();
props.setProperty("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
Session session = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(session);
Transport transport = null;
System.out.println("从网页传回的时间格式:"+remindTime);
String taskdate = "";
try {
String msgBody = "您好:" + "\n\n" + taskdate + "\n\n您有日程安排:" + info;
msg.setSubject("日程 - 温馨提醒");
msg.setText(msgBody);
transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com", "",
"");
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(
mailAddress));
msg.setSentDate(date);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}
@Override
public void run() {
}
public static void start_publish_scheduling(){
Timer timer = new Timer();
Calendar calendar = Calendar.getInstance();
calendar.set(2014, 04, 9, 17, 16, 00);
Date date = calendar.getTime();
timer.schedule(new MyTask(), date);
}
static class MyTask extends java.util.TimerTask {
@Override
public void run() {
System.out.println("----execute------");
}
}