Skip to content

Commit bcd38c5

Browse files
committed
第一次提交
1 parent da01a9b commit bcd38c5

File tree

7 files changed

+215
-0
lines changed

7 files changed

+215
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
20+
return configurations.get(key);
21+
}
22+
23+
24+
public static void init(PromotionMail promotion){
25+
Configuration config = new Configuration();
26+
promotion.smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER);
27+
promotion.altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER);
28+
promotion.fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN);
29+
}
30+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
23+
return userList;
24+
}
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
8+
public class FileUtil {
9+
10+
public static void readFile(File file,PromotionMail promotion) throws IOException // @02C
11+
{
12+
BufferedReader br = null;
13+
try {
14+
br = new BufferedReader(new FileReader(file));
15+
String temp = br.readLine();
16+
String[] data = temp.split(" ");
17+
18+
promotion.productID = data[0];
19+
promotion.productDesc = data[1];
20+
21+
System.out.println("产品ID = " + promotion.productID + "\n");
22+
System.out.println("产品描述 = " + promotion.productDesc + "\n");
23+
24+
} catch (IOException e) {
25+
throw new IOException(e.getMessage());
26+
} finally {
27+
br.close();
28+
}
29+
}
30+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.coderising.ood.srp;
2+
3+
import java.io.IOException;
4+
import java.util.HashMap;
5+
import java.util.Iterator;
6+
import java.util.List;
7+
8+
public class MailUtil {
9+
10+
public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost,
11+
boolean debug) {
12+
// 假装发了一封邮件
13+
StringBuilder buffer = new StringBuilder();
14+
buffer.append("From:").append(fromAddress).append("\n");
15+
buffer.append("To:").append(toAddress).append("\n");
16+
buffer.append("Subject:").append(subject).append("\n");
17+
buffer.append("Content:").append(message).append("\n");
18+
System.out.println(buffer.toString());
19+
20+
}
21+
22+
public static void sendEMails(boolean debug, List mailingList, PromotionMail promotion) throws IOException {
23+
System.out.println("开始发送邮件");
24+
if (mailingList != null) {
25+
Iterator iter = mailingList.iterator();
26+
while (iter.hasNext()) {
27+
promotion.configureEMail((HashMap) iter.next());
28+
try {
29+
if (promotion.toAddress.length() > 0)
30+
MailUtil.sendEmail(promotion.toAddress, promotion.fromAddress, promotion.subject,
31+
promotion.message, promotion.smtpHost, debug);
32+
} catch (Exception e) {
33+
34+
try {
35+
MailUtil.sendEmail(promotion.toAddress, promotion.fromAddress, promotion.subject,
36+
promotion.message, promotion.altSmtpHost, debug);
37+
38+
} catch (Exception e2) {
39+
System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage());
40+
}
41+
}
42+
}
43+
44+
} else {
45+
System.out.println("没有邮件发送");
46+
47+
}
48+
49+
}
50+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.coderising.ood.srp;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
8+
public class PromotionMail {
9+
10+
protected String sendMailQuery = null;
11+
12+
protected String smtpHost = null;
13+
protected String altSmtpHost = null;
14+
protected String fromAddress = null;
15+
protected String toAddress = null;
16+
protected String subject = null;
17+
protected String message = null;
18+
19+
protected String productID = null;
20+
protected String productDesc = null;
21+
22+
private static final String NAME_KEY = "NAME";
23+
private static final String EMAIL_KEY = "EMAIL";
24+
25+
public static void main(String[] args) throws Exception {
26+
27+
File f = new File("G:\\Java\\github\\coding2017\\students\\404481481\\day01\\src\\product_promotion.txt");
28+
boolean emailDebug = false;
29+
30+
PromotionMail pe = new PromotionMail(f, emailDebug);
31+
32+
}
33+
34+
public PromotionMail(File file, boolean mailDebug) throws Exception {
35+
36+
// 读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8
37+
FileUtil.readFile(file, this);
38+
Configuration.init(this);
39+
setLoadQuery();
40+
41+
MailUtil.sendEMails(mailDebug, loadMailingList(), this);
42+
43+
}
44+
45+
protected void setLoadQuery() throws Exception {
46+
sendMailQuery = "Select name from subscriptions " + "where product_id= '" + productID + "' "
47+
+ "and send_mail=1 ";
48+
System.out.println("loadQuery set");
49+
}
50+
51+
protected void setMessage(HashMap userInfo) throws IOException {
52+
String name = (String) userInfo.get(NAME_KEY);
53+
subject = "您关注的产品降价了";
54+
message = "尊敬的 " + name + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!";
55+
}
56+
57+
protected void configureEMail(HashMap userInfo) throws IOException {
58+
toAddress = (String) userInfo.get(EMAIL_KEY);
59+
if (toAddress.length() > 0)
60+
setMessage(userInfo);
61+
}
62+
63+
protected List loadMailingList() throws Exception {
64+
return DBUtil.query(this.sendMailQuery);
65+
}
66+
67+
}
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)