-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathSMTPSession.java
More file actions
799 lines (685 loc) · 20 KB
/
SMTPSession.java
File metadata and controls
799 lines (685 loc) · 20 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
package com.genexus.internet;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PushbackInputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.TimeZone;
import org.apache.commons.codec.binary.Base64OutputStream;
import org.apache.commons.lang.StringUtils;
import com.genexus.CommonUtil;
import com.genexus.common.interfaces.SpecificImplementation;
import com.genexus.platform.INativeFunctions;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.protocol.BasicHttpContext;
final class SMTPSession implements GXInternetConstants,ISMTPSession
{
private final int CONN_NORMAL = 0;
private final int CONN_TLS = 1;
private final int CONN_SSL = 2;
private boolean DEBUG = GXInternetConstants.DEBUG;
protected String host;
protected int port;
private PrintStream logOutput;
private String recipient;
private String senderAddress;
private String senderName;
private String message;
private String subject = "[No Subject]";
private String user = "";
private String password = "";
protected int timeout = 0;
private boolean authenticate = false;
private boolean secureConnection = false;
private String cc;
private String bcc;
private String attachments = "";
protected Socket sessionSock;
protected InputStream inStream;
protected DataOutputStream outStream;
private static final String CRLF = "\r\n";
private static String localHostName;
private BASE64Encoder base64encoder = new BASE64Encoder();
static
{
try
{
localHostName = java.net.InetAddress.getLocalHost().getHostName();
}
catch (Exception e)
{
localHostName = "unknown";
}
}
public SMTPSession()
{
if (DEBUG)
{
try
{
logOutput = new PrintStream(new FileOutputStream(new File("_gx_smtp.log")));
}
catch (IOException e)
{
System.out.println("Can't open SMTP log file smtp.log");
}
}
}
public SMTPSession(String host, String senderAddress, String message) throws GXMailException
{
this();
this.host = host;
this.port = 25; // default SMTP port is 25
this.message = message;
this.senderAddress = senderAddress;
this.senderName = senderAddress;
this.subject = "";
}
public void login(GXSMTPSession sessionInfo)
{
host = sessionInfo.getHost();
port = sessionInfo.getPort();
timeout = sessionInfo.getTimeout();
user = sessionInfo.getUserName();
password = sessionInfo.getPassword();
authenticate = sessionInfo.getAuthentication() != 0;
secureConnection = sessionInfo.getSecure() != 0;
try
{
connectAndLogin();
}
catch (GXMailException e)
{
sessionInfo.exceptionHandler(e);
}
}
public void send(GXSMTPSession sessionInfo, GXMailMessage msg)
{
try
{
if (msg.getTo().getCount() == 0 && msg.getCc().getCount() == 0 && msg.getBcc().getCount() == 0)
{
throw new GXMailException("No main recipient specified", MAIL_NoRecipient);
}
commandOk("MAIL FROM: <" + sessionInfo.getSender().getAddress() + ">", "2", MAIL_MessageNotSent);
// Now tell the server who we want to send a message to
log("Send TO");
sendToMailRecipientCollection(msg.getTo());
log("Send CC");
sendToMailRecipientCollection(msg.getCc());
log("Send BC");
sendToMailRecipientCollection(msg.getBcc());
// Okay, now send the mail message
commandOk("DATA", "3", MAIL_MessageNotSent);
try
{
println(FROM + ": " + sessionInfo.getSender().getRecipientString());
sendAllRecipients(msg.getTo(), GXInternetConstants.TO);
sendAllRecipients(msg.getCc(), GXInternetConstants.CC);
sendAllRecipients(msg.getReplyto(), GXInternetConstants.REPLY_TO);
println("MIME-Version: 1.0");
println(SUBJECT + ": " + GXMailer.getEncodedString(msg.getSubject()));
java.text.SimpleDateFormat df;
df = new java.text.SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss", Locale.ENGLISH);
TimeZone ts = TimeZone.getDefault();
String tsString;
int offset = ts.getRawOffset() / 3600000;
if (offset == 0)
tsString = "-0000";
else
{
int absOffset = java.lang.Math.abs(offset);
if (absOffset < 10)
if (absOffset == offset)
tsString = "+" + "0" + String.valueOf(absOffset);
else
tsString = "-" + "0" + String.valueOf(absOffset);
else
tsString = String.valueOf(offset);
tsString = tsString + "00";
}
String date = df.format((Date)new Date()) + " " + tsString;
println(DATE + ": " + date);
Enumeration<String> keys = msg.getHeaders().keys();
while(keys.hasMoreElements())
{
String key = (String)keys.nextElement();
println(key + " :" + msg.getHeaders().get(key));
}
// Comento estos headers porque aparentemente dan problemas al enviar
// mails a hotmail
//println("X-Mailer: GeneXus Application");
//println(PRIORITY + ": 3 (Normal)");
String sTime = Long.toString(System.currentTimeMillis());
if (msg.getAttachments().getCount() != 0)
{
println("Content-Type: multipart/mixed;boundary=\"" +
getStartMessageIdMixed(sTime) +
"\"\r\n\r\nThis message is in MIME format. Since your mail reader does not understand\r\nthis format, some or all of this message may not be legible.\r\n\r\n" +
getNextMessageIdMixed(sTime, false));
}
if (msg.getHtmltext().length() != 0)
{
println("Content-Type: multipart/alternative;boundary=\"" +
getStartMessageIdAlternative(sTime) +
"\"\r\n\r\nThis message is in MIME format. Since your mail reader does not understand\r\nthis format, some or all of this message may not be legible.\r\n\r\n");
}
if (msg.getText().length() != 0)
{
if (msg.getHtmltext().length() != 0)
{
println(getNextMessageIdAlternative(sTime, false));
}
println("Content-Type:text/plain; charset=\"UTF-8\"\r\n");
sendTextUTF8(msg.getText());
}
if (msg.getHtmltext().length() != 0)
{
println("\r\n\r\n" + getNextMessageIdAlternative(sTime, false));
println("Content-Type: text/html; charset=\"UTF-8\"\r\n");
sendTextUTF8(msg.getHtmltext());
println("");
println(getNextMessageIdAlternative(sTime, true));
}
sendAttachments(sTime, msg.getAttachments(), CommonUtil.addLastPathSeparator(sessionInfo.getAttachDir()));
}
catch (IOException e)
{
log ("6 - IOException " + e.getMessage());
throw new GXMailException(e.getMessage(), MAIL_ConnectionLost);
}
// A "." on a line by itself ends a message.
commandOk(CRLF + ".", "2", MAIL_MessageNotSent);
}
catch (GXMailException e)
{
sessionInfo.exceptionHandler(e);
}
}
private void sendAllRecipients(MailRecipientCollection msgList, String cmd) throws IOException {
if (msgList.getCount() > 0)
{
List<String> addresses = new ArrayList<String>();
MailRecipient recipient;
int addressFormat = 2;
for (int i=1; i <= msgList.getCount(); i++)
{
recipient = msgList.item(i);
addresses.add(recipient.getRecipientString(addressFormat));
}
println(cmd + ": " + StringUtils.join(addresses, ','));
}
}
private void sendTextUTF8(String text) throws IOException
{
// Send each line of the message
StringTokenizer st = new StringTokenizer(text, "\n", true);
String aux;
while (st.hasMoreTokens())
{
aux = st.nextToken();
// If the line begins with a ".", put an extra "." in front of it.
if (aux.charAt(0) == '.')
outStream.writeBytes(".");
outStream.write(aux.getBytes("UTF8"));
outStream.flush();
log(aux);
}
}
public void logout(GXSMTPSession sessionInfo)
{
try
{
logout();
}
catch (GXMailException e)
{
sessionInfo.exceptionHandler(e);
}
}
public void setSubject(String subject)
{
if (subject.trim().length() == 0)
this.subject = "[No Subject]";
else
this.subject = subject;
}
public void setTO(String recipient)
{
this.recipient = recipient;
}
public void setCC(String cc)
{
this.cc = cc;
}
public void setBCC(String bcc)
{
this.bcc = bcc;
}
public void setAttachments(String attachments)
{
this.attachments = attachments.trim();
}
boolean isLoggedIn()
{
return sessionSock != null;
}
private void close() throws IOException
{
// Close down the session
if (sessionSock != null)
{
sessionSock.close();
sessionSock = null;
}
}
private void sendToMailRecipientCollection(MailRecipientCollection recipients) throws GXMailException
{
try
{
String response;
for (int i = 0 ; i < recipients.getCount(); i++)
{
response = doCommand("RCPT TO: <" + recipients.item(i + 1).getAddress() + ">");
if (response.charAt(0) != '2')
{
log ("2 - Response invalid " + response);
throw new GXMailException(response, MAIL_InvalidRecipient);
}
}
}
catch (IOException e)
{
log ("3 - IOException " + e.getMessage());
throw new GXMailException(e.getMessage(), MAIL_ConnectionLost);
}
}
private void connectAndLogin() throws GXMailException
{
if(secureConnection)
{
try
{
connectIndirectTLS();
}
catch(GXMailException e1)
{
closeSafe();
try
{
connectDirectTLS();
}
catch(GXMailException e2)
{
closeSafe();
connectSSL();
}
}
}
else
{
connectNormal();
}
}
private void connectIndirectTLS() throws GXMailException
{
connect(CONN_TLS);
doLogin(true);
}
private void connectDirectTLS() throws GXMailException
{
connect(CONN_TLS);
doLogin(false);
}
private void connectSSL() throws GXMailException
{
connect(CONN_SSL);
doLogin(false);
}
private void connectNormal() throws GXMailException
{
connect(CONN_NORMAL);
doLogin(false);
}
private void connect(final int type) throws GXMailException
{
// Connect to the server
SpecificImplementation.NativeFunctions.getInstance().executeWithPermissions(
new Runnable() {
public void run()
{
try
{
sessionSock = getConnectionSocket(type);
log("Timeout: " + timeout);
sessionSock.setSoTimeout(timeout * 1000);
sessionSock.setTcpNoDelay(true);
inStream = sessionSock.getInputStream();
outStream = new DataOutputStream(sessionSock.getOutputStream());
}
catch (UnknownHostException e)
{
}
catch (IOException e)
{
}
}
}, INativeFunctions.CONNECT);
if (sessionSock == null)
{
log ("1 - Can't connect to host");
throw new GXMailException("Can't connect to host", MAIL_CantLogin);
}
}
private Socket getConnectionSocket(int type) throws UnknownHostException, IOException
{
InetAddress ipAddr = InetAddress.getByName(host.trim());
SSLConnectionSocketFactory sslConn;
switch(type)
{
case CONN_NORMAL:
return new Socket(host.trim(), port);
case CONN_TLS:
sslConn = SSLConnConstructor.getSSLSecureInstance(new String[] { "TLSv1.1", "TLSv1.2" });
return sslConn.createLayeredSocket(new Socket(host.trim(), port),ipAddr.getHostName(),port,new BasicHttpContext());
case CONN_SSL:
sslConn = SSLConnConstructor.getSSLSecureInstance(new String[] { "TLSv1" });
return sslConn.createLayeredSocket(new Socket(host.trim(), port),ipAddr.getHostName(),port,new BasicHttpContext());
}
return new Socket(host.trim(), port);
}
private void doLogin(boolean startTLS) throws GXMailException
{
// After connecting, the SMTP server will send a response string. Make
// sure it starts with a '2' (reponses in the 200's are positive
// responses.
try
{
String response = getResponse();
if (response.charAt(0) != '2') {
log ("4 - Invalid Response " + response);
throw new GXMailException(response, MAIL_CantLogin);
}
}
catch (IOException e)
{
log ("5 - IOException " + e.getMessage());
throw new GXMailException(e.getMessage(), MAIL_ConnectionLost);
}
if(!authenticate)
{
// Introduce ourselves to the SMTP server with a polite "HELO"
commandOk("HELO " + localHostName);
if(startTLS)
{
commandOk("STARTTLS");
}
}else
{
// @gusbro: Si quremos autenticarnos, debemos hacer un 'extended hello'
// y no un HELO simple...
commandOk("EHLO " + localHostName);
doAuthLogin(startTLS);
}
}
private void doAuthLogin(boolean startTLS) throws GXMailException
{
commandOk("AUTH LOGIN", "334", MAIL_AuthenticationError);
if(startTLS)
{
commandOk("STARTTLS");
}
commandOk(base64encoder.encodeBuffer(user.getBytes()), "334", MAIL_AuthenticationError);
commandOk(base64encoder.encodeBuffer(password.getBytes()), "235", MAIL_PasswordRefused);
}
private void closeSafe()
{
try
{
close();
}
catch(IOException ioex) {}
}
public void logout() throws GXMailException
{
commandOk("QUIT");
try
{
close();
}
catch (IOException e)
{
log ("7 - IOException " + e.getMessage());
throw new GXMailException(e.getMessage(), MAIL_ConnectionLost);
}
}
private void commandOk(String message) throws GXMailException
{
commandOk(message, "2");
}
private void commandOk(String message, String startsWith, int error) throws GXMailException
{
try
{
String response = doCommand(message);
if (!response.startsWith(startsWith))
{
log ("8 - Response Invalid " + response + " - " + startsWith);
doCommand("RSET");
throw new GXMailException(response, error);
}
}
catch (IOException e)
{
log ("9 - IOException " + e.getMessage());
throw new GXMailException(e.getMessage(), MAIL_ConnectionLost);
}
}
private void commandOk(String message, String startsWith) throws GXMailException
{
commandOk(message, startsWith, 0);
}
private void sendAttachments(String sTime, StringCollection attachments, String attachmentPath) throws GXMailException
{
try
{
if (attachments.getCount() == 0)
{
return;
}
println("");
for (int i = 0; i < attachments.getCount(); i++)
{
sendAttachment(sTime, attachments.item(i + 1), attachmentPath);
}
println(getNextMessageIdMixed(sTime, true));
}
catch (IOException e)
{
log ("10 - IOException " + e.getMessage());
throw new GXMailException(e.getMessage(), MAIL_ConnectionLost);
}
}
private String getStartMessageIdMixed(String sTime)
{
return getStartMessage(sTime, "MIXED");
}
private String getNextMessageIdMixed(String sTime, boolean end)
{
return getNextMessage(sTime, "MIXED", end);
}
private String getStartMessageIdAlternative(String sTime)
{
return getStartMessage(sTime, "ALTERNATIVE");
}
private String getNextMessageIdAlternative(String sTime, boolean end)
{
return getNextMessage(sTime, "ALTERNATIVE", end);
}
private String getStartMessage(String sTime, String sPrefix)
{
return "----_=_NextPart_" + sPrefix + "_" + sTime;
}
private String getNextMessage(String sTime, String sPrefix, boolean end)
{
return "--" + getStartMessage(sTime, sPrefix) + (end?"--":"");
}
private void sendAttachment(String sTime, String fileNamePath, String attachmentPath) throws GXMailException, IOException
{
InputStream is;
String fileName = fileNamePath;
if (fileNamePath.lastIndexOf(File.separator) != -1)
fileName = fileNamePath.substring(fileNamePath.lastIndexOf(File.separator) + 1);
try
{
is = new FileInputStream(attachmentPath + fileNamePath);
}
catch (FileNotFoundException e)
{
log ("11 - FileNotFound " + e.getMessage());
throw new GXMailException("Can't find " + attachmentPath + fileNamePath, MAIL_InvalidAttachment);
}
println(getNextMessageIdMixed(sTime, false));
println("Content-Type: " + "application/octet-stream");
println("Content-Transfer-Encoding: " + "base64");
println("Content-Disposition: " + "attachment; filename=\"" + GXMailer.getEncodedString(fileName) + "\"");
println("");
int BUFFER_SIZE = 4096;
byte[] buffer = new byte[BUFFER_SIZE];
OutputStream base64Output = new Base64OutputStream(outStream);
int n = is.read(buffer, 0, BUFFER_SIZE);
while (n >= 0) {
base64Output.write(buffer, 0, n);
n = is.read(buffer, 0, BUFFER_SIZE);
}
base64Output.flush();
outStream.writeBytes(CRLF);
outStream.flush();
}
private void println(String s) throws IOException
{
outStream.writeBytes(s + CRLF);
outStream.flush();
log(s);
}
private String doCommand(String commandString) throws IOException
{
// Send a command and wait for a response
log(commandString);
outStream.writeBytes(commandString + CRLF);
outStream.flush();
log("Write Ok");
String response = getResponse();
return response;
}
private char[] lineBuffer = new char[128];
private String readLine() throws IOException
{
InputStream in = this.inStream;
char buf[] = lineBuffer;
if (buf == null)
{
buf = lineBuffer = new char[128];
}
int room = buf.length;
int offset = 0;
int c;
loop:
while (true)
{
c = in.read();
logChar(c);
switch (c )
{
case -1:
case '\n':
break loop;
case '\r':
int c2 = in.read();
if (c2 != '\n')
{
if (!(in instanceof PushbackInputStream))
{
in = this.inStream = new PushbackInputStream(in);
}
((PushbackInputStream)in).unread(c2);
}
break loop;
default:
if (--room < 0)
{
buf = new char[offset + 128];
room = buf.length - offset - 1;
System.arraycopy(lineBuffer, 0, buf, 0, offset);
lineBuffer = buf;
}
buf[offset++] = (char) c;
break;
}
}
if ((c == -1) && (offset == 0))
{
return null;
}
log("\nOUT: " + String.copyValueOf(buf, 0, offset));
return String.copyValueOf(buf, 0, offset);
}
private String getResponse() throws IOException
{
// Get a response back from the server. Handles multi-line responses
// and returns them as part of the string.
String response = "";
for (;;)
{
String line = readLine();
if (line == null)
{
throw new IOException("Bad response from server.");
}
// FTP response lines should at the very least have a 3-digit number
if (line.length() < 3)
{
throw new IOException("Bad response from server.");
}
response += line + CRLF;
// If there isn't a '-' immediately after the number, we've gotten the
// complete response. ('-' is the continuation character for FTP responses)
if ((line.length() == 3) || (line.charAt(3) != '-'))
{
return response;
}
}
}
protected void log(String text)
{
if (DEBUG)
if (logOutput != null)
{
logOutput.println(text);
logOutput.flush();
}
}
private void logChar(int text)
{
if (DEBUG)
if (logOutput != null)
{
logOutput.print((char) text);
logOutput.flush();
}
}
}