|
1
|
import java.io.*;
|
|
2
|
import java.util.Properties;
|
|
3
|
import javax.activation.DataHandler;
|
|
4
|
import javax.activation.DataSource;
|
|
5
|
import javax.activation.FileDataSource;
|
|
6
|
import javax.mail.*;
|
|
7
|
import javax.mail.internet.*;
|
|
8
|
import jxl.Cell;
|
|
9
|
import jxl.Sheet;
|
|
10
|
import jxl.Workbook;
|
|
11
|
import jxl.WorkbookSettings;
|
|
12
|
import jxl.read.biff.BiffException;
|
|
13
|
|
|
14
|
public class Mail {
|
|
15
|
|
|
16
|
/*
|
|
17
|
* Mail server connection variables
|
|
18
|
*/
|
|
19
|
private static final String SMTP_HOST_NAME = "my_smtp_hostname";
|
|
20
|
private static final String SMTP_AUTH_USER = "smtp_user";
|
|
21
|
private static final String SMTP_AUTH_PWD = "smtp_user_password";
|
|
22
|
|
|
23
|
/*
|
|
24
|
* Main
|
|
25
|
*/
|
|
26
|
public static void main(String[] args) throws Exception {
|
|
27
|
new Mail().test();
|
|
28
|
}
|
|
29
|
|
|
30
|
/*
|
|
31
|
* test() creation
|
|
32
|
*/
|
|
33
|
public void test() throws Exception {
|
|
34
|
|
|
35
|
Properties props = new Properties();
|
|
36
|
props.put("mail.transport.protocol", "smtp");
|
|
37
|
props.put("mail.smtp.host", SMTP_HOST_NAME);
|
|
38
|
props.put("mail.smtp.auth", "true");
|
|
39
|
|
|
40
|
|
|
41
|
|
|
42
|
|
|
43
|
|
|
44
|
|
|
45
|
|
|
46
|
/*
|
|
47
|
* Opening the excel sheet
|
|
48
|
*/
|
|
49
|
|
|
50
|
Workbook wb = null;
|
|
51
|
Cell wbCell[][] = new Cell[15][6965];
|
|
52
|
String wbString[][] = new String[15][6965];
|
|
53
|
|
|
54
|
|
|
55
|
/*
|
|
56
|
* Initializing reading loop variables
|
|
57
|
*/
|
|
58
|
|
|
59
|
int i, j, k;
|
|
60
|
i = 1;
|
|
61
|
j = 1;
|
|
62
|
k = 1;
|
|
63
|
|
|
64
|
/*
|
|
65
|
* Initializing supervising loop variables
|
|
66
|
*/
|
|
67
|
int total, success, failure;
|
|
68
|
total = 0;
|
|
69
|
success = 0;
|
|
70
|
failure = 0;
|
|
71
|
|
|
72
|
|
|
73
|
/*
|
|
74
|
* Starting loop
|
|
75
|
*/
|
|
76
|
|
|
77
|
try {
|
|
78
|
/*
|
|
79
|
* Reading Excel file
|
|
80
|
*/
|
|
81
|
WorkbookSettings ws = new WorkbookSettings();
|
|
82
|
ws.setEncoding("ISO-8859-1");
|
|
83
|
wb = Workbook.getWorkbook(new File("path/to/file.xls"), ws);
|
|
84
|
|
|
85
|
/*
|
|
86
|
* Accessing the sheet I want
|
|
87
|
*/
|
|
88
|
|
|
89
|
Sheet wbSheet = wb.getSheet(0);
|
|
90
|
|
|
91
|
/*
|
|
92
|
* Creating loop
|
|
93
|
*/
|
|
94
|
|
|
95
|
/*
|
|
96
|
* for (j = 1; j <= 5300; j++)
|
|
97
|
* j = 1 if you have column titles, 0 if not until last line number
|
|
98
|
*/
|
|
99
|
|
|
100
|
for (j = 1; j <= 5300; j++) {
|
|
101
|
|
|
102
|
/*
|
|
103
|
* I save my data in a table call wbString
|
|
104
|
*/
|
|
105
|
|
|
106
|
for (i = 0; i <= 14; i++) {
|
|
107
|
wbCell[i][j] = wbSheet.getCell(i, j);
|
|
108
|
wbString[i][j] = wbCell[i][j].getContents();
|
|
109
|
}
|
|
110
|
|
|
111
|
|
|
112
|
total = total + 1;
|
|
113
|
for (k = 1; k <= 16; k++) {
|
|
114
|
CHEMINCell[k][j] = CHEMINSheet.getCell(k, j);
|
|
115
|
CHEMINString[k][j] = CHEMINCell[k][j].getContents();
|
|
116
|
}
|
|
117
|
|
|
118
|
try {
|
|
119
|
/*
|
|
120
|
* Creating mail associated to the line I just read
|
|
121
|
*/
|
|
122
|
|
|
123
|
|
|
124
|
/*
|
|
125
|
* Create the body of the mail with the Cell you read
|
|
126
|
* Lot is custom field
|
|
127
|
*/
|
|
128
|
|
|
129
|
String body = "description\n " + wbString[1][j] + "\nproject: " + wbString[2][j] + ... + "\nLot: " + wbString[3][j]";
|
|
130
|
boolean debug = true;
|
|
131
|
boolean result = false;
|
|
132
|
try {
|
|
133
|
Authenticator auth = new SMTPAuthenticator();
|
|
134
|
Session mailSession = Session.getDefaultInstance(props, auth);
|
|
135
|
// uncomment for debugging infos to stdout
|
|
136
|
// mailSession.setDebug(true);
|
|
137
|
Transport transport = mailSession.getTransport();
|
|
138
|
transport.connect();
|
|
139
|
|
|
140
|
|
|
141
|
MimeMessage message = new MimeMessage(mailSession);
|
|
142
|
|
|
143
|
message.setFrom(new InternetAddress("e-mail@domain.com"));
|
|
144
|
message.setSubject(wbString[0][j]);
|
|
145
|
message.addRecipient(Message.RecipientType.TO,
|
|
146
|
new InternetAddress("e-mail@domain.com"));
|
|
147
|
mailSession.setDebug(debug);
|
|
148
|
|
|
149
|
MimeBodyPart mbpText = new MimeBodyPart();
|
|
150
|
mbpText.setText(body);
|
|
151
|
|
|
152
|
MimeMultipart mp = new MimeMultipart();
|
|
153
|
mp.addBodyPart(mbpText);
|
|
154
|
|
|
155
|
|
|
156
|
/*
|
|
157
|
* Create first attachment
|
|
158
|
*/
|
|
159
|
if (new File(wbString[4][j]).exists() == true) {
|
|
160
|
MimeBodyPart mbpAttachment1 = new MimeBodyPart();
|
|
161
|
DataSource source1 = new FileDataSource(SIGMAString[13][j]);
|
|
162
|
mbpAttachment1.setDataHandler(new DataHandler(source1));
|
|
163
|
mbpAttachment1.setFileName(SIGMAString[13][j]);
|
|
164
|
mp.addBodyPart(mbpAttachment1);
|
|
165
|
}
|
|
166
|
|
|
167
|
/*
|
|
168
|
*Create second attachment
|
|
169
|
*/
|
|
170
|
if (new File(wbString[5][j]).exists() == true) {
|
|
171
|
MimeBodyPart mbpAttachment2 = new MimeBodyPart();
|
|
172
|
DataSource source2 = new FileDataSource(CHEMINString[1][j]);
|
|
173
|
mbpAttachment2.setDataHandler(new DataHandler(source2));
|
|
174
|
mbpAttachment2.setFileName(CHEMINString[1][j]);
|
|
175
|
mp.addBodyPart(mbpAttachment2);
|
|
176
|
}
|
|
177
|
|
|
178
|
...
|
|
179
|
|
|
180
|
|
|
181
|
message.setContent(mp);
|
|
182
|
|
|
183
|
|
|
184
|
transport.sendMessage(message,
|
|
185
|
message.getRecipients(Message.RecipientType.TO));
|
|
186
|
transport.close();
|
|
187
|
result = true;
|
|
188
|
} catch (AddressException e) {
|
|
189
|
e.printStackTrace();
|
|
190
|
} catch (MessagingException e) {
|
|
191
|
e.printStackTrace();
|
|
192
|
}
|
|
193
|
if (result == true) {
|
|
194
|
success = success + 1;
|
|
195
|
} else {
|
|
196
|
failure = failure + 1;
|
|
197
|
}
|
|
198
|
}
|
|
199
|
} catch (BiffException e) {
|
|
200
|
e.printStackTrace();
|
|
201
|
} finally {
|
|
202
|
if (wb != null) {
|
|
203
|
wb.close();
|
|
204
|
}
|
|
205
|
}
|
|
206
|
|
|
207
|
System.out.println(total + " mails with " + success + " success and " + failure + " failure");
|
|
208
|
}
|
|
209
|
|
|
210
|
private class SMTPAuthenticator extends javax.mail.Authenticator {
|
|
211
|
|
|
212
|
public PasswordAuthentication getPasswordAuthentication() {
|
|
213
|
String username = SMTP_AUTH_USER;
|
|
214
|
String password = SMTP_AUTH_PWD;
|
|
215
|
return new PasswordAuthentication(username, password);
|
|
216
|
}
|
|
217
|
}
|
|
218
|
}
|