From 4c068f59c4e4c0d69d82bec1b02e9404445a2267 Mon Sep 17 00:00:00 2001
From: jimmy <jimmy_kwong@qq.com>
Date: Tue, 20 Jun 2017 08:09:35 +0800
Subject: [PATCH 1/3] =?UTF-8?q?=E9=87=8D=E6=9E=84PromotionMail=E6=BB=A1?=
 =?UTF-8?q?=E8=B6=B3SRP?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 students/63072784/pom.xml                     | 12 ++++
 .../src/main/java/ood/srp/Configuration.java  | 29 +++++++++
 .../main/java/ood/srp/ConfigurationKeys.java  | 11 ++++
 .../src/main/java/ood/srp/DBUtil.java         | 23 +++++++
 .../63072784/src/main/java/ood/srp/Mail.java  | 50 +++++++++++++++
 .../src/main/java/ood/srp/MailAccount.java    | 42 ++++++++++++
 .../src/main/java/ood/srp/MailUtil.java       | 35 ++++++++++
 .../src/main/java/ood/srp/Product.java        | 54 ++++++++++++++++
 .../src/main/java/ood/srp/PromotionMail.java  | 64 +++++++++++++++++++
 .../src/main/java/ood/srp/UserInfo.java       | 41 ++++++++++++
 .../src/main/resources/product_promotion.txt  |  4 ++
 11 files changed, 365 insertions(+)
 create mode 100644 students/63072784/pom.xml
 create mode 100644 students/63072784/src/main/java/ood/srp/Configuration.java
 create mode 100644 students/63072784/src/main/java/ood/srp/ConfigurationKeys.java
 create mode 100644 students/63072784/src/main/java/ood/srp/DBUtil.java
 create mode 100644 students/63072784/src/main/java/ood/srp/Mail.java
 create mode 100644 students/63072784/src/main/java/ood/srp/MailAccount.java
 create mode 100644 students/63072784/src/main/java/ood/srp/MailUtil.java
 create mode 100644 students/63072784/src/main/java/ood/srp/Product.java
 create mode 100644 students/63072784/src/main/java/ood/srp/PromotionMail.java
 create mode 100644 students/63072784/src/main/java/ood/srp/UserInfo.java
 create mode 100644 students/63072784/src/main/resources/product_promotion.txt

diff --git a/students/63072784/pom.xml b/students/63072784/pom.xml
new file mode 100644
index 0000000000..42eddb8471
--- /dev/null
+++ b/students/63072784/pom.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>com.jimmykwong</groupId>
+    <artifactId>coding2017</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+
+</project>
\ No newline at end of file
diff --git a/students/63072784/src/main/java/ood/srp/Configuration.java b/students/63072784/src/main/java/ood/srp/Configuration.java
new file mode 100644
index 0000000000..eb315dd7e3
--- /dev/null
+++ b/students/63072784/src/main/java/ood/srp/Configuration.java
@@ -0,0 +1,29 @@
+package ood.srp;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class Configuration {
+
+    static Map<String, String> 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");
+        configurations.put(ConfigurationKeys.EMAIL_ADMIN_PASSWORD, "admin!");
+        configurations.put(ConfigurationKeys.EMAIL_PORT, "25");
+    }
+
+    /**
+     * 应该从配置文件读， 但是这里简化为直接从一个map 中去读
+     *
+     * @param key
+     * @return
+     */
+    public String getProperty(String key) {
+
+        return configurations.get(key);
+    }
+
+}
diff --git a/students/63072784/src/main/java/ood/srp/ConfigurationKeys.java b/students/63072784/src/main/java/ood/srp/ConfigurationKeys.java
new file mode 100644
index 0000000000..168ee4a304
--- /dev/null
+++ b/students/63072784/src/main/java/ood/srp/ConfigurationKeys.java
@@ -0,0 +1,11 @@
+package ood.srp;
+
+public 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";
+    public static final String EMAIL_PORT = "email.port";
+    public static final String EMAIL_ADMIN_PASSWORD = "email.admin.password";
+
+}
diff --git a/students/63072784/src/main/java/ood/srp/DBUtil.java b/students/63072784/src/main/java/ood/srp/DBUtil.java
new file mode 100644
index 0000000000..86c9ab9102
--- /dev/null
+++ b/students/63072784/src/main/java/ood/srp/DBUtil.java
@@ -0,0 +1,23 @@
+package ood.srp;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class DBUtil {
+
+    /**
+     * 应该从数据库读， 但是简化为直接生成。
+     *
+     * @param sql
+     * @return
+     */
+    public static List<UserInfo> query(String sql) {
+        System.out.printf("执行sql=" + sql);
+        List<UserInfo> userList = new ArrayList<>();
+        for (int i = 1; i <= 3; i++) {
+            UserInfo userInfo = new UserInfo("User" + i, "aa@bb.com");
+            userList.add(userInfo);
+        }
+        return userList;
+    }
+}
diff --git a/students/63072784/src/main/java/ood/srp/Mail.java b/students/63072784/src/main/java/ood/srp/Mail.java
new file mode 100644
index 0000000000..db08647d9a
--- /dev/null
+++ b/students/63072784/src/main/java/ood/srp/Mail.java
@@ -0,0 +1,50 @@
+package ood.srp;
+
+/**
+ * Created by jimmy on 6/20/2017.
+ */
+public class Mail {
+    private MailAccount mailAccount;
+    private String toAddress;
+    private String subject;
+    private String message;
+
+    public Mail(MailAccount mailAccount, String toAddress, String subject, String message) {
+        this.mailAccount = mailAccount;
+        this.toAddress = toAddress;
+        this.subject = subject;
+        this.message = message;
+    }
+
+    public MailAccount getMailAccount() {
+        return mailAccount;
+    }
+
+    public void setMailAccount(MailAccount mailAccount) {
+        this.mailAccount = mailAccount;
+    }
+
+    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;
+    }
+}
diff --git a/students/63072784/src/main/java/ood/srp/MailAccount.java b/students/63072784/src/main/java/ood/srp/MailAccount.java
new file mode 100644
index 0000000000..f0aaf3ee49
--- /dev/null
+++ b/students/63072784/src/main/java/ood/srp/MailAccount.java
@@ -0,0 +1,42 @@
+package ood.srp;
+
+/**
+ * Created by jimmy on 6/20/2017.
+ */
+public class MailAccount {
+    private String smtpHost;
+    private String altSmtpHost;
+    private int port;
+    private String account;
+    private String password;
+
+    public static MailAccount buildAccount(Configuration config) {
+        MailAccount mailAccount = new MailAccount();
+        mailAccount.smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER);
+        mailAccount.altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER);
+        mailAccount.port = Integer.parseInt(config.getProperty(ConfigurationKeys.EMAIL_PORT));
+        mailAccount.account = config.getProperty(ConfigurationKeys.EMAIL_ADMIN);
+        mailAccount.password = config.getProperty(ConfigurationKeys.EMAIL_ADMIN_PASSWORD);
+        return mailAccount;
+    }
+
+    public String getSmtpHost() {
+        return smtpHost;
+    }
+
+    public String getAltSmtpHost() {
+        return altSmtpHost;
+    }
+
+    public int getPort() {
+        return port;
+    }
+
+    public String getAccount() {
+        return account;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+}
diff --git a/students/63072784/src/main/java/ood/srp/MailUtil.java b/students/63072784/src/main/java/ood/srp/MailUtil.java
new file mode 100644
index 0000000000..0e4d135252
--- /dev/null
+++ b/students/63072784/src/main/java/ood/srp/MailUtil.java
@@ -0,0 +1,35 @@
+package ood.srp;
+
+public class MailUtil {
+
+    public static void sendEmail(Mail mail, boolean debug) {
+        String toAddress = mail.getToAddress();
+        String fromAddress = mail.getMailAccount().getAccount();
+        String subject = mail.getSubject();
+        String message = mail.getMessage();
+        String smtpHost = mail.getMailAccount().getSmtpHost();
+        String altSmtpHost = mail.getMailAccount().getAltSmtpHost();
+        try {
+            sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug);
+        } catch (Exception e) {
+            try {
+                MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug);
+
+            } catch (Exception e2) {
+                System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage());
+            }
+        }
+
+    }
+
+    private 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/63072784/src/main/java/ood/srp/Product.java b/students/63072784/src/main/java/ood/srp/Product.java
new file mode 100644
index 0000000000..3e6b36b671
--- /dev/null
+++ b/students/63072784/src/main/java/ood/srp/Product.java
@@ -0,0 +1,54 @@
+package ood.srp;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+
+/**
+ * Created by jimmy on 6/20/2017.
+ */
+public class Product {
+    private String productId;
+    private String productDesc;
+
+    public static Product getPromotionProduct(File file) throws Exception {
+        BufferedReader br = null;
+        try {
+            br = new BufferedReader(new FileReader(file));
+            String temp = br.readLine();
+            String[] data = temp.split(" ");
+
+            String productId = data[0];
+            String productDesc = data[1];
+
+            System.out.println("产品ID = " + productId + "\n");
+            System.out.println("产品描述 = " + productDesc + "\n");
+
+            Product product = new Product();
+            product.productId = productId;
+            product.productDesc = productDesc;
+            return product;
+        } catch (IOException e) {
+            throw new IOException(e.getMessage());
+        } finally {
+            br.close();
+        }
+    }
+
+    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/63072784/src/main/java/ood/srp/PromotionMail.java b/students/63072784/src/main/java/ood/srp/PromotionMail.java
new file mode 100644
index 0000000000..abc97065e9
--- /dev/null
+++ b/students/63072784/src/main/java/ood/srp/PromotionMail.java
@@ -0,0 +1,64 @@
+package ood.srp;
+
+import java.io.File;
+import java.util.List;
+
+public class PromotionMail {
+
+
+    private static final String SUBJECT = "您关注的商品降价了";
+
+    public static void main(String[] args) throws Exception {
+        //这里可以做成参数输入
+        File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt");
+        boolean debug = true;
+        PromotionMail pe = new PromotionMail();
+        pe.sendEMails(f, debug);
+    }
+
+
+    public PromotionMail() {
+    }
+
+    private String buildMessage(UserInfo userInfo, Product product) {
+        return String.format("尊敬的 %s, 您关注的产品 %s 降价了，欢迎购买!", userInfo.getName(), product.getProductDesc());
+    }
+
+    private Mail buildMail(MailAccount mailAccount, UserInfo userInfo, Product product) {
+        //可以更加详细的检查
+        if (mailAccount == null || userInfo == null || product == null) {
+            return null;
+        } else {
+            String message = buildMessage(userInfo, product);
+            return new Mail(mailAccount, userInfo.getEmailAddress(), SUBJECT, message);
+        }
+    }
+
+
+    private void sendEMails(File file, boolean debug) throws Exception {
+
+        //构建发送邮件账号
+        MailAccount mailAccount = MailAccount.buildAccount(new Configuration());
+
+        //读取降价列表
+        Product product = Product.getPromotionProduct(file);
+
+        //获取订阅用户列表
+        List<UserInfo> userInfoList = UserInfo.getUserInfo(product.getProductId());
+
+        System.out.println("开始发送邮件");
+
+        if (userInfoList != null) {
+            for (UserInfo userInfo : userInfoList) {
+                Mail mail = buildMail(mailAccount, userInfo, product);
+                if (mail == null) {
+                    continue;
+                }
+                MailUtil.sendEmail(mail, debug);
+            }
+        } else {
+            System.out.println("没有邮件发送");
+
+        }
+    }
+}
diff --git a/students/63072784/src/main/java/ood/srp/UserInfo.java b/students/63072784/src/main/java/ood/srp/UserInfo.java
new file mode 100644
index 0000000000..3387dcb1bc
--- /dev/null
+++ b/students/63072784/src/main/java/ood/srp/UserInfo.java
@@ -0,0 +1,41 @@
+package ood.srp;
+
+import java.util.List;
+
+/**
+ * Created by jimmy on 6/20/2017.
+ */
+public class UserInfo {
+    private String name;
+    private String emailAddress;
+
+    public UserInfo(String name, String emailAddress) {
+        this.name = name;
+        this.emailAddress = emailAddress;
+    }
+
+    private static final String QUERY_PRODUCT = "Select name from subscriptions "
+            + "where product_id= '%s' "
+            + "and send_mail=1 ";
+
+    public static List<UserInfo> getUserInfo(String productId) {
+        System.out.println("loadQuery set");
+        return DBUtil.query(String.format(QUERY_PRODUCT, productId));
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getEmailAddress() {
+        return emailAddress;
+    }
+
+    public void setEmailAddress(String emailAddress) {
+        this.emailAddress = emailAddress;
+    }
+}
diff --git a/students/63072784/src/main/resources/product_promotion.txt b/students/63072784/src/main/resources/product_promotion.txt
new file mode 100644
index 0000000000..b7a974adb3
--- /dev/null
+++ b/students/63072784/src/main/resources/product_promotion.txt
@@ -0,0 +1,4 @@
+P8756 iPhone8
+P3946 XiaoMi10
+P8904 Oppo_R15
+P4955 Vivo_X20
\ No newline at end of file

From 473e0e8a9d2e874a706eeb300cd08377370d5293 Mon Sep 17 00:00:00 2001
From: jimmy <jimmy_kwong@qq.com>
Date: Tue, 20 Jun 2017 08:19:11 +0800
Subject: [PATCH 2/3] =?UTF-8?q?=E9=87=8D=E6=9E=84PromotionMail=E6=BB=A1?=
 =?UTF-8?q?=E8=B6=B3SRP?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 students/63072784/pom.xml                                  | 4 ++++
 students/63072784/src/main/java/ood/srp/PromotionMail.java | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/students/63072784/pom.xml b/students/63072784/pom.xml
index 42eddb8471..4c78ba51ac 100644
--- a/students/63072784/pom.xml
+++ b/students/63072784/pom.xml
@@ -8,5 +8,9 @@
     <artifactId>coding2017</artifactId>
     <version>1.0-SNAPSHOT</version>
 
+    <properties>
+        <maven.compiler.source>1.8</maven.compiler.source>
+        <maven.compiler.target>1.8</maven.compiler.target>
+    </properties>
 
 </project>
\ No newline at end of file
diff --git a/students/63072784/src/main/java/ood/srp/PromotionMail.java b/students/63072784/src/main/java/ood/srp/PromotionMail.java
index abc97065e9..a41c7fe446 100644
--- a/students/63072784/src/main/java/ood/srp/PromotionMail.java
+++ b/students/63072784/src/main/java/ood/srp/PromotionMail.java
@@ -10,7 +10,7 @@ public class PromotionMail {
 
     public static void main(String[] args) throws Exception {
         //这里可以做成参数输入
-        File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt");
+        File f = new File("product_promotion.txt");
         boolean debug = true;
         PromotionMail pe = new PromotionMail();
         pe.sendEMails(f, debug);

From bbafab3fbb001ec8ef76beda2c76630f58ed4cac Mon Sep 17 00:00:00 2001
From: jimmy <jimmy_kwong@qq.com>
Date: Tue, 20 Jun 2017 08:37:01 +0800
Subject: [PATCH 3/3] =?UTF-8?q?=E9=87=8D=E6=9E=84PromotionMail=E6=BB=A1?=
 =?UTF-8?q?=E8=B6=B3SRP?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 students/63072784/src/main/java/ood/srp/PromotionMail.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/students/63072784/src/main/java/ood/srp/PromotionMail.java b/students/63072784/src/main/java/ood/srp/PromotionMail.java
index a41c7fe446..f6646fac14 100644
--- a/students/63072784/src/main/java/ood/srp/PromotionMail.java
+++ b/students/63072784/src/main/java/ood/srp/PromotionMail.java
@@ -10,7 +10,7 @@ public class PromotionMail {
 
     public static void main(String[] args) throws Exception {
         //这里可以做成参数输入
-        File f = new File("product_promotion.txt");
+        File f = new File(PromotionMail.class.getClassLoader().getResource("product_promotion.txt").toURI());
         boolean debug = true;
         PromotionMail pe = new PromotionMail();
         pe.sendEMails(f, debug);
