通过java在特定日期发送自动邮件

我正在使用Java mail API通过我的java应用程序发送电子邮件。 但是我想在未来的日期自动发送它,即每个月/每年的任何具体日期。 我已经使用我的ISP的SMTP服务器发送上述id的电子邮件。我在网上引用了以下可用示例。 如何在这里设置任何具体的日期。我已经尝试过SimpleDateFormat并在此处设置它但它仍然立即发送邮件,但是按照提到的具体日期设置其发送日期。 有没有其他方法可以在上述日期和时间发送自动电子邮件?

import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; // Send a simple, single part, text/plain e-mail public class TestEmail { public static void main(String[] args) { // SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!! String to = "abc@abc.com"; String from = "abc@abc.com"; // SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!! String host = "smtp.yourisp.net"; // Create properties, get Session Properties props = new Properties(); // If using static Transport.send(), // need to specify which host to send it to props.put("mail.smtp.host", host); // To see what is going on behind the scene props.put("mail.debug", "true"); Session session = Session.getInstance(props); try { // Instantiatee a message Message msg = new MimeMessage(session); //Set message attributes msg.setFrom(new InternetAddress(from)); InternetAddress[] address = {new InternetAddress(to)}; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject("Test E-Mail through Java"); msg.setSentDate(new Date()); // Set message content msg.setText("This is a test of sending a " + "plain text e-mail through Java.\n" + "Here is line 2."); //Send the message Transport.send(msg); } catch (MessagingException mex) { // Prints all nested (chained) exceptions as well mex.printStackTrace(); } } }//End of class 

为它配置Quartz作业。 使用cron trigger指定执行事件

如果您使用的是EJB 3.0+容器,则可以轻松使用计时器服务。

您需要创建会话bean,并实现TimedObject接口或使用TimedObject注释方法。 您可以通过getTimerService()InitialContext获取TimerService的实例,然后使用createTimer()变体之一创建一个计时器。 它可能需要一个间隔,或一个Date对象指定何时到期…

 import java.security.Security; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class GoogleTest { private static final String SMTP_HOST_NAME = "smtp.gmail.com"; private static final String SMTP_PORT = "465"; private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; /*public static void main(String args[]) throws Exception { Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); new GoogleTest().sendSSLMessage(sendTo, emailSubjectTxt,emailMsgTxt, emailFromAddress); System.out.println("Sucessfully Sent mail to All Users"); }*/ public void sendSSLMessage(String recipients, String subject, String message, String from) throws MessagingException { boolean debug = true; Properties props = new Properties(); props.put("mail.smtp.host", SMTP_HOST_NAME); props.put("mail.smtp.auth", "true"); props.put("mail.debug", "true"); props.put("mail.smtp.port", SMTP_PORT); props.put("mail.smtp.socketFactory.port", SMTP_PORT); props.put("mail.smtp.socketFactory.class", SSL_FACTORY); props.put("mail.smtp.socketFactory.fallback", "false"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("GMAIL ID", "PASSWORD"); } }); session.setDebug(debug); Message msg = new MimeMessage(session); InternetAddress addressFrom = new InternetAddress(from); msg.setFrom(addressFrom); InternetAddress addressTo = new InternetAddress(); addressTo = new InternetAddress(recipients); msg.setRecipient(Message.RecipientType.TO, addressTo); // Setting the Subject and Content Type msg.setSubject(subject); msg.setContent(message, "text/plain"); Transport.send(msg); } 

使用两个主要的Jar文件activation.jar,mail.jar