通过java在特定日期发送自动邮件

Send automatic mail on specific date through java(通过java在特定日期发送自动邮件)

本文介绍了通过java在特定日期发送自动邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Java 邮件 API 通过我的 Java 应用程序发送电子邮件.但我想在未来日期自动发送它,即每个月/年的任何特定日期.我已经使用我的 ISP 的 SMTP 服务器在提到的 id 上发送电子邮件.我在网上参考了以下可用示例.如何在此处设置任何特定日期.我尝试过 SimpleDateFormat 并将其设置在此处,但它仍会立即发送邮件,但将其发送日期设置为上述特定日期.有没有其他方法可以在提到的日期和时间发送自动电子邮件?

I am using Java mail API to send email through my java application. But I want to send it automatically on future date i.e. any specific date of every month/year. I have used my ISP's SMTP server to send email on mentioned id.I have referred the below available example on net. How to set any specific date here.I have tried SimpleDateFormat and set it here but it still sends mail immediately but set its sent date as mentioned specific date. Is there any other way to send automatic email on mentioned date and time?

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 = "abc@abc.com";
    String from = "abc@abc.com";
    // 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.
" +
                    "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

推荐答案

如果您使用的是 EJB 3.0+ 容器,则可以轻松使用计时器服务.

If you're using an EJB 3.0+ container, you could easily use the timer service.

您需要创建一个会话 bean,并实现 TimedObject 接口或使用 @Timeout 注释方法.您可以通过 getTimerService()InitialContext 获取 TimerService 的实例,然后使用 createTimer() 变体.它可能需要一个时间间隔,或者一个 Date 对象指定它何时过期......

You need to make a session bean, and either implement the TimedObject interface or annotate a method with @Timeout. You can get an instance of the TimerService from the InitialContext via getTimerService(), then create a timer with one of the createTimer() variants. It can take an interval, or a Date object specifying when it expires...

这篇关于通过java在特定日期发送自动邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:通过java在特定日期发送自动邮件

基础教程推荐