0

我想使用 Java 代码发送电子邮件。我正在使用smtp.gmail.com发送邮件,它工作正常。现在我想在每个月的特定日期发送电子邮件,比如每个月的第一天。我搜索了很多,但没有为我工作。

下面是我发送邮件的代码。

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

     //This is for google
             sendMail("from@gmail.com","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("from@gmail.com"));
        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 日自动发送消息的方式实现它,我将不胜感激。

4

3 回答 3

1

您可以使用Quartz调度程序来执行此操作。

于 2013-06-28T16:09:11.833 回答
1

您可以使用“注释类型计划”

@Target(value=METHOD) @Retention(value=RUNTIME) public @interface Schedule 根据类似 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/

于 2013-06-28T16:17:38.097 回答
0

这不是您可以直接通过邮件 API 执行的操作,您必须使用某种调度工具(Quartz、Spring@Scheduled或类似工具)sendMail在适当的时间使用适当的参数进行调用。

于 2013-06-28T16:17:01.203 回答