Skip to content

Commit ba6ceb3

Browse files
ximanximan
authored andcommitted
fix
1 parent 2cb061f commit ba6ceb3

File tree

4 files changed

+123
-57
lines changed

4 files changed

+123
-57
lines changed

students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/MailInfo.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@
22

33
import lombok.Data;
44

5+
/**
6+
* 邮件基本信息
7+
*
8+
* @author ida 2017/6/12
9+
*/
510
@Data
611
public class MailInfo {
712

813
private String smtpHost;
9-
14+
1015
private String altSmtpHost;
11-
16+
1217
private String fromAddress;
13-
18+
1419
private String toAddress;
1520

1621
private String subject;
17-
22+
1823
private String message;
1924

2025
}

students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.coderising.ood.srp;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* 产品信息
7+
*
8+
* @author ida 2017/6/12
9+
*/
10+
@Data
11+
public class ProductInfo {
12+
13+
/** 产品id */
14+
private String productId;
15+
/** 产品描述 */
16+
private String productDesc;
17+
}

students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java

Lines changed: 97 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,26 @@
1111
import org.apache.commons.collections.CollectionUtils;
1212
import org.apache.commons.lang3.StringUtils;
1313

14+
/**
15+
* 邮件发送处理类
16+
*
17+
* @author ida 2017/6/12
18+
*/
1419
public class PromotionMail {
1520

1621
protected static String sendMailQuery = "";
22+
1723
private static Configuration config;
1824

1925
private static final String NAME_KEY = "NAME";
2026
private static final String EMAIL_KEY = "EMAIL";
2127

28+
/**
29+
* 发送邮件公有方法
30+
*
31+
* @param file
32+
* @param mailDebug
33+
*/
2234
public void sendMails(File file, boolean mailDebug) {
2335
try {
2436
config = new Configuration();
@@ -28,6 +40,48 @@ public void sendMails(File file, boolean mailDebug) {
2840
}
2941
}
3042

43+
/**
44+
* 发送邮件
45+
*
46+
* @param file
47+
* @param debug
48+
* @throws IOException
49+
*/
50+
private static void sendEMails(File file, boolean debug) throws IOException {
51+
52+
ProductInfo product = readFile(file);
53+
54+
MailInfo mailInfo = setMailInfo();
55+
56+
System.out.println("开始发送邮件");
57+
58+
List<?> mailingList = DBUtil.query(sendMailQuery);
59+
if (CollectionUtils.isNotEmpty(mailingList)) {
60+
Iterator<?> iter = mailingList.iterator();
61+
while (iter.hasNext()) {
62+
MailInfo newMail = configureEMail((HashMap<?, ?>) iter.next(), mailInfo, product);
63+
try {
64+
if (StringUtils.isNotBlank(newMail.getToAddress()))
65+
sendMail(mailInfo, debug);
66+
} catch (Exception e) {
67+
try {
68+
sendMail(mailInfo, debug);
69+
} catch (Exception e2) {
70+
System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage());
71+
}
72+
}
73+
}
74+
} else {
75+
System.out.println("没有邮件发送");
76+
}
77+
78+
}
79+
80+
/**
81+
* 设置邮件部分信息
82+
*
83+
* @return
84+
*/
3185
private static MailInfo setMailInfo() {
3286
MailInfo mailInfo = new MailInfo();
3387
mailInfo.setSmtpHost(config.getProperty(ConfigurationKeys.SMTP_SERVER));
@@ -36,15 +90,32 @@ private static MailInfo setMailInfo() {
3690
return mailInfo;
3791
}
3892

39-
private static MailInfo setMessage(HashMap<?, ?> userInfo, MailInfo mailInfo, Product product) throws IOException {
93+
/**
94+
* 设置邮件message
95+
*
96+
* @param userInfo
97+
* @param mailInfo
98+
* @param product
99+
* @return
100+
* @throws IOException
101+
*/
102+
private static MailInfo setMessage(HashMap<?, ?> userInfo, MailInfo mailInfo, ProductInfo product)
103+
throws IOException {
40104
String name = (String) userInfo.get(NAME_KEY);
41105
mailInfo.setSubject("您关注的产品降价了");
42106
mailInfo.setMessage("尊敬的 " + name + ", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!");
43107
return mailInfo;
44108
}
45109

46-
private static Product readFile(File file) throws IOException {
47-
Product product = setProductInfo(file);
110+
/**
111+
* 读取文件
112+
*
113+
* @param file
114+
* @return
115+
* @throws IOException
116+
*/
117+
private static ProductInfo readFile(File file) throws IOException {
118+
ProductInfo product = setProductInfo(file);
48119
System.out.println("产品ID = " + product.getProductId() + "\n");
49120
System.out.println("产品描述 = " + product.getProductDesc() + "\n");
50121
sendMailQuery = "Select name from subscriptions " + "where product_id= '" + product.getProductId() + "' "
@@ -53,11 +124,18 @@ private static Product readFile(File file) throws IOException {
53124
return product;
54125
}
55126

56-
private static Product setProductInfo(File file) throws IOException {
57-
Product product = null;
127+
/**
128+
* 设置peoduct信息
129+
*
130+
* @param file
131+
* @return
132+
* @throws IOException
133+
*/
134+
private static ProductInfo setProductInfo(File file) throws IOException {
135+
ProductInfo product = null;
58136
BufferedReader br = null;
59137
try {
60-
product = new Product();
138+
product = new ProductInfo();
61139
br = new BufferedReader(new FileReader(file));
62140
String temp = br.readLine();
63141
String[] data = temp.split(" ");
@@ -71,54 +149,32 @@ private static Product setProductInfo(File file) throws IOException {
71149
return product;
72150
}
73151

74-
private static MailInfo configureEMail(HashMap<?, ?> userInfo, MailInfo mailInfo, Product product) throws IOException {
152+
/**
153+
* 读取邮件数据
154+
*
155+
* @param userInfo
156+
* @param mailInfo
157+
* @param product
158+
* @return
159+
* @throws IOException
160+
*/
161+
private static MailInfo configureEMail(HashMap<?, ?> userInfo, MailInfo mailInfo, ProductInfo product)
162+
throws IOException {
75163
String toAddress = (String) userInfo.get(EMAIL_KEY);
76164
mailInfo.setToAddress(toAddress);
77165
if (toAddress.length() > 0)
78166
return setMessage(userInfo, mailInfo, product);
79167
return mailInfo;
80168
}
81169

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-
113170
private static void sendMail(MailInfo mailInfo, Boolean debug) {
114171
MailUtil.sendEmail(mailInfo.getToAddress(), mailInfo.getFromAddress(), mailInfo.getSubject(),
115172
mailInfo.getMessage(), mailInfo.getSmtpHost(), debug);
116173
}
117174

118175
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");
176+
File file = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt");
177+
// File file = new File("/Users/myhome/Desktop/product_promotion.txt");
122178
boolean emailDebug = false;
123179
PromotionMail pe = new PromotionMail();
124180
pe.sendMails(file, emailDebug);

0 commit comments

Comments
 (0)