2011-04-28 96 views
3

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

回答

1

如果您使用EJB 3.0+容器,則可以輕鬆使用計時器服務。

您需要創建會話bean,並執行TimedObject接口或使用@Timeout註釋方法。您可以通過getTimerService()InitialContext中獲得TimerService的實例,然後使用其中一個createTimer()變體創建一個計時器。它可以採取的間隔,或Date對象到期時,指定...

-1
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

+0

這並沒有回答定期發送的電子郵件的主要問題 – dbrin 2012-11-10 06:31:41

相關問題