建議方法:
創(chuàng)新互聯(lián)公司科技有限公司專業(yè)互聯(lián)網(wǎng)基礎(chǔ)服務(wù)商,為您提供溫江服務(wù)器租用,高防服務(wù)器,成都IDC機(jī)房托管,成都主機(jī)托管等互聯(lián)網(wǎng)服務(wù)。
如果你的java會(huì)死亡/失去控制, 請(qǐng)用系統(tǒng)的任務(wù)調(diào)度來(lái)發(fā)送郵件;
如果你的java由jboss等之類的托管, 用jboss內(nèi)部的任務(wù)調(diào)度來(lái)發(fā)送郵件.
方法1:
Windws 平臺(tái), 在task中添加一個(gè) 任務(wù), 命令行執(zhí)行java 代碼即可, 這樣可靠性由操作系統(tǒng)保證;
Linux等平臺(tái), 直接 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.*;
/**
* 使成為事實(shí)郵件的特快專遞功能
* @author Winter Lau
*/
public class Mailer {
public static void main(String[] args) throws NamingException,
MessagingException {
// 列出所有郵件服務(wù)器:
Timer timer = new Timer();
timer.schedule(new myTask(), 30000);
}
static class myTask extends TimerTask{
public void run(){
// DNS服務(wù)器,看看本機(jī)的DNS配置
String dns = "dns://61.144.56.101";
String email = "liudong@mo168點(diǎn)抗 ";
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");
}
}
}
}
/**
* 發(fā)送郵件
*
* @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("這搭是郵件內(nèi)部實(shí)質(zhì)意義");
multipart.addBodyPart(messageBodyPart);
mailMessage.setDontent(multipart);
mailMessage.setSentDate(new Date());
mailMessage.setFrom(new InternetBddress("javayou@gmail點(diǎn)抗 "));
mailMessage.addRecipient(Message.RecipientType.TO, new InternetBddress(
email));
mailMessage.setSubject("hi,郵件發(fā)送測(cè)試");
Transport.send(mailMessage);
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}
既然有了定時(shí),其他的就好辦啦,像你這個(gè)需求,肯定得做很多配置,比如表單該如何自動(dòng)填寫,填寫什么樣的內(nèi)容進(jìn)去,至于發(fā)送時(shí)間就可以在quartz里面配置好。總之,你想讓程序幫你自動(dòng)完成的功能,你必須事先把需要做的事清楚的告訴程序代碼,否則你讓它怎么下手呢!