您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章将为大家详细讲解有关怎么在java中使用email实现自动推送功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
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="gkh45@foxmail.com"; //直接用我的邮件进行发送测试 String MailPassword="wggddlvcrqfubhde"; //密码请勿修改 SendMail sendMail=new SendMail(SMTPHost,Port,MailUsername,MailPassword); String MailFrom="gkh45@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实现自动推送功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。