-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSMTP.java
More file actions
175 lines (158 loc) · 4.5 KB
/
SMTP.java
File metadata and controls
175 lines (158 loc) · 4.5 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
package javaforce;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.net.*;
import javax.net.ssl.*;
/**
* SMTP client class.
*/
public class SMTP {
public SMTP() {
}
private Socket s;
private InputStream is;
private OutputStream os;
private BufferedReader br;
private boolean passive = true;
private String host;
private ServerSocket active; //active socket
private boolean log = false;
public boolean debug = false;
/**
* Holds the repsonse strings from the last executed command
*/
public String response[];
public boolean connect(String host, int port) throws Exception {
s = new Socket(host, port);
is = s.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
os = s.getOutputStream();
this.host = host;
getResponse();
if (response[response.length - 1].startsWith("220")) {
return true;
}
disconnect(); //not valid SMTP site
return false;
}
public boolean connectSSL(String host, int port) throws Exception {
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
// Let us create the factory where we can set some parameters for the connection
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
// SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); //this method will only work with trusted certs
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) sc.getSocketFactory(); //this method will work with untrusted certs
SSLSocket ssl = (SSLSocket) sslsocketfactory.createSocket(host, port);
s = (Socket) ssl;
is = s.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
os = s.getOutputStream();
this.host = host;
getResponse();
if (response[response.length - 1].startsWith("220")) {
return true;
}
disconnect(); //not valid SMTP site
return false;
}
public void disconnect() throws Exception {
if (s != null) {
s.close();
}
s = null;
is = null;
os = null;
}
public void setLogging(boolean state) {
log = state;
}
public boolean login() throws Exception {
cmd("HELO " + host);
getResponse();
if (!response[response.length - 1].startsWith("250")) {
return false;
}
return true;
}
public void logout() throws Exception {
cmd("quit");
getResponse(); //should be "221" but ignored
}
public void cmd(String cmd) throws Exception {
if ((s == null) || (s.isClosed())) {
throw new Exception("not connected");
}
if (log) {
if (cmd.startsWith("pass ")) {
JFLog.log("pass ****");
} else {
JFLog.log(cmd);
}
}
cmd += "\r\n";
os.write(cmd.getBytes());
}
public void from(String email) throws Exception {
cmd("MAIL FROM:<" + email + ">");
getResponse();
}
public void to(String email) throws Exception {
cmd("RCPT TO:<" + email + ">");
getResponse();
}
public boolean data(String msg) throws Exception {
/*
sample data:
From: "First Last" <[email protected]>
To: "First Last" <[email protected]>
Cc: "First Last" <[email protected]>
Date: Tue, 15 Jan 2008 16:02:43 -0500
Subject: Subject line
Hello Bob,
blah blah blah
make sure you don't have . (period) on a line by itself since that ends the message.
*/
cmd("DATA");
getResponse();
if (!response[response.length - 1].startsWith("354")) {
return false;
}
os.write(msg.getBytes());
os.write("\r\n.\r\n".getBytes());
getResponse();
if (!response[response.length - 1].startsWith("250")) {
return false;
}
return true;
}
private void getResponse() throws Exception {
ArrayList<String> tmp = new ArrayList<String>();
String str;
while (!s.isClosed()) {
str = br.readLine();
tmp.add(str);
if (str.charAt(3) == ' ') {
break;
}
}
int size = tmp.size();
response = new String[size];
for (int a = 0; a < size; a++) {
response[a] = tmp.get(a);
if (debug) {
System.out.println(response[a]);
}
}
}
};