java结合email实现自动推送
1、编写方法获取最新标题的最新的标题
2、使用获取额标题进行模糊查询,查询出邮箱地址,标题名称
3、使用email发送邮件
1、获取表中最后一条数据
1publicstatic 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; 6if(rs.next()) { 7 str= rs.getString("bt"); 8 } 9return str; 10 }
2、使用模糊查询,获取符合条件的所有数据
1publicstatic 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<>() ; 6while(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 }13return list; 14 }
3、Java 发送邮件
1import javax.mail.Authenticator; 2import javax.mail.PasswordAuthentication; 34publicclass Auth extends Authenticator {
5
6private String username = "";
7private String password = "";
8
9public Auth(String username, String password) {
10this.username = username;
11this.password = password;
12 }
13public PasswordAuthentication getPasswordAuthentication() {
14returnnew PasswordAuthentication(username, password);
15 }
16 }
1import java.util.Properties; 2import javax.mail.Message; 3import javax.mail.Session; 4import javax.mail.Transport; 5import javax.mail.internet.InternetAddress; 6import javax.mail.internet.MimeMessage; 78publicclass SendMail {
9
10private Properties props; //系统属性
11private Session mailSession; //邮件会话对象
12private MimeMessage mimeMsg; //MIME邮件对象
13
14public 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
25publicboolean sendingMimeMail(String MailFrom, String MailTo,
26 String MailCopyTo, String MailBCopyTo, String MailSubject,
27 String MailBody) {
28try {
29//创建MIME邮件对象
30 mimeMsg=new MimeMessage(mailSession);
31//设置发信人
32 mimeMsg.setFrom(new InternetAddress(MailFrom));
33//设置收信人
34if(MailTo!=null){
35 mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(MailTo));
36 }
37//设置抄送人
38if(MailCopyTo!=null){
39 mimeMsg.setRecipients(javax.mail.Message.RecipientType.CC,InternetAddress.parse(MailCopyTo));
40 }
41//设置暗送人
42if(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);
51returntrue;
52 } catch (Exception e) {
53 e.printStackTrace();
54returnfalse;
55 }
56 }
57 }
1publicstaticboolean 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//发送邮件
14boolean isSend=sendMail.sendingMimeMail(MailFrom, MailTo, MailCopyTo, MailBCopyTo, MailSubject, MailBody);
15return isSend;
16 }
4、main方法
1publicstaticvoid main(String[] args) throws SQLException { 2boolean flag = false; 3 String str=demo(); 4for(DuibiModel duibiModel : getBaoX(str)) { 5 System.out.println("名称为:"+duibiModel.getBt()+","+"身份证号:"+duibiModel.getCardid()+","+"电话号码为:"+duibiModel.getPhone()); 6 flag=email("gkh8299@gmail.com", duibiModel.getBt()); 7 } 8if(flag) { 9 System.out.println("邮件发送成功");10 }else {11 System.out.println("邮件发送失败");12 }13 }
以上是 java结合email实现自动推送 的全部内容, 来源链接: utcz.com/z/532914.html