1、获取表中最后一条数据
public static String demo() throws SQLException { String sql = "select * FROM baoxiu ORDER BY id DESC LIMIT 0,1;"; PreparedStatement ptmt = conn.prepareStatement(sql); ResultSet rs = ptmt.executeQuery(); String str=null; if(rs.next()) { str= rs.getString("bt"); } return str; }
2、使用模糊查询,获取符合条件的所有数据
public static List<DuibiModel> getBaoX(String bt) throws SQLException { String sql = "select bt,`user`.cardid,phone FROM baoxiu,`user` WHERE baoxiu.cardid=`user`.cardid AND bt LIKE '%"+bt+"%'"; PreparedStatement ptmt = conn.prepareStatement(sql); ResultSet rs = ptmt.executeQuery(); List<DuibiModel> list = new ArrayList<>() ; while(rs.next()) { DuibiModel duibi=new DuibiModel(); duibi.setBt(rs.getString("bt")); duibi.setCardid(rs.getString("cardid")); duibi.setPhone(rs.getString("phone")); list.add(duibi); } return list; }
3、Java 发送邮件
import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; public class Auth extends Authenticator { private String username = ""; private String password = ""; public Auth(String username, String password) { this.username = username; this.password = password; } public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } } import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class SendMail { private Properties props; //系统属性 private Session mailSession; //邮件会话对象 private MimeMessage mimeMsg; //MIME邮件对象 public SendMail(String SMTPHost, String Port, String MailUsername, String MailPassword) { Auth au = new Auth(MailUsername, MailPassword); //设置系统属性 props=java.lang.System.getProperties(); //获得系统属性对象 props.put("mail.smtp.host", SMTPHost); //设置SMTP主机 props.put("mail.smtp.port", Port); //设置服务端口号 props.put("mail.smtp.auth", "true"); //同时通过验证 //获得邮件会话对象 mailSession = Session.getInstance(props, au); } public boolean sendingMimeMail(String MailFrom, String MailTo, String MailCopyTo, String MailBCopyTo, String MailSubject, String MailBody) { try { //创建MIME邮件对象 mimeMsg=new MimeMessage(mailSession); //设置发信人 mimeMsg.setFrom(new InternetAddress(MailFrom)); //设置收信人 if(MailTo!=null){ mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(MailTo)); } //设置抄送人 if(MailCopyTo!=null){ mimeMsg.setRecipients(javax.mail.Message.RecipientType.CC,InternetAddress.parse(MailCopyTo)); } //设置暗送人 if(MailBCopyTo!=null){ mimeMsg.setRecipients(javax.mail.Message.RecipientType.BCC,InternetAddress.parse(MailBCopyTo)); } //设置邮件主题 mimeMsg.setSubject(MailSubject,"utf-8"); //设置邮件内容,将邮件body部分转化为HTML格式 mimeMsg.setContent(MailBody,"text/html;charset=utf-8"); //发送邮件 Transport.send(mimeMsg); return true; } catch (Exception e) { e.printStackTrace(); return false; } } } public static boolean email(String email,String str) { String SMTPHost="smtp.qq.com"; String Port="25"; String MailUsername="gkh35@foxmail.com"; //直接用我的邮件进行发送测试 String MailPassword="wggddlvcrqfubhde"; //密码请勿修改 SendMail sendMail=new SendMail(SMTPHost,Port,MailUsername,MailPassword); String MailFrom="gkh35@foxmail.com"; //发件人 String MailTo=email; //收件人 String MailCopyTo=null; //抄送人 String MailBCopyTo=null; //暗送人 String MailSubject="发现类似商品"; //邮件主题 String MailBody=str; //邮件内容 //发送邮件 boolean isSend=sendMail.sendingMimeMail(MailFrom, MailTo, MailCopyTo, MailBCopyTo, MailSubject, MailBody); return isSend; }
4、main方法
public static void main(String[] args) throws SQLException { boolean flag = false; String str=demo(); for(DuibiModel duibiModel : getBaoX(str)) { System.out.println("名称为:"+duibiModel.getBt()+","+"身份证号:"+duibiModel.getCardid()+","+"电话号码为:"+duibiModel.getPhone()); flag=email("gkh8299@gmail.com", duibiModel.getBt()); } if(flag) { System.out.println("邮件发送成功"); }else { System.out.println("邮件发送失败"); } }
总结
到此这篇关于java结合email实现自动推送功能的文章就介绍到这了,更多相关java email 自动推送内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!
- 本文固定链接: https://zxbcw.cn/post/183847/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)