diff --git a/students/799298900/src/com/leipengzj/Configuration.java b/students/799298900/src/com/leipengzj/Configuration.java new file mode 100644 index 0000000000..26665ad1a9 --- /dev/null +++ b/students/799298900/src/com/leipengzj/Configuration.java @@ -0,0 +1,31 @@ +package com.leipengzj; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} + +class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/799298900/src/com/leipengzj/DBUtil.java b/students/799298900/src/com/leipengzj/DBUtil.java new file mode 100644 index 0000000000..ec8ae4aba9 --- /dev/null +++ b/students/799298900/src/com/leipengzj/DBUtil.java @@ -0,0 +1,25 @@ +package com.leipengzj; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/799298900/src/com/leipengzj/MailInfo.java b/students/799298900/src/com/leipengzj/MailInfo.java new file mode 100644 index 0000000000..ecaa0dac9b --- /dev/null +++ b/students/799298900/src/com/leipengzj/MailInfo.java @@ -0,0 +1,92 @@ +package com.leipengzj; + +/** + * Created by pl on 2017/6/19. + */ +public class MailInfo { + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + public String getSendMailQuery() { + return sendMailQuery; + } + + public void setSendMailQuery(String sendMailQuery) { + this.sendMailQuery = sendMailQuery; + } + + public String getSmtpHost() { + return smtpHost; + } + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + + public String getAltSmtpHost() { + return altSmtpHost; + } + + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } +} diff --git a/students/799298900/src/com/leipengzj/MailUtil.java b/students/799298900/src/com/leipengzj/MailUtil.java new file mode 100644 index 0000000000..42d4329f96 --- /dev/null +++ b/students/799298900/src/com/leipengzj/MailUtil.java @@ -0,0 +1,18 @@ +package com.leipengzj; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/799298900/src/com/leipengzj/PromotionMail.java b/students/799298900/src/com/leipengzj/PromotionMail.java new file mode 100644 index 0000000000..5d152ac3cb --- /dev/null +++ b/students/799298900/src/com/leipengzj/PromotionMail.java @@ -0,0 +1,139 @@ +package com.leipengzj; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + +// protected String sendMailQuery = null; +// +// +// protected String smtpHost = null; +// protected String altSmtpHost = null; +// protected String fromAddress = null; +// protected String toAddress = null; +// protected String subject = null; +// protected String message = null; +// +// protected String productID = null; +// protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + //发送邮件 + public PromotionMail(File file, boolean mailDebug) throws Exception { + MailInfo mi = new MailInfo(); + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(mi,file); + + + config = new Configuration(); + + mi.setSmtpHost(config.getProperty(ConfigurationKeys.SMTP_SERVER)); + mi.setAltSmtpHost(config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); + mi.setFromAddress(config.getProperty(ConfigurationKeys.EMAIL_ADMIN)); + + String sql = "Select name from subscriptions " + + "where product_id= '" + mi.getProductID() +"' " + + "and send_mail=1 "; + //查询出邮件列表 + List query = DBUtil.query(sql); + + sendEMails(mi,mailDebug, query); + + + } + + + //读取产品信息 + protected void readFile(MailInfo mi,File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + mi.setProductID(data[0]); + mi.setProductDesc(data[1]); + + System.out.println("产品ID = " + data[0] + "\n"); + System.out.println("产品描述 = " + data[1] + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + //配置邮件并设置发送的邮件内容 + protected void configureEMail(HashMap userInfo,MailInfo mi) throws IOException + { + String toAddress = (String) userInfo.get(EMAIL_KEY); + String name = (String) userInfo.get(NAME_KEY); + if (toAddress.length() > 0){ + mi.setSubject("您关注的产品降价了"); + mi.setMessage("尊敬的 "+name+", 您关注的产品 " + mi.getProductDesc() + " 降价了,欢迎购买!"); + } + + } + + + protected void sendEMails(MailInfo mi,boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next(),mi); + try + { + if (mi.getToAddress().length() > 0) + MailUtil.sendEmail(mi.getToAddress(), mi.getToAddress(), mi.getSubject(), mi.getMessage(), mi.getSmtpHost(), debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(mi.getToAddress(), mi.getToAddress(), mi.getSubject(), mi.getMessage(), mi.getAltSmtpHost(), debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + } + else { + System.out.println("没有邮件发送"); + } + + } +} diff --git a/students/799298900/src/com/leipengzj/myfirstGitFork.java b/students/799298900/src/com/leipengzj/myfirstGitFork.java new file mode 100644 index 0000000000..a87099c226 --- /dev/null +++ b/students/799298900/src/com/leipengzj/myfirstGitFork.java @@ -0,0 +1,10 @@ +package com.leipengzj; + +/** + * Created by tyrion on 2017/6/15. + */ +public class myfirstGitFork { + public static void main(String[] args) { + System.out.println("myfirst git"); + } +} diff --git a/students/799298900/src/com/leipengzj/product_promotion.txt b/students/799298900/src/com/leipengzj/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/799298900/src/com/leipengzj/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file