通过java发送特定日期的自动邮件 [英] Send automatic mail on specific date through java
本文介绍了通过java发送特定日期的自动邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import java.util。*;
import javax.mail。*;
import javax.mail.internet。*;
import javax.activation。*;
//发送一个简单的单个部分,文本/纯电子邮件
public class TestEmail {
public static void main(String [] args){
//取代您的电子邮件地址!
String to =abc@abc.com;
String from =abc@abc.com;
//取代您的ISP的邮件服务器!
String host =smtp.yourisp.net;
//创建属性,获取会话
属性props = new Properties();
//如果使用静态Transport.send(),
//需要指定哪个主机发送到
props.put(mail.smtp.host主办);
//查看场景后面发生的事情
props.put(mail.debug,true);
会话session = Session.getInstance(props);
try {
//实例化消息
消息msg = new MimeMessage(session);
//设置消息属性
msg.setFrom(new InternetAddress(from));
InternetAddress [] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject(通过Java测试电子邮件);
msg.setSentDate(new Date());
//设置消息内容
msg.setText(这是通过Java.\\\
+ $发送+
纯文本电子邮件的测试b $ b这是第2行);
//发送消息
Transport.send(msg);
}
catch(MessagingException mex){
//打印所有嵌套(链接)异常以及
mex.printStackTrace();
}
}
} //类结束
解决方案
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.\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
解决方案
Configure Quartz
Job for it. Use cron trigger to specify the execution event
这篇关于通过java发送特定日期的自动邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文