大家好,我是考100分的小小码 ,祝大家学习进步,加薪顺利呀。今天说一说java结合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 }
代码100分
2、使用模糊查询,获取符合条件的所有数据
代码100分 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 }
代码100分 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 }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/9265.html