2011-04-28 70 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

相关问题