Skip to content

Commit 1b68f6c

Browse files
authored
Merge pull request onlyliuxin#422 from GUK0/master
提交作业,希望老师看看拆分的是否合理,谢谢。
2 parents edc5836 + eae3b01 commit 1b68f6c

File tree

6 files changed

+201
-0
lines changed

6 files changed

+201
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package week00;
2+
import java.util.HashMap;
3+
import java.util.Map;
4+
public class Configuration {
5+
static Map<String,String> configurations = new HashMap<>();
6+
static{
7+
configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com");
8+
configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com");
9+
configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com");
10+
}
11+
/**
12+
* 应该从配置文件读, 但是这里简化为直接从一个map 中去读
13+
* @param key
14+
* @return
15+
*/
16+
public String getProperty(String key) {
17+
return configurations.get(key);
18+
}
19+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package week00;
2+
public class ConfigurationKeys {
3+
public static final String SMTP_SERVER = "smtp.server";
4+
public static final String ALT_SMTP_SERVER = "alt.smtp.server";
5+
public static final String EMAIL_ADMIN = "email.admin";
6+
}

students/247565311/week00/DBUtil.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package week00;
2+
import java.util.ArrayList;
3+
import java.util.HashMap;
4+
import java.util.List;
5+
public class DBUtil {
6+
/**
7+
* 应该从数据库读, 但是简化为直接生成。
8+
* @param sql
9+
* @return
10+
*/
11+
public static List<HashMap<String,String>> query(String sql){
12+
List<HashMap<String,String>> userList = new ArrayList<HashMap<String,String>>();
13+
for (int i = 1; i <= 3; i++) {
14+
HashMap<String,String> userInfo = new HashMap<String,String>();
15+
userInfo.put("NAME", "User" + i);
16+
userInfo.put("EMAIL", "aa@bb.com");
17+
userList.add(userInfo);
18+
}
19+
return userList;
20+
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package week00;
2+
3+
public class MailUtil {
4+
public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost,
5+
boolean debug) {
6+
//¼Ù×°·¢ÁËÒ»·âÓʼþ
7+
StringBuilder buffer = new StringBuilder();
8+
buffer.append("From:").append(fromAddress).append("\n");
9+
buffer.append("To:").append(toAddress).append("\n");
10+
buffer.append("Subject:").append(subject).append("\n");
11+
buffer.append("Content:").append(message).append("\n");
12+
System.out.println(buffer.toString());
13+
}
14+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
P8756 iPhone8
2+
P3946 XiaoMi10
3+
P8904 Oppo_R15
4+
P4955 Vivo_X20
5+

0 commit comments

Comments
 (0)