SimpleMail.java
5.99 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
package br.gov.mc.cadsei.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SimpleMail {
private String text;
private String subject;
private String from;
private String password;
private Boolean html;
private String host;
private String smtpServer;
private String smtpPort;
private String user;
private String port;
private String replyTo;
private String socksProxyHost;
private String socksProxyPort;
private String smtpTimeout;
private List<Address> to;
private List<Address> cc;
public SimpleMail() {
this.setTo(new ArrayList<Address>());
this.setCc(new ArrayList<Address>());
}
public void enviarEmail() throws Exception {
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
if (port != null || port.length() > 0) {
props.put("mail.smtp.port", port);
}
props.put("mail.smtp.auth", getPassword() == null ? "false" : "true");
if (socksProxyHost != null && !socksProxyHost.equals("")) {
props.setProperty("socksProxyHost", socksProxyHost);
props.setProperty("socksProxyPort", socksProxyPort);
}
if (smtpTimeout != null && !smtpTimeout.equals("")) {
props.setProperty("mail.smtp.connectiontimeout",smtpTimeout);
props.setProperty("mail.smtp.timeout", smtpTimeout);
}
Authenticator auth = new SMTPAuthenticator();
Session mailSession = Session.getDefaultInstance(props, auth);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setContent(text, (getHtml() ? "text/html" : "text/plain"));
message.setSubject(subject);
message.setFrom(new InternetAddress(from));
// Forçando REPLY TO
if (getReplyTo() != null && !"".equals(getReplyTo().trim())) {
ArrayList<Address> replys = new ArrayList<Address>();
replys.add(new InternetAddress(getReplyTo()));
InternetAddress[] tempReply = new InternetAddress[1];
replys.toArray(tempReply);
message.setReplyTo(tempReply);
}
InternetAddress[] tempArray = new InternetAddress[to.size()];
getTo().toArray(tempArray);
message.addRecipients(Message.RecipientType.TO, tempArray);
if (cc != null) {
tempArray = new InternetAddress[cc.size()];
cc.toArray(tempArray);
cc = null;
message.addRecipients(Message.RecipientType.CC, tempArray);
}
System.out.println("# Conectando ao servidor: " + smtpServer);
System.out.println("# Usuario......: " + user);
transport.connect();
System.out.println("# Conecção bem sucedida.");
System.out.println("# Enviado o email para :" + to.toString());
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
System.out.println("# Mensagem enviada com sucesso.");
transport.close();
}
public void addCC() {
}
public void addTO(String addr) throws AddressException {
getTo().add(new InternetAddress(addr));
}
public void addCC(String addr) throws AddressException {
getCc().add(new InternetAddress(addr));
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public boolean isHtml() {
return getHtml();
}
public void setHtml(boolean html) {
this.html = html;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getSmtpServer() {
return smtpServer;
}
public void setSmtpServer(String smtpServer) {
this.smtpServer = smtpServer;
}
public String getSmtpPort() {
return smtpPort;
}
public void setSmtpPort(String smtpPort) {
this.smtpPort = smtpPort;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Boolean getHtml() {
return html;
}
public void setHtml(Boolean html) {
this.html = html;
}
public List<Address> getTo() {
return to;
}
public void setTo(List<Address> to) {
this.to = to;
}
public List<Address> getCc() {
return cc;
}
public void setCc(List<Address> cc) {
this.cc = cc;
}
public String getReplyTo() {
return replyTo;
}
public void setReplyTo(String replyTo) {
this.replyTo = replyTo;
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, getPassword());
}
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getSocksProxyPort() {
return socksProxyPort;
}
public String getSocksProxyHost() {
return socksProxyHost;
}
public void setSocksProxyHost(String socksProxyHost) {
this.socksProxyHost = socksProxyHost;
}
/**
* @return the smtpTimeout
*/
public String getSmtpTimeout() {
return smtpTimeout;
}
/**
* @param smtpTimeout the smtpTimeout to set
*/
public void setSmtpTimeout(String smtpTimeout) {
this.smtpTimeout = smtpTimeout;
}
public void setSocksProxyPort(String socksProxyPort) {
this.socksProxyPort = socksProxyPort;
}
}