|
| 1 | +package week00; |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileNotFoundException; |
| 5 | +import java.io.FileReader; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Iterator; |
| 9 | +import java.util.List; |
| 10 | +class ProductMessage{ |
| 11 | + public String productID; |
| 12 | + public String productDesc; |
| 13 | + public ProductMessage(String id,String desc){ |
| 14 | + productID = id; |
| 15 | + productDesc = desc; |
| 16 | + } |
| 17 | +} |
| 18 | +class ProductMessageIter{ |
| 19 | + BufferedReader reader = null; |
| 20 | + boolean loadFileSuccess = false,readFinished = false; |
| 21 | + public ProductMessageIter(File socFile){ |
| 22 | + try { |
| 23 | + reader = new BufferedReader(new FileReader(socFile)); |
| 24 | + } catch (FileNotFoundException e) { |
| 25 | + e.printStackTrace(); |
| 26 | + return; |
| 27 | + } |
| 28 | + loadFileSuccess = true; |
| 29 | + } |
| 30 | + public boolean hasNext(){ |
| 31 | + if(!loadFileSuccess || readFinished) return false; |
| 32 | + try { |
| 33 | + reader.mark(10); |
| 34 | + String lineMsg = reader.readLine(); |
| 35 | + reader.reset(); |
| 36 | + if(lineMsg.equals("")) { |
| 37 | + readFinished = true; |
| 38 | + reader.close(); |
| 39 | + return false; |
| 40 | + } |
| 41 | + } catch (IOException e) { |
| 42 | + e.printStackTrace(); |
| 43 | + } |
| 44 | + return true; |
| 45 | + } |
| 46 | + public ProductMessage next(){ |
| 47 | + String lineMsg=null; |
| 48 | + try { |
| 49 | + lineMsg = reader.readLine(); |
| 50 | + } catch (IOException e) { |
| 51 | + e.printStackTrace(); |
| 52 | + } |
| 53 | + if(lineMsg == null) return null; |
| 54 | + String [] msgs = lineMsg.split(" "); |
| 55 | + ProductMessage msg = new ProductMessage(msgs[0],msgs[1]); |
| 56 | + System.out.println("产品ID = " + msgs[0]); |
| 57 | + System.out.println("产品描述 = " + msgs[1]); |
| 58 | + return msg; |
| 59 | + } |
| 60 | +} |
| 61 | +class EMailSender{ |
| 62 | + String fromAddress = null; |
| 63 | + String smtpHost = null; |
| 64 | + String altSmtpHost = null; |
| 65 | + private static final String NAME_KEY = "NAME"; |
| 66 | + private static final String EMAIL_KEY = "EMAIL"; |
| 67 | + |
| 68 | + public EMailSender(){ |
| 69 | + Configuration config = new Configuration(); |
| 70 | + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); |
| 71 | + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); |
| 72 | + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); |
| 73 | + } |
| 74 | + // 生成邮件内容,成功生成会发送邮件 |
| 75 | + public void configureAndSendEMail(HashMap<String,String> userInfo,ProductMessage prdMsg,boolean debug) throws IOException |
| 76 | + { |
| 77 | + String toAddress = "",subject = "",message = ""; |
| 78 | + toAddress = (String) userInfo.get(EMAIL_KEY); |
| 79 | + if (toAddress.length() > 0) { |
| 80 | + String name = (String) userInfo.get(NAME_KEY); |
| 81 | + subject = "您关注的产品降价了"; |
| 82 | + message = "尊敬的 "+name+", 您关注的产品 " + prdMsg.productDesc + " 降价了,欢迎购买!" ; |
| 83 | + System.out.println("开始发送邮件"); |
| 84 | + sendEMail(toAddress,subject,message,debug); |
| 85 | + }else { |
| 86 | + System.out.println("没有邮件发送"); |
| 87 | + } |
| 88 | + } |
| 89 | + // 发送通知邮件 |
| 90 | + void sendEMail(String toAddress,String subject,String message,boolean debug) throws IOException |
| 91 | + { |
| 92 | + boolean sendSucceed = false; |
| 93 | + try |
| 94 | + { |
| 95 | + if (toAddress.length() > 0){ |
| 96 | + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); |
| 97 | + sendSucceed = true; |
| 98 | + } |
| 99 | + } |
| 100 | + catch (Exception e) {} |
| 101 | + if(!sendSucceed){ |
| 102 | + try { |
| 103 | + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); |
| 104 | + } catch (Exception e2) |
| 105 | + { |
| 106 | + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | +public class PromotionMail { |
| 112 | + public static void main(String[] args) throws Exception { |
| 113 | + File f = new File("J:\\gitstore\\coding2017\\students\\247565311\\week00\\product_promotion.txt"); |
| 114 | + boolean emailDebug = false; |
| 115 | + PromotionMail pe = new PromotionMail(f, emailDebug); |
| 116 | + } |
| 117 | + // 流程控制 |
| 118 | + public PromotionMail(File file, boolean mailDebug) throws Exception { |
| 119 | + ProductMessageIter iter = new ProductMessageIter(file);//读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 |
| 120 | + while(iter.hasNext()){ |
| 121 | + ProductMessage prdMsg = iter.next(); |
| 122 | + List<HashMap<String,String>> userInfos = loadMailingList(prdMsg); |
| 123 | + for(HashMap<String,String> user : userInfos){ |
| 124 | + new EMailSender().configureAndSendEMail(user,prdMsg,mailDebug); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + // 获取用户订阅列表 |
| 129 | + protected List<HashMap<String,String>> loadMailingList(ProductMessage prdMsg) throws Exception { |
| 130 | + String sendMailQuery = "Select name from subscriptions " |
| 131 | + + "where product_id= '" + prdMsg.productID +"' " |
| 132 | + + "and send_mail=1 "; |
| 133 | + System.out.println("加载用户列表中..."); |
| 134 | + return DBUtil.query(sendMailQuery); |
| 135 | + } |
| 136 | +} |
0 commit comments