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

标签 java email smtp scheduled-tasks

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

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.\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

最佳答案

配置Quartz为之工作。使用 cron trigger指定执行事件

关于java - 通过java在特定日期发送自动邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5814590/

相关文章:

java - 如何找到父线程的名字?

vba - 通过从带有换行符 : VBA 的单元格值创建消息来在电子邮件中新建行

python - 将 SMTP AUTH 支持添加到 Python smtpd 库...无法覆盖该方法?

C++使用CSmtp库发送邮件

php - 在本地主机上的 laravel 8 中发送电子邮件时出现 "Connection could not be established with host smtp.gmail.com :stream_socket_client()"错误

java - 方法参数

java - 创建全屏 Activity 但保留通知栏

java - Java、Android、数据库应用程序的架构?该应用程序应该是什么样子

email - 如何在 salesforce.com 中配置有关新案例评论的电子邮件通知?

html - 如何使用 acure 样式在浏览器中安全地显示电子邮件?