1
2
3
4
5
6
7
8
9
10
11 package psiprobe.tools;
12
13 import jakarta.activation.DataSource;
14 import jakarta.activation.FileDataSource;
15
16 import java.io.File;
17 import java.util.ArrayList;
18 import java.util.List;
19
20
21
22
23 public class MailMessage {
24
25
26 private final List<String> to = new ArrayList<>();
27
28
29 private final List<String> cc = new ArrayList<>();
30
31
32 private final List<String> bcc = new ArrayList<>();
33
34
35 private final List<DataSource> attachments = new ArrayList<>();
36
37
38 private String subject = "";
39
40
41 private String body = "";
42
43
44 private boolean bodyHtml;
45
46
47
48
49
50
51
52
53 public MailMessage(String to, String subject, String body) {
54 addRecipientTo(to);
55 setSubject(subject);
56 setBody(body);
57 }
58
59
60
61
62
63
64 public String[] getToArray() {
65 return to.toArray(new String[to.size()]);
66 }
67
68
69
70
71
72
73 public String[] getCcArray() {
74 return cc.toArray(new String[cc.size()]);
75 }
76
77
78
79
80
81
82 public String[] getBccArray() {
83 return bcc.toArray(new String[bcc.size()]);
84 }
85
86
87
88
89
90
91 public DataSource[] getAttachmentsArray() {
92 return attachments.toArray(new DataSource[attachments.size()]);
93 }
94
95
96
97
98
99
100 public String getSubject() {
101 return subject;
102 }
103
104
105
106
107
108
109 public String getBody() {
110 return body;
111 }
112
113
114
115
116
117
118 public boolean isBodyHtml() {
119 return bodyHtml;
120 }
121
122
123
124
125
126
127
128
129 public MailMessage addRecipientTo(String address) {
130 if (address != null) {
131 to.add(address);
132 }
133 return this;
134 }
135
136
137
138
139
140
141
142
143 public MailMessage addRecipientCc(String address) {
144 if (address != null) {
145 cc.add(address);
146 }
147 return this;
148 }
149
150
151
152
153
154
155
156
157 public MailMessage addRecipientBcc(String address) {
158 if (address != null) {
159 bcc.add(address);
160 }
161 return this;
162 }
163
164
165
166
167
168
169
170
171 public MailMessage addAttachment(File attachment) {
172 FileDataSource ds = new FileDataSource(attachment);
173 return addAttachment(ds);
174 }
175
176
177
178
179
180
181
182
183 public MailMessage addAttachment(DataSource attachment) {
184 if (attachment != null) {
185 attachments.add(attachment);
186 }
187 return this;
188 }
189
190
191
192
193
194
195 public MailMessage clearRecipientsTo() {
196 to.clear();
197 return this;
198 }
199
200
201
202
203
204
205 public MailMessage clearRecipientsCc() {
206 cc.clear();
207 return this;
208 }
209
210
211
212
213
214
215 public MailMessage clearRecipientsBcc() {
216 bcc.clear();
217 return this;
218 }
219
220
221
222
223
224
225 public MailMessage clearAttachments() {
226 attachments.clear();
227 return this;
228 }
229
230
231
232
233
234
235
236
237 public MailMessage setSubject(String subject) {
238 this.subject = subject;
239 return this;
240 }
241
242
243
244
245
246
247
248
249 public MailMessage setBody(String body) {
250 this.body = body;
251 return this;
252 }
253
254
255
256
257
258
259
260
261 public MailMessage setBodyHtml(boolean bodyHtml) {
262 this.bodyHtml = bodyHtml;
263 return this;
264 }
265
266
267
268
269
270
271 protected List<String> getTo() {
272 return to;
273 }
274
275
276
277
278
279
280 protected List<String> getCc() {
281 return cc;
282 }
283
284
285
286
287
288
289 protected List<String> getBcc() {
290 return bcc;
291 }
292
293
294
295
296
297
298 protected List<DataSource> getAttachments() {
299 return attachments;
300 }
301
302 }