Skip to content

Commit 2cb061f

Browse files
ximanximan
ximan
authored and
ximan
committed
reconsitution
1 parent da01a9b commit 2cb061f

File tree

9 files changed

+279
-0
lines changed

9 files changed

+279
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.coderising</groupId>
6+
<artifactId>ood-assignment</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>ood-assignment</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
19+
<dependency>
20+
<groupId>junit</groupId>
21+
<artifactId>junit</artifactId>
22+
<version>4.12</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.projectlombok</groupId>
26+
<artifactId>lombok</artifactId>
27+
<version>1.14.8</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.apache.commons</groupId>
31+
<artifactId>commons-lang3</artifactId>
32+
<version>3.1</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>commons-collections</groupId>
36+
<artifactId>commons-collections</artifactId>
37+
<version>3.2</version>
38+
</dependency>
39+
40+
</dependencies>
41+
<repositories>
42+
<repository>
43+
<id>aliyunmaven</id>
44+
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
45+
</repository>
46+
</repositories>
47+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.coderising.ood.srp;
2+
import java.util.HashMap;
3+
import java.util.Map;
4+
5+
public class Configuration {
6+
7+
static Map<String,String> configurations = new HashMap<>();
8+
static{
9+
configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com");
10+
configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com");
11+
configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com");
12+
}
13+
/**
14+
* 应该从配置文件读, 但是这里简化为直接从一个map 中去读
15+
* @param key
16+
* @return
17+
*/
18+
public String getProperty(String key) {
19+
return configurations.get(key);
20+
}
21+
22+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.coderising.ood.srp;
2+
3+
public class ConfigurationKeys {
4+
5+
public static final String SMTP_SERVER = "smtp.server";
6+
public static final String ALT_SMTP_SERVER = "alt.smtp.server";
7+
public static final String EMAIL_ADMIN = "email.admin";
8+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.coderising.ood.srp;
2+
import java.util.ArrayList;
3+
import java.util.HashMap;
4+
import java.util.List;
5+
6+
public class DBUtil {
7+
8+
/**
9+
* 应该从数据库读, 但是简化为直接生成。
10+
* @param sql
11+
* @return
12+
*/
13+
public static List query(String sql){
14+
15+
List userList = new ArrayList();
16+
for (int i = 1; i <= 3; i++) {
17+
HashMap userInfo = new HashMap();
18+
userInfo.put("NAME", "User" + i);
19+
userInfo.put("EMAIL", "aa@bb.com");
20+
userList.add(userInfo);
21+
}
22+
return userList;
23+
}
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.coderising.ood.srp;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class MailInfo {
7+
8+
private String smtpHost;
9+
10+
private String altSmtpHost;
11+
12+
private String fromAddress;
13+
14+
private String toAddress;
15+
16+
private String subject;
17+
18+
private String message;
19+
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.coderising.ood.srp;
2+
3+
public class MailUtil {
4+
5+
public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost,
6+
boolean debug) {
7+
// 假装发了一封邮件
8+
StringBuilder buffer = new StringBuilder();
9+
buffer.append("From:").append(fromAddress).append("\n");
10+
buffer.append("To:").append(toAddress).append("\n");
11+
buffer.append("Subject:").append(subject).append("\n");
12+
buffer.append("Content:").append(message).append("\n");
13+
System.out.println(buffer.toString());
14+
}
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.coderising.ood.srp;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class Product {
7+
8+
private String productId;
9+
10+
private String productDesc;
11+
12+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package com.coderising.ood.srp;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
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+
11+
import org.apache.commons.collections.CollectionUtils;
12+
import org.apache.commons.lang3.StringUtils;
13+
14+
public class PromotionMail {
15+
16+
protected static String sendMailQuery = "";
17+
private static Configuration config;
18+
19+
private static final String NAME_KEY = "NAME";
20+
private static final String EMAIL_KEY = "EMAIL";
21+
22+
public void sendMails(File file, boolean mailDebug) {
23+
try {
24+
config = new Configuration();
25+
sendEMails(file, mailDebug);
26+
} catch (IOException e) {
27+
System.out.println(e.getMessage());
28+
}
29+
}
30+
31+
private static MailInfo setMailInfo() {
32+
MailInfo mailInfo = new MailInfo();
33+
mailInfo.setSmtpHost(config.getProperty(ConfigurationKeys.SMTP_SERVER));
34+
mailInfo.setAltSmtpHost(config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER));
35+
mailInfo.setFromAddress(config.getProperty(ConfigurationKeys.EMAIL_ADMIN));
36+
return mailInfo;
37+
}
38+
39+
private static MailInfo setMessage(HashMap<?, ?> userInfo, MailInfo mailInfo, Product product) throws IOException {
40+
String name = (String) userInfo.get(NAME_KEY);
41+
mailInfo.setSubject("您关注的产品降价了");
42+
mailInfo.setMessage("尊敬的 " + name + ", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!");
43+
return mailInfo;
44+
}
45+
46+
private static Product readFile(File file) throws IOException {
47+
Product product = setProductInfo(file);
48+
System.out.println("产品ID = " + product.getProductId() + "\n");
49+
System.out.println("产品描述 = " + product.getProductDesc() + "\n");
50+
sendMailQuery = "Select name from subscriptions " + "where product_id= '" + product.getProductId() + "' "
51+
+ "and send_mail=1 ";
52+
System.out.println("loadQuery set");
53+
return product;
54+
}
55+
56+
private static Product setProductInfo(File file) throws IOException {
57+
Product product = null;
58+
BufferedReader br = null;
59+
try {
60+
product = new Product();
61+
br = new BufferedReader(new FileReader(file));
62+
String temp = br.readLine();
63+
String[] data = temp.split(" ");
64+
product.setProductId(data[0]);
65+
product.setProductDesc(data[1]);
66+
} catch (IOException e) {
67+
throw new IOException(e.getMessage());
68+
} finally {
69+
br.close();
70+
}
71+
return product;
72+
}
73+
74+
private static MailInfo configureEMail(HashMap<?, ?> userInfo, MailInfo mailInfo, Product product) throws IOException {
75+
String toAddress = (String) userInfo.get(EMAIL_KEY);
76+
mailInfo.setToAddress(toAddress);
77+
if (toAddress.length() > 0)
78+
return setMessage(userInfo, mailInfo, product);
79+
return mailInfo;
80+
}
81+
82+
private static void sendEMails(File file, boolean debug) throws IOException {
83+
84+
Product product = readFile(file);
85+
86+
MailInfo mailInfo = setMailInfo();
87+
88+
System.out.println("开始发送邮件");
89+
90+
List<?> mailingList = DBUtil.query(sendMailQuery);
91+
if (CollectionUtils.isNotEmpty(mailingList)) {
92+
Iterator<?> iter = mailingList.iterator();
93+
while (iter.hasNext()) {
94+
MailInfo newMail = configureEMail((HashMap<?, ?>) iter.next(), mailInfo, product);
95+
try {
96+
if (StringUtils.isNotBlank(newMail.getToAddress()))
97+
sendMail(mailInfo, debug);
98+
} catch (Exception e) {
99+
try {
100+
sendMail(mailInfo, debug);
101+
} catch (Exception e2) {
102+
System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage());
103+
}
104+
}
105+
}
106+
107+
} else {
108+
System.out.println("没有邮件发送");
109+
}
110+
111+
}
112+
113+
private static void sendMail(MailInfo mailInfo, Boolean debug) {
114+
MailUtil.sendEmail(mailInfo.getToAddress(), mailInfo.getFromAddress(), mailInfo.getSubject(),
115+
mailInfo.getMessage(), mailInfo.getSmtpHost(), debug);
116+
}
117+
118+
public static void main(String[] args) throws Exception {
119+
// File f = new
120+
// File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt");
121+
File file = new File("/Users/dianping/Desktop/product_promotion.txt");
122+
boolean emailDebug = false;
123+
PromotionMail pe = new PromotionMail();
124+
pe.sendMails(file, emailDebug);
125+
}
126+
127+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
P8756 iPhone8
2+
P3946 XiaoMi10
3+
P8904 Oppo_R15
4+
P4955 Vivo_X20

0 commit comments

Comments
 (0)