1、編寫方法獲取最新標題的最新的標題 2、使用獲取額標題進行模糊查詢,查詢出郵箱地址,標題名稱 3、使用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 }
2、使用模糊查詢,獲取符合條件的所有數據
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 }
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="[email protected]"; //直接用我的郵件進行發送測試 5 String MailPassword="wggddlvcrqfubhde"; //密碼請勿修改 6 SendMail sendMail=new SendMail(SMTPHost,Port,MailUsername,MailPassword); 7 String MailFrom="[email protected]"; //發件人 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("[email protected]", duibiModel.getBt()); 7 } 8 if(flag) { 9 System.out.println("郵件發送成功"); 10 }else { 11 System.out.println("郵件發送失敗"); 12 } 13 }