我现在想实现两个功能:1、在页面中选择的日期是哪天就哪天发送邮件,例如选择2007年12月31日,那就在那天发送邮件
2、在页面中选择几天后发送就在这一天发送邮件,例如今天是10月28日选择三天后发送,那就到10月31日那天发送
请问该如何实现呢?是该用多线程吧?具体思路和做法是什么呢?请大家帮忙啊,高分相送!
以下是一个简单的javax发送邮件的小例子:实际项目中用的不是这个
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*; public class MailExample { public static void main (String args[]) throws Exception {
String host = "smtp.163.com"; //发件人使用发邮件的电子信箱服务器
String from = "[email protected]"; //发邮件的出发地(发件人的信箱)
String to = "[email protected]"; //发邮件的目的地(收件人信箱) // Get system properties
Properties props = System.getProperties(); // Setup mail server
props.put("mail.smtp.host", host); // Get session
props.put("mail.smtp.auth", "true"); //这样才能通过验证 MyAuthenticator myauth = new MyAuthenticator("[email protected]", "******"); //用户名和秘密
Session session = Session.getDefaultInstance(props, myauth); // session.setDebug(true); // Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from)); // Set the to address
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to)); // Set the subject
message.setSubject("测试程序!"); // Set the content
message.setText("这是用java写的发送电子邮件的测试程序!"); message.saveChanges(); Transport.send(message);
}
} //校验发信人权限的方法
class MyAuthenticator extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public MyAuthenticator(String user, String password) {
this.strUser = user;
this.strPwd = password;
} protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}
2、在页面中选择几天后发送就在这一天发送邮件,例如今天是10月28日选择三天后发送,那就到10月31日那天发送
请问该如何实现呢?是该用多线程吧?具体思路和做法是什么呢?请大家帮忙啊,高分相送!
以下是一个简单的javax发送邮件的小例子:实际项目中用的不是这个
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*; public class MailExample { public static void main (String args[]) throws Exception {
String host = "smtp.163.com"; //发件人使用发邮件的电子信箱服务器
String from = "[email protected]"; //发邮件的出发地(发件人的信箱)
String to = "[email protected]"; //发邮件的目的地(收件人信箱) // Get system properties
Properties props = System.getProperties(); // Setup mail server
props.put("mail.smtp.host", host); // Get session
props.put("mail.smtp.auth", "true"); //这样才能通过验证 MyAuthenticator myauth = new MyAuthenticator("[email protected]", "******"); //用户名和秘密
Session session = Session.getDefaultInstance(props, myauth); // session.setDebug(true); // Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from)); // Set the to address
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to)); // Set the subject
message.setSubject("测试程序!"); // Set the content
message.setText("这是用java写的发送电子邮件的测试程序!"); message.saveChanges(); Transport.send(message);
}
} //校验发信人权限的方法
class MyAuthenticator extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public MyAuthenticator(String user, String password) {
this.strUser = user;
this.strPwd = password;
} protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}
或者你写一个发邮件的程序做成.bat的文件,把他加到windows的计划任务里去也可以的
spring中也有定时功能,你可以参考以下
http://topic.csdn.net/u/20071029/10/131edb1b-38a6-4949-b11b-9c897e9ca3e3.html
的回答