我正在使用 Java 邮件 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指定执行事件
关于java - 通过java在特定日期发送自动邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5814590/