Send automatic mail on specific date through 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在特定日期发送自动邮件
基础教程推荐
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 降序排序:Java Map 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01