2013-06-28 74 views
0

我想使用Java代碼發送電子郵件。我使用smtp.gmail.com發送郵件,它工作正常。現在我想在每個月的特定日期發送電子郵件,說每個月的第一天。我搜查了很多,但沒有爲我工作。在特定日期發送電子郵件

以下是我發送郵件的代碼。

public class sendMailUsingTimeInterval{ 
public static void main(String[] args) throws IOException{ 
    String[] to={"[email protected]"}; 
    String[] cc={"[email protected]"}; 
    String subject = "hello"; 
    String body = "Thanks , this is test.....!!"; 

    //This is for google 
      sendMail("[email protected]","password","smtp.gmail.com","465","true", 
    "true",true,"javax.net.ssl.SSLSocketFactory","false",to,cc, 
    subject,body); 
} 
public synchronized static boolean sendMail(String userName,String passWord,String host,String port,String starttls,String auth,boolean debug,String socketFactoryClass,String fallback,String[] to,String[] cc,String subject,String text) 
    { 
     Properties props = new Properties(); 
     //Properties props=System.getProperties(); 
     props.put("mail.smtp.user", userName); 
     props.put("mail.smtp.host", host); 
     if(!"".equals(port)) 
     props.put("mail.smtp.port", port); 
     if(!"".equals(starttls)) 
     props.put("mail.smtp.starttls.enable",starttls); 
     props.put("mail.smtp.auth", auth); 
     if(debug){ 
     props.put("mail.smtp.debug", "true"); 
     } 
     else 
     { 
     props.put("mail.smtp.debug", "false");   
     } 
     if(!"".equals(port)) 
     props.put("mail.smtp.socketFactory.port", port); 
     if(!"".equals(socketFactoryClass)) 
     props.put("mail.smtp.socketFactory.class",socketFactoryClass); 
     if(!"".equals(fallback)) 
     props.put("mail.smtp.socketFactory.fallback", fallback); 

     try 
     { 
     Session session = Session.getDefaultInstance(props, null); 
     session.setDebug(debug); 
     MimeMessage msg = new MimeMessage(session); 
      msg.setContent(text,"text/html"); 
     msg.setSubject(subject); 
     msg.setFrom(new InternetAddress("[email protected]")); 
     for(int i=0;i<to.length;i++) 
     { 
      msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i])); 
     } 
     for(int i=0;i<cc.length;i++) 
     { 
      msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i])); 
     } 
     msg.saveChanges(); 
     Transport transport = session.getTransport("smtp"); 
     transport.connect(host, userName, passWord); 
     transport.sendMessage(msg, msg.getAllRecipients()); 
     transport.close(); 
     return true; 
     } 
     catch (Exception mex) 
     { 
     mex.printStackTrace(); 
     return false; 
     } 
    } 

當這個類被調用時,代碼正在工作。如果有人能夠告訴我如何以每月1日自動發送消息的方式實現它,我將非常感激。

+1

也許你想通過你的操作系統做調度,而不是在代碼中。 –

+1

沒有必要在標題中添加主標籤。 –

+0

從您的問題中不清楚您嘗試解決此問題的方法。你能解釋一下你的研究發現並分享任何實施嘗試嗎? –

回答

1

您可以使用Quartz調度程序執行此操作。

0

這不是你可以直接通過郵件API完成的事情,你必須使用某種排程工具(Quartz,Spring @Scheduled或類似的)在適當的時間用適當的參數調用sendMail

1

您可以使用「註釋類型時間表」

@Target基於(值=法) @Retention(值= RUNTIME) 公共@interface附表 時間表自動創建一個日程安排超時定時器類似cron的時間表達。註釋的方法用作超時回調方法。

此批註的所有元素都是可選的。如果沒有指定任何一個持久計時器將回調存在的每天午夜在其中執行應用程序容器關聯的默認時區創建...

文檔 - http://docs.oracle.com/javaee/6/api/javax/ejb/Schedule.html

示例 - https://stackoverflow.com/a/7499769/1490962

https://stackoverflow.com/a/5357856/1490962

http://ci.apache.org/projects/openejb/examples-generated/schedule-methods/

相關問題