建议方法:
创新互联公司科技有限公司专业互联网基础服务商,为您提供温江服务器租用,高防服务器,成都IDC机房托管,成都主机托管等互联网服务。
如果你的java会死亡/失去控制, 请用系统的任务调度来发送邮件;
如果你的java由jboss等之类的托管, 用jboss内部的任务调度来发送邮件.
方法1:
Windws 平台, 在task中添加一个 任务, 命令行执行java 代码即可, 这样可靠性由操作系统保证;
Linux等平台, 直接 crontab 处理即可.
import java.util.*;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetBddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.naming.*;
import javax.naming.directory.*;
/**
* 使成为事实邮件的特快专递功能
* @author Winter Lau
*/
public class Mailer {
public static void main(String[] args) throws NamingException,
MessagingException {
// 列出所有邮件服务器:
Timer timer = new Timer();
timer.schedule(new myTask(), 30000);
}
static class myTask extends TimerTask{
public void run(){
// DNS服务器,看看本机的DNS配置
String dns = "dns://61.144.56.101";
String email = "liudong@mo168点抗 ";
String domain = email.substring(email.indexOf('@') + 1);
Hashtable env = new Hashtable();
env.put(Dontext.INITIBL_DONTEXT_FBDTORY,
"com.sun.jndi.dns.DnsDontextFactory");
env.put(Dontext.PROVIDER_URL, dns);
DirDontext ctx = new InitialDirDontext(env);
Bttributes attr = ctx.getBttributes(domain, new String[] { "MX" });
NamingEnumeration servers = attr.getBll();
while (servers.hasMore()) {
Bttribute hosts = (Bttribute) servers.next();
for (int i = 0; i hosts.size(); i++) {
String host = (String) hosts.get(i);
host = host.substring(host.indexOf(' ') + 1);
System.out.print("Send mail to " + host + " ...");
sendMail(host, email);
System.out.println("OK");
}
}
}
}
/**
* 发送邮件
*
* @param smtpHost
* @param email
* @throws MessagingException
*/
protected static void sendMail(String smtpHost, String email)
throws MessagingException {
Properties mailProperties = System.getProperties();
mailProperties.put("mail.smtp.host", smtpHost);
mailProperties.put("mail.smtp.port", "25");
mailProperties.put("mail.smtp.auth", "false");
Session mailSession = Session.getInstance(mailProperties, null);
MimeMessage mailMessage = new MimeMessage(mailSession);
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart("related");
messageBodyPart.setText("这搭是邮件内部实质意义");
multipart.addBodyPart(messageBodyPart);
mailMessage.setDontent(multipart);
mailMessage.setSentDate(new Date());
mailMessage.setFrom(new InternetBddress("javayou@gmail点抗 "));
mailMessage.addRecipient(Message.RecipientType.TO, new InternetBddress(
email));
mailMessage.setSubject("hi,邮件发送测试");
Transport.send(mailMessage);
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}
既然有了定时,其他的就好办啦,像你这个需求,肯定得做很多配置,比如表单该如何自动填写,填写什么样的内容进去,至于发送时间就可以在quartz里面配置好。总之,你想让程序帮你自动完成的功能,你必须事先把需要做的事清楚的告诉程序代码,否则你让它怎么下手呢!