mail spy

MailSpy.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package forest;

import java.io.IOException;
import java.security.Security;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import javax.activation.DataHandler;
import javax.mail.Authenticator;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.sun.net.ssl.internal.ssl.Provider;

public class MailSpy {
public static Logger log = Logger.getLogger("MailSpy");
private static ResourceBundle mailProperties = ResourceBundle.getBundle("forest.mail");

private static String username;
private static String password;
private static Properties props = new Properties();
private static SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

static {
try {
FileHandler fh = new FileHandler(mailProperties.getString("logFile"));
fh.setFormatter(new Formatter() {
@Override
public String format(LogRecord record) {
return String.format("%s [%s] - %s\n", sf.format(record.getMillis()), record.getLevel(), record
.getMessage());
}
});
log.addHandler(fh);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Security.addProvider(new Provider());
System.setProperty("javax.net.ssl.trustStore", mailProperties.getString("jssecacerts"));

for (String key : mailProperties.keySet()) {
props.setProperty(key, mailProperties.getString(key));
}

username = mailProperties.getString("username");
password = mailProperties.getString("password");
}

public static void main(String[] args) {
while (true) {
try {
MailSpy util = new MailSpy();
MailMessage mailMessage = util.getMail();
if (mailMessage != null) {
log.info("foward mail");
util.forwardMail(mailMessage);
}

} catch (Exception e) {
log.severe(e.getMessage());
}

try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

/**
* pharmgeneral@dh.gov.hk pass0424
*/
public void forwardMail(MailMessage mailMessage) throws AddressException, MessagingException {
Session session = Session.getDefaultInstance(props, new MyAuthenticator(username, password));
session.setDebug(false);
MimeMessage msg = new MimeMessage(session);

if (!mailMessage.getBodyPart().isEmpty()) {

MimeMultipart multipart = new MimeMultipart();
for (MimeBodyPart body : mailMessage.getBodyPart()) {
multipart.addBodyPart(body);
}
msg.setContent(multipart);
} else {
msg.setText(mailMessage.getBodyText());
}

//msg.setFrom(new InternetAddress(mailMessage.getFrom()));
msg.setFrom(new InternetAddress(username));

msg.setSender(new InternetAddress(mailMessage.getFrom()));
msg.setRecipients(Message.RecipientType.TO, mailProperties.getString("forwardTo"));
//msg.setRecipients(Message.RecipientType.TO, "PRS2.AP1@hp.com");

log.config("Sender " + mailMessage.getFrom() + ", Cc " + mailProperties.getString("forwardTo"));

msg.setSubject(mailMessage.getSubject());

msg.setSentDate(mailMessage.getSent());
Transport.send(msg);

log.info("subject: " + mailMessage.getSubject() + " is sent.");
}

public MailMessage getMail() {
Session session = Session.getDefaultInstance(props, new MyAuthenticator(username, password));
Store store = null;
MailMessage mailMessage = null;

try {
session.setDebug(false);
store = session.getStore();
store.connect();

Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);

int count = folder.getMessageCount();
if (count == 0) {
log.info("no message");
} else {
mailMessage = new MailMessage();
Message message = folder.getMessage(1);

Object content = message.getContent();

mailMessage.setFrom(InternetAddress.toString(message.getFrom()));

//mailMessage.setReplyTo(InternetAddress.toString(message.getReplyTo()));

//mailMessage.setTo(InternetAddress.toString(message.getRecipients(Message.RecipientType.TO)));

//mailMessage.setCc(InternetAddress.toString(message.getRecipients(Message.RecipientType.CC)));

mailMessage.setSubject(message.getSubject());

mailMessage.setSent(message.getSentDate());

if (content instanceof MimeMultipart) {
MimeMultipart mp = (MimeMultipart) content;
for (int i = 0, n = mp.getCount(); i < n; i++) {

Part part = mp.getBodyPart(i);
DataHandler hd = part.getDataHandler();

MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(hd.getDataSource()));
mailMessage.getBodyPart().add(messageBodyPart);

/*String disposition = part.getDisposition();
if (disposition == null) {
DataHandler hd = part.getDataHandler();

MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(hd.getDataSource()));
//messageBodyPart.setFileName(fileName);

mailMessage.getBodyPart().add(messageBodyPart);
}

if ((disposition != null)
&& (disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE)))) {
String fileName = part.getFileName();
log.info("file name: " + fileName);
InputStream in = part.getInputStream();

ByteArrayDataSource dataSource = new ByteArrayDataSource(in, part.getContentType());
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(dataSource));
messageBodyPart.setFileName(fileName);

mailMessage.getBodyPart().add(messageBodyPart);
}*/
}
} else {
log.info("content of text/plain " + content);
mailMessage.setBodyText(content.toString());
}

message.setFlag(Flags.Flag.DELETED, true);
}

folder.close(true);

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
log.fine("store closed");
store.close();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return mailMessage;
}
}

class MyAuthenticator extends Authenticator {
private String username;
private String password;

public MyAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}

protected PasswordAuthentication getPasswordAuthentication() {
MailSpy.log.config("username " + username + " , password " + password);
return new PasswordAuthentication(username, password);
}
}

class MailMessage {
private String from;
private String replyTo;
private String to;
private String cc;
private Date sent;
private String subject;
private String bodyText;
private List<MimeBodyPart> bodyPart = new ArrayList<MimeBodyPart>();

public String getFrom() {
return from;
}

public void setFrom(String from) {
this.from = from;
}

public String getReplyTo() {
return replyTo;
}

public void setReplyTo(String replyTo) {
this.replyTo = replyTo;
}

public String getTo() {
return to;
}

public void setTo(String to) {
this.to = to;
}

public Date getSent() {
return sent;
}

public void setSent(Date sent) {
this.sent = sent;
}

public String getSubject() {
return subject;
}

public void setSubject(String subject) {
this.subject = subject;
}

public String getBodyText() {
return bodyText;
}

public void setBodyText(String bodyText) {
this.bodyText = bodyText;
}

public List<MimeBodyPart> getBodyPart() {
return bodyPart;
}

public void setBodyPart(List<MimeBodyPart> bodyPart) {
this.bodyPart = bodyPart;
}

public String getCc() {
return cc;
}

public void setCc(String cc) {
this.cc = cc;
}

}

mail.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#SMTP
mail.smtp.host=smtpa.host
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.ssl.enable=true
mail.smtp.socketFactory.fallback=false
mail.smtp.port=465
mail.transport.protocol=smtps
mail.smtp.socketFactory.port=465
mail.smtp.auth=true

#POP3
mail.pop3s.host=pop3.host
mail.pop3.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.pop3.ssl.enable=true
mail.pop3.socketFactory.fallback=false
mail.pop3s.port=995
mail.store.protocol=pop3s
mail.pop3.socketFactory.port=995
mail.pop3s.auth=true

username=abc@abc.com
password=password
forwardTo=xyz@xyz.com

logFile=c:/mail.log
jssecacerts=jssecacerts