用定时器定时发送邮件

/*以下代码几乎功能是OK的,测试邮件发送时改掉代码内对应的邮箱名:wangchao 及密码:用"********"代替的
需要引入的包有:mail.jar activation.jar 在附件中上传
*/
package mail;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.naming.NamingException;
import javax.naming.InitialContext;
import javax.naming.Context;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.net.*;
public class SimpleMailSender {

public static void main(String[] args){
new SimpleMailSender().beepForAnHour();

}
public void test_main()throws Exception{

try{

Properties props = new Properties ();
/**在这mail.smtp.host 是邮件服务器的地址,比喻smtp.163.com 是163的接收邮件的服务器一般用smtp.163.com 就可以了但是,在我的AIX操作系统中不能解析这个地址,所以用220.181.12.16,
你也可以通过在dos底下用ping smtp.163.com 来得到一个IP地址
*/
props.put("mail.smtp.host", "smtp.163.com");

/**
mail.transport.protocol 是邮件传输协议中的接收协议;smtp既可;
*/
props.put("mail.transport.protocol","smtp");
/**
是否通过验证;一般为true 。false不能通过验证;
*/
props.put("mail.smtp.auth","true");
//mail.user 为用户邮箱登陆名;
props.put("mail.user", "wangchao");
//mail.password 邮箱登陆密码;
props.put("mail.password", "****");
Session sendMailSession;
//用户验证使用的实例;
MyAuth auth = new MyAuth("wangchao","******");
//根据用户验证和当前属性来创建一个会话对象;
sendMailSession = Session.getDefaultInstance(props,auth);
sendMailSession.setDebug(false);
Transport tran = sendMailSession.getTransport("smtp");
tran.connect();
System.out.println("测试连接是否成功 = "+tran.isConnected());//测试连接是否成功;
Message mess = new MimeMessage(sendMailSession);//通过会话
//发送邮件的邮件地址;
mess.setFrom(new InternetAddress("wangchao@163.com"));
//接收邮件的邮件地址;
// mess.setRecipient(Message.RecipientType.TO,new InternetAddress("wangrc@leagsoft.com"));
//接收邮件的邮件地址;
// mess.addRecipient(Message.RecipientType.TO,new InternetAddress("wangrc@leagsoft.com"));
//发送的内容;
mess.setText("现在开始测试发送文件是否成功:汪荣超");
//发送时间;
mess.setSentDate(new Date());
//内容主题;
mess.setSubject("测试发送文件是否成功");

mess.saveChanges();

Address[] ad = new Address[2];
//接收邮件的地址;
ad[0] = new InternetAddress("xxx@126.com");
ad[1] = new InternetAddress("hu_jing-sz@vanceinfo.com");
//发送邮件;
tran.sendMessage(mess,ad);

System.out.println("发送成功");



/*
//接收邮件;
Properties props = new Properties ();
//接收邮件邮件的服务器地址;mail.pop3.host ;220.181.12.113 同上面的一样;就不多说了;
props.put("mail.pop3.host", "mail.pop3.host");

props.put("mail.store.protocol","pop3");//接收邮件的协议mail.store.protocol = pop3

props.put("mail.password", "6860347");

props.put("mail.user", "wangchao19850623");//用户密码;

props.put("mail.port", "110"); //接收邮件服务器开的端口;163.com的端口是110;

Session sendMailSession;

sendMailSession = Session.getInstance(props,null);

Provider pro = sendMailSession.getProvider("pop3");
//默认的不能创建Store store = sendMailSession.getStore();创建不出来;为NULL异常;
Store store = sendMailSession.getStore(pro);
//先建立连接才能接收邮件;
store.connect("220.181.12.218",110,"wangchao19850623","6860347");
//是否连接成功;
System.out.println(store.isConnected());
//打开收件箱,得到一个目录;
Folder folder = store.getFolder("INBOX");
//打开目录;只读方式打开;
folder.open(Folder.READ_ONLY);
// 从收件箱中提出邮件;
Message[] mess = folder.getMessages();
//底下应该看的懂了;
System.out.println(mess.length);

System.out.println(mess[0].getSubject());

System.out.println(mess[0].getSentDate().toString());


}catch(NullPointerException e)

{

System.out.println(e.getMessage()+"异常");
*/



}catch(Exception ex)
{
System.out.println(ex.getMessage()+"异常");
}
}

public void beepForAnHour(){
TimerTask task = new TimerMask();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date date_str = null;
try {
date_str = sdf.parse("2010-1-20 09:23:00");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Date date = date_str;
Timer timer = new Timer();
timer.schedule(task, date,60000);
}
}


class MyAuth extends Authenticator
{
private String userName = "";
private String password = "";
private PasswordAuthentication pass = null;
public MyAuth(String userName,String password)
{
pass = new PasswordAuthentication(userName,password);
}
public PasswordAuthentication getPasswordAuthentication()
{
return pass;
}
}

class TimerMask extends TimerTask{
public void run(){
java.sql.Time sTime=new java.sql.Time(new Date().getTime());
System.out.println("测定Timer "+sTime);
try {
new SimpleMailSender().test_main();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值