我使用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