您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。