欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java结合email实现自动推送

程序员文章站 2022-04-15 16:11:35
1、编写方法获取最新标题的最新的标题2、使用获取额标题进行模糊查询,查询出邮箱地址,标题名称3、使用email发送邮件 ......

1、获取表中最后一条数据

 1 public static string demo() throws sqlexception {
 2     string sql = "select * from baoxiu order by id desc limit 0,1;";
 3         preparedstatement ptmt = conn.preparestatement(sql);
 4         resultset rs = ptmt.executequery();
 5         string str=null;
 6     if(rs.next()) {
 7         str= rs.getstring("bt");
 8     }
 9     return str;    
10 }

2、使用模糊查询,获取符合条件的所有数据

 1 public static list<duibimodel> getbaox(string bt) throws sqlexception {
 2     string sql = "select bt,`user`.cardid,phone from baoxiu,`user` where baoxiu.cardid=`user`.cardid and bt like '%"+bt+"%'";
 3         preparedstatement ptmt = conn.preparestatement(sql);
 4         resultset rs = ptmt.executequery();    
 5     list<duibimodel> list = new arraylist<>() ;
 6     while(rs.next()) {
 7         duibimodel duibi=new duibimodel();
 8         duibi.setbt(rs.getstring("bt"));
 9         duibi.setcardid(rs.getstring("cardid"));
10         duibi.setphone(rs.getstring("phone"));
11         list.add(duibi);
12     }
13     return list;    
14 }

3、java 发送邮件

 1 import javax.mail.authenticator;
 2 import javax.mail.passwordauthentication;
 3  
 4 public class auth extends authenticator {
 5  
 6     private string username = "";
 7     private string password = "";
 8  
 9     public auth(string username, string password) {
10         this.username = username;
11         this.password = password;
12     }
13     public passwordauthentication getpasswordauthentication() {
14         return new passwordauthentication(username, password);
15     } 
16 }
 1 import java.util.properties;
 2 import javax.mail.message;
 3 import javax.mail.session;
 4 import javax.mail.transport;
 5 import javax.mail.internet.internetaddress;
 6 import javax.mail.internet.mimemessage;
 7  
 8 public class sendmail {
 9  
10     private properties props; //系统属性
11     private session mailsession; //邮件会话对象
12     private mimemessage mimemsg; //mime邮件对象
13  
14     public sendmail(string smtphost, string port, string mailusername, string mailpassword) {
15         auth au = new auth(mailusername, mailpassword);
16         //设置系统属性
17         props=java.lang.system.getproperties(); //获得系统属性对象
18         props.put("mail.smtp.host", smtphost); //设置smtp主机
19         props.put("mail.smtp.port", port); //设置服务端口号
20         props.put("mail.smtp.auth", "true"); //同时通过验证
21         //获得邮件会话对象 
22         mailsession = session.getinstance(props, au);
23     }
24   
25     public boolean sendingmimemail(string mailfrom, string mailto,
26             string mailcopyto, string mailbcopyto, string mailsubject,
27             string mailbody) {
28         try { 
29             //创建mime邮件对象
30             mimemsg=new mimemessage(mailsession);
31             //设置发信人
32             mimemsg.setfrom(new internetaddress(mailfrom));
33             //设置收信人
34             if(mailto!=null){
35                 mimemsg.setrecipients(message.recipienttype.to, internetaddress.parse(mailto));
36             } 
37             //设置抄送人
38             if(mailcopyto!=null){
39                 mimemsg.setrecipients(javax.mail.message.recipienttype.cc,internetaddress.parse(mailcopyto));
40             }
41             //设置暗送人
42             if(mailbcopyto!=null){
43                 mimemsg.setrecipients(javax.mail.message.recipienttype.bcc,internetaddress.parse(mailbcopyto));
44             }
45             //设置邮件主题 
46             mimemsg.setsubject(mailsubject,"utf-8");
47             //设置邮件内容,将邮件body部分转化为html格式
48             mimemsg.setcontent(mailbody,"text/html;charset=utf-8");
49             //发送邮件
50             transport.send(mimemsg);
51             return true;
52         } catch (exception e) {
53             e.printstacktrace();
54             return false;
55         }
56     }
57 }
 1 public static boolean email(string email,string str) {
 2     string smtphost="smtp.qq.com";
 3     string port="25";
 4     string mailusername="gkh35@foxmail.com"; //直接用我的邮件进行发送测试
 5     string mailpassword="wggddlvcrqfubhde"; //密码请勿修改
 6     sendmail sendmail=new     sendmail(smtphost,port,mailusername,mailpassword);    
 7     string mailfrom="gkh35@foxmail.com"; //发件人
 8     string mailto=email; //收件人
 9     string mailcopyto=null; //抄送人
10     string mailbcopyto=null; //暗送人
11     string mailsubject="发现类似商品"; //邮件主题
12     string mailbody=str; //邮件内容
13     //发送邮件
14     boolean issend=sendmail.sendingmimemail(mailfrom, mailto, mailcopyto, mailbcopyto, mailsubject, mailbody);
15     return issend;
16 }

4、main方法

 1 public static void main(string[] args) throws sqlexception {
 2     boolean flag = false;
 3     string str=demo();
 4     for(duibimodel duibimodel : getbaox(str)) {
 5         system.out.println("名称为:"+duibimodel.getbt()+","+"身份证号:"+duibimodel.getcardid()+","+"电话号码为:"+duibimodel.getphone());
 6         flag=email("gkh8299@gmail.com", duibimodel.getbt());
 7     }
 8     if(flag) {
 9         system.out.println("邮件发送成功");
10     }else {
11         system.out.println("邮件发送失败");
12     }
13 }