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