我使用Java邮件API通过我的Java应用程序发送电子邮件。但是我想在将来的日期,即每月/每年的任何特定日期自动发送它。我用我的ISP的SMTP服务器发送上述id的电子邮件。我在网上提到了下面的可用示例。如何在这里设置任何特定的日期。我已经尝试过SimpleDateFormat并在此处设置它,但它仍然立即发送邮件,但将其发送日期设置为提及的特定日期。在上述日期和时间有没有其他方式发送自动电子邮件?通过java在特定日期发送自动邮件
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 = "[email protected]";
String from = "[email protected]";
// 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
这并没有回答定期发送的电子邮件的主要问题 – dbrin 2012-11-10 06:31:41