11
11
import org .apache .commons .collections .CollectionUtils ;
12
12
import org .apache .commons .lang3 .StringUtils ;
13
13
14
+ /**
15
+ * 邮件发送处理类
16
+ *
17
+ * @author ida 2017/6/12
18
+ */
14
19
public class PromotionMail {
15
20
16
21
protected static String sendMailQuery = "" ;
22
+
17
23
private static Configuration config ;
18
24
19
25
private static final String NAME_KEY = "NAME" ;
20
26
private static final String EMAIL_KEY = "EMAIL" ;
21
27
28
+ /**
29
+ * 发送邮件公有方法
30
+ *
31
+ * @param file
32
+ * @param mailDebug
33
+ */
22
34
public void sendMails (File file , boolean mailDebug ) {
23
35
try {
24
36
config = new Configuration ();
@@ -28,6 +40,48 @@ public void sendMails(File file, boolean mailDebug) {
28
40
}
29
41
}
30
42
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
+ */
31
85
private static MailInfo setMailInfo () {
32
86
MailInfo mailInfo = new MailInfo ();
33
87
mailInfo .setSmtpHost (config .getProperty (ConfigurationKeys .SMTP_SERVER ));
@@ -36,15 +90,32 @@ private static MailInfo setMailInfo() {
36
90
return mailInfo ;
37
91
}
38
92
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 {
40
104
String name = (String ) userInfo .get (NAME_KEY );
41
105
mailInfo .setSubject ("您关注的产品降价了" );
42
106
mailInfo .setMessage ("尊敬的 " + name + ", 您关注的产品 " + product .getProductDesc () + " 降价了,欢迎购买!" );
43
107
return mailInfo ;
44
108
}
45
109
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 );
48
119
System .out .println ("产品ID = " + product .getProductId () + "\n " );
49
120
System .out .println ("产品描述 = " + product .getProductDesc () + "\n " );
50
121
sendMailQuery = "Select name from subscriptions " + "where product_id= '" + product .getProductId () + "' "
@@ -53,11 +124,18 @@ private static Product readFile(File file) throws IOException {
53
124
return product ;
54
125
}
55
126
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 ;
58
136
BufferedReader br = null ;
59
137
try {
60
- product = new Product ();
138
+ product = new ProductInfo ();
61
139
br = new BufferedReader (new FileReader (file ));
62
140
String temp = br .readLine ();
63
141
String [] data = temp .split (" " );
@@ -71,54 +149,32 @@ private static Product setProductInfo(File file) throws IOException {
71
149
return product ;
72
150
}
73
151
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 {
75
163
String toAddress = (String ) userInfo .get (EMAIL_KEY );
76
164
mailInfo .setToAddress (toAddress );
77
165
if (toAddress .length () > 0 )
78
166
return setMessage (userInfo , mailInfo , product );
79
167
return mailInfo ;
80
168
}
81
169
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
170
private static void sendMail (MailInfo mailInfo , Boolean debug ) {
114
171
MailUtil .sendEmail (mailInfo .getToAddress (), mailInfo .getFromAddress (), mailInfo .getSubject (),
115
172
mailInfo .getMessage (), mailInfo .getSmtpHost (), debug );
116
173
}
117
174
118
175
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");
122
178
boolean emailDebug = false ;
123
179
PromotionMail pe = new PromotionMail ();
124
180
pe .sendMails (file , emailDebug );
0 commit comments