1 package com.ozacc.mail;
2
3 import java.io.UnsupportedEncodingException;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.Map;
10
11 import javax.mail.internet.AddressException;
12 import javax.mail.internet.InternetAddress;
13
14 /***
15 * ¥á¡¼¥?¡£
16 *
17 * @author Tomohiro Otsuka
18 * @version $Id: Mail.java,v 1.4 2004/09/10 22:41:42 otsuka Exp $
19 */
20 public class Mail {
21
22 /*** <code>ISO-2022-JP</code> */
23 public static final String JIS_CHARSET = "ISO-2022-JP";
24
25 private String charset = JIS_CHARSET;
26
27 private String text;
28
29 private InternetAddress from;
30
31 private String subject;
32
33 private List to;
34
35 private List cc;
36
37 private List bcc;
38
39 private InternetAddress returnPath;
40
41 private InternetAddress replyTo;
42
43 private String importance;
44
45 private Map xHeaders;
46
47 /***
48 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
49 */
50 public Mail() {}
51
52 /***
53 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
54 * °¸Àè¤äº¹½Ð¿Í¤Î̾Á°¤ò¥¨¥ó¥³¡¼¥É¤¹¤?»?¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É¤ò»ØÄꤷ¤Þ¤¹¡£
55 * ¥Ç¥Õ¥©¥?¥È¤Ï<code>ISO-2022-JP</code>¤Ç¤¹¡£
56 * <p>
57 * Æ?Ëܸ?´Ä¶¤ÇÍøÍѤ¹¤?¾?¹ç¤ÏÄ̾?Êѹ¹¤¹¤?ɬÍפϤ¢¤ê¤Þ¤»¤ó¡£
58 *
59 * @param charset ¥¨¥ó¥³¡¼¥É¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É
60 */
61 public Mail(String charset) {
62 this();
63 this.charset = charset;
64 }
65
66 /***
67 * ¥¨¥ó¥³¡¼¥É¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É¤òÊÖ¤·¤Þ¤¹¡£
68 *
69 * @return ¥¨¥ó¥³¡¼¥É¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É
70 */
71 public String getCharset() {
72 return charset;
73 }
74
75 /***
76 * ¥á¡¼¥?¤Î½ÅÍ×ÅÙ¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
77 * °ú¿ô¤Ç»ØÄ?²Äǽ¤ÊÃͤϡÖhigh¡×¡¢¡Önormal¡×¡¢¡Ölow¡×¤Î¤¤¤º¤?¤«¤Ç¤¹¡£
78 *
79 * @param importance ¥á¡¼¥?¤Î½ÅÍ×ÅÙ¡£¡Öhigh¡×¡¢¡Önormal¡×¡¢¡Ölow¡×¤Î¤¤¤º¤?¤«¡£
80 * @throws IllegalArgumentException »ØÄ?²Äǽ¤ÊÃͰʳ°¤¬»ØÄꤵ¤?¤¿¾?¹?
81 *
82 * @see Mail.Importance
83 */
84 public void setImportance(String importance) throws IllegalArgumentException {
85 if ("high".equals(importance) || "normal".equals(importance) || "low".equals(importance)) {
86 this.importance = importance;
87 } else {
88 throw new IllegalArgumentException("'" + importance + "'¤Ï¡¢¥á¡¼¥?½ÅÍ×Å٤ˤϻØÄê¤Ç¤¤Ê¤¤ÃͤǤ¹¡£");
89 }
90 }
91
92 /***
93 * ¥á¡¼¥?¤Î½ÅÍ×ÅÙ¤òÊÖ¤·¤Þ¤¹¡£
94 * ÃͤϡÖhigh¡×¡¢¡Önormal¡×¡¢¡Ölow¡×¤Î¤¤¤º¤?¤«¤Ç¤¹¡£
95 *
96 * @return ¥á¡¼¥?¤Î½ÅÍ×ÅÙ¡£¡Öhigh¡×¡¢¡Önormal¡×¡¢¡Ölow¡×¤Î¤¤¤º¤?¤«¡£
97 */
98 public String getImportance() {
99 return importance;
100 }
101
102 /***
103 * ¥á¡¼¥?¤ÎÁ÷¿®À襢¥É¥?¥¹¤òÄɲä·¤Þ¤¹¡£
104 *
105 * @param address Á÷¿®À襢¥É¥?¥¹
106 */
107 public void addTo(InternetAddress address) {
108 if (to == null) {
109 to = new ArrayList();
110 }
111 to.add(address);
112 }
113
114 /***
115 * ¥á¡¼¥?¤ÎÁ÷¿®À襢¥É¥?¥¹¤òÄɲä·¤Þ¤¹¡£
116 *
117 * @param email Á÷¿®À襢¥É¥?¥¹
118 * @throws IllegalArgumentException ÉÔÀµ¤Ê¥Õ¥©¡¼¥Þ¥Ã¥È¤Î¥¢¥É¥?¥¹¤¬»ØÄꤵ¤?¤¿¾?¹?
119 */
120 public void addTo(String email) throws IllegalArgumentException {
121 try {
122 addTo(new InternetAddress(email));
123 } catch (AddressException e) {
124 throw new IllegalArgumentException(e.getMessage());
125 }
126 }
127
128 /***
129 * ¥á¡¼¥?¤ÎÁ÷¿®Àè̾¤È¥¢¥É¥?¥¹¤òÄɲä·¤Þ¤¹¡£
130 * ̾Á°¤ÏJIS_CHARSET¤Ç¥¨¥ó¥³¡¼¥É¤µ¤?¤Þ¤¹¡£
131 *
132 * @param email Á÷¿®À襢¥É¥?¥¹
133 * @param name Á÷¿®Àè̾
134 * @throws IllegalArgumentException ÉÔÀµ¤Ê¥Õ¥©¡¼¥Þ¥Ã¥È¤Î¥¢¥É¥?¥¹¤¬»ØÄꤵ¤?¤¿¾?¹?
135 */
136 public void addTo(String email, String name) throws IllegalArgumentException {
137 try {
138 addTo(new InternetAddress(email, name, charset));
139 } catch (UnsupportedEncodingException e) {
140 throw new IllegalArgumentException(e.getMessage());
141 }
142 }
143
144 /***
145 * ¥á¡¼¥?¤ÎÁ÷¿®À襢¥É¥?¥¹¤ÎÇÛÎó¤òÊÖ¤·¤Þ¤¹¡£
146 * Á÷¿®À襢¥É¥?¥¹¤¬°?·?¤â¥»¥Ã¥È¤µ¤?¤Æ¤¤¤Ê¤¤¤È¤¤Ï¶õ¤ÎÇÛÎó¤òÊÖ¤·¤Þ¤¹¡£
147 *
148 * @return Á÷¿®À襢¥É¥?¥¹¤ÎÇÛÎ?
149 */
150 public InternetAddress[] getTo() {
151 if (to == null) {
152 return new InternetAddress[0];
153 }
154 return (InternetAddress[])to.toArray(new InternetAddress[to.size()]);
155 }
156
157 /***
158 * CC¥¢¥É¥?¥¹¤òÄɲä·¤Þ¤¹¡£
159 *
160 * @param address CC¤Î¥¢¥É¥?¥¹
161 */
162 public void addCc(InternetAddress address) {
163 if (cc == null) {
164 cc = new ArrayList();
165 }
166 cc.add(address);
167 }
168
169 /***
170 * CC¥¢¥É¥?¥¹¤òÄɲä·¤Þ¤¹¡£
171 *
172 * @param email CC¤Î¥¢¥É¥?¥¹
173 * @throws IllegalArgumentException ÉÔÀµ¤Ê¥Õ¥©¡¼¥Þ¥Ã¥È¤Î¥¢¥É¥?¥¹¤¬»ØÄꤵ¤?¤¿¾?¹?
174 */
175 public void addCc(String email) throws IllegalArgumentException {
176 try {
177 addCc(new InternetAddress(email));
178 } catch (AddressException e) {
179 throw new IllegalArgumentException(e.getMessage());
180 }
181 }
182
183 /***
184 * CC¤Î°¸Ì¾¤È¥¢¥É¥?¥¹¤òÄɲä·¤Þ¤¹¡£
185 * ̾Á°¤ÏJIS_CHARSET¤Ç¥¨¥ó¥³¡¼¥É¤µ¤?¤Þ¤¹¡£
186 *
187 * @param email CC¤Î¥¢¥É¥?¥¹
188 * @param name CC¤Î°¸Ì¾
189 * @throws IllegalArgumentException ÉÔÀµ¤Ê¥Õ¥©¡¼¥Þ¥Ã¥È¤Î¥¢¥É¥?¥¹¤¬»ØÄꤵ¤?¤¿¾?¹?
190 */
191 public void addCc(String email, String name) throws IllegalArgumentException {
192 try {
193 addCc(new InternetAddress(email, name, charset));
194 } catch (UnsupportedEncodingException e) {
195 throw new IllegalArgumentException(e.getMessage());
196 }
197 }
198
199 /***
200 * ¥á¡¼¥?¤ÎCC¥¢¥É¥?¥¹ÇÛÎó¤òÊÖ¤·¤Þ¤¹¡£
201 * CC¥¢¥É¥?¥¹¤¬°?·?¤â¥»¥Ã¥È¤µ¤?¤Æ¤¤¤Ê¤¤¤È¤¤Ï¶õ¤ÎÇÛÎó¤òÊÖ¤·¤Þ¤¹¡£
202 *
203 * @return CC¥¢¥É¥?¥¹¤ÎÇÛÎ?
204 */
205 public InternetAddress[] getCc() {
206 if (cc == null) {
207 return new InternetAddress[0];
208 }
209 return (InternetAddress[])cc.toArray(new InternetAddress[cc.size()]);
210 }
211
212 /***
213 * BCC¥¢¥É¥?¥¹¤òÄɲä·¤Þ¤¹¡£
214 *
215 * @param address BCC¤Î¥¢¥É¥?¥¹
216 */
217 public void addBcc(InternetAddress address) {
218 if (bcc == null) {
219 bcc = new ArrayList();
220 }
221 bcc.add(address);
222 }
223
224 /***
225 * BCC¥¢¥É¥?¥¹¤òÄɲä·¤Þ¤¹¡£
226 *
227 * @param email BCC¤Î¥¢¥É¥?¥¹
228 * @throws IllegalArgumentException ÉÔÀµ¤Ê¥Õ¥©¡¼¥Þ¥Ã¥È¤Î¥¢¥É¥?¥¹¤¬»ØÄꤵ¤?¤¿¾?¹?
229 */
230 public void addBcc(String email) throws IllegalArgumentException {
231 try {
232 addBcc(new InternetAddress(email));
233 } catch (AddressException e) {
234 throw new IllegalArgumentException(e.getMessage());
235 }
236 }
237
238 /***
239 * ¥á¡¼¥?¤ÎBCC¥¢¥É¥?¥¹¤ÎÇÛÎó¤òÊÖ¤·¤Þ¤¹¡£
240 * BCC¥¢¥É¥?¥¹¤¬°?·?¤â¥»¥Ã¥È¤µ¤?¤Æ¤¤¤Ê¤¤¤È¤¤Ï¶õ¤ÎÇÛÎó¤òÊÖ¤·¤Þ¤¹¡£
241 *
242 * @return BCC¥¢¥É¥?¥¹¤ÎÇÛÎ?
243 */
244 public InternetAddress[] getBcc() {
245 if (bcc == null) {
246 return new InternetAddress[0];
247 }
248 return (InternetAddress[])bcc.toArray(new InternetAddress[bcc.size()]);
249 }
250
251 /***
252 * ¥á¡¼¥?¤Îº¹½Ð¿Í¥¢¥É¥?¥¹¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
253 *
254 * @param address º¹½Ð¿Í¥¢¥É¥?¥¹
255 */
256 public void setFrom(InternetAddress address) {
257 from = address;
258 }
259
260 /***
261 * ¥á¡¼¥?¤Îº¹½Ð¿Í¥¢¥É¥?¥¹¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
262 *
263 * @param email º¹½Ð¿Í¥¢¥É¥?¥¹
264 * @throws IllegalArgumentException ÉÔÀµ¤Ê¥Õ¥©¡¼¥Þ¥Ã¥È¤Î¥¢¥É¥?¥¹¤¬»ØÄꤵ¤?¤¿¾?¹?
265 */
266 public void setFrom(String email) throws IllegalArgumentException {
267 try {
268 setFrom(new InternetAddress(email));
269 } catch (AddressException e) {
270 throw new IllegalArgumentException(e.getMessage());
271 }
272 }
273
274 /***
275 * ¥á¡¼¥?¤Îº¹½Ð¿Í̾¤È¥¢¥É¥?¥¹¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
276 * ̾Á°¤ÏJIS_CHARSET¤Ç¥¨¥ó¥³¡¼¥É¤µ¤?¤Þ¤¹¡£
277 *
278 * @param email º¹½Ð¿Í¥¢¥É¥?¥¹
279 * @param name º¹½Ð¿Í̾
280 * @throws IllegalArgumentException ÉÔÀµ¤Ê¥Õ¥©¡¼¥Þ¥Ã¥È¤Î¥¢¥É¥?¥¹¤¬»ØÄꤵ¤?¤¿¾?¹?
281 */
282 public void setFrom(String email, String name) throws IllegalArgumentException {
283 try {
284 setFrom(new InternetAddress(email, name, charset));
285 } catch (UnsupportedEncodingException e) {
286 throw new IllegalArgumentException(e.getMessage());
287 }
288 }
289
290 /***
291 * ¥á¡¼¥?¤Îº¹½Ð¿Í¥¢¥É¥?¥¹¤òÊÖ¤·¤Þ¤¹¡£¥»¥Ã¥È¤µ¤?¤Æ¤¤¤Ê¤¤¾?¹ç¤Ïnull¤òÊÖ¤·¤Þ¤¹¡£
292 *
293 * @return ¥á¡¼¥?¤Îº¹½Ð¿Í¥¢¥É¥?¥¹
294 */
295 public InternetAddress getFrom() {
296 return from;
297 }
298
299 /***
300 * Return-Path¥¢¥É¥?¥¹¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
301 *
302 * @param address Return-Path¥¢¥É¥?¥¹
303 */
304 public void setReturnPath(InternetAddress address) {
305 returnPath = address;
306 }
307
308 /***
309 * Return-Path¥¢¥É¥?¥¹¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
310 *
311 * @param email Return-Path¥¢¥É¥?¥¹
312 * @throws IllegalArgumentException ÉÔÀµ¤Ê¥Õ¥©¡¼¥Þ¥Ã¥È¤Î¥¢¥É¥?¥¹¤¬»ØÄꤵ¤?¤¿¾?¹?
313 */
314 public void setReturnPath(String email) throws IllegalArgumentException {
315 try {
316 setReturnPath(new InternetAddress(email));
317 } catch (AddressException e) {
318 throw new IllegalArgumentException(e.getMessage());
319 }
320 }
321
322 /***
323 * Return-Path¥¢¥É¥?¥¹¤òÊÖ¤·¤Þ¤¹¡£
324 *
325 * @return Return-Path¥¢¥É¥?¥¹
326 */
327 public InternetAddress getReturnPath() {
328 return returnPath;
329 }
330
331 /***
332 * ÊÖ¿®À襢¥É¥?¥¹¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
333 *
334 * @param address ÊÖ¿®À襢¥É¥?¥¹
335 */
336 public void setReplyTo(InternetAddress address) {
337 replyTo = address;
338 }
339
340 /***
341 * ÊÖ¿®À襢¥É¥?¥¹¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
342 *
343 * @param email ÊÖ¿®À襢¥É¥?¥¹
344 * @throws IllegalArgumentException ÉÔÀµ¤Ê¥Õ¥©¡¼¥Þ¥Ã¥È¤Î¥¢¥É¥?¥¹¤¬»ØÄꤵ¤?¤¿¾?¹?
345 */
346 public void setReplyTo(String email) throws IllegalArgumentException {
347 try {
348 setReplyTo(new InternetAddress(email));
349 } catch (AddressException e) {
350 throw new IllegalArgumentException(e.getMessage());
351 }
352 }
353
354 /***
355 * ¥á¡¼¥?¤ÎÊÖ¿®À襢¥É¥?¥¹¤òÊÖ¤·¤Þ¤¹¡£¥»¥Ã¥È¤µ¤?¤Æ¤¤¤Ê¤¤¾?¹ç¤Ïnull¤òÊÖ¤·¤Þ¤¹¡£
356 *
357 * @return ÊÖ¿®À襢¥É¥?¥¹
358 */
359 public InternetAddress getReplyTo() {
360 return replyTo;
361 }
362
363 /***
364 * ¥á¡¼¥?¤Î·?̾¤òÊÖ¤·¤Þ¤¹¡£¥»¥Ã¥È¤µ¤?¤Æ¤¤¤Ê¤¤¾?¹ç¤Ï¶õʸ»úÎó¤òÊÖ¤·¤Þ¤¹¡£
365 *
366 * @return ¥á¡¼¥?¤Î·?̾
367 */
368 public String getSubject() {
369 if (subject == null) {
370 return "";
371 }
372 return subject;
373 }
374
375 /***
376 * ¥á¡¼¥?¤Î·?̾¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
377 *
378 * @param subject ¥á¡¼¥?¤Î·?̾
379 */
380 public void setSubject(String subject) {
381 this.subject = subject;
382 }
383
384 /***
385 * ¥á¡¼¥?ËÜʸ¤òÊÖ¤·¤Þ¤¹¡£
386 * ËÜʸ¥»¥Ã¥È¤µ¤?¤Æ¤¤¤Ê¤¤¾?¹ç¤Ï¶õʸ»úÎó¤òÊÖ¤·¤Þ¤¹¡£
387 *
388 * @return ¥á¡¼¥?ËÜʸ
389 */
390 public String getText() {
391 if (text == null) {
392 return "";
393 }
394 return text;
395 }
396
397 /***
398 * ¥á¡¼¥?ËÜʸ¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
399 *
400 * @param text ¥á¡¼¥?ËÜʸ
401 */
402 public void setText(String text) {
403 this.text = text;
404 }
405
406 /***
407 * ¥á¡¼¥?¥Ø¥Ã¥À¤ËǤ°Õ¤Î¥Ø¥Ã¥À¤òÄɲä·¤Þ¤¹¡£
408 * Ǥ°Õ¥Ø¥Ã¥À¤Ï¡ÖX-key: value¡×¤Î¥Õ¥©¡¼¥Þ¥Ã¥È¤Ç¥á¡¼¥?¥Ø¥Ã¥À¤ËÁȤ߹?¤Þ¤?¤Þ¤¹¡£
409 *
410 * @param key Ǥ°Õ¥Ø¥Ã¥À̾¡£Æ¬¤¬"X-"¤Ç»Ï¤Þ¤Ã¤Æ¤¤¤Ê¤±¤?¤Ð¡¢¼«Æ°Åª¤ËÉÕÍ¿¤µ¤?¤Þ¤¹¡£
411 * @param value Ǥ°Õ¥Ø¥Ã¥À¤ÎÃÍ
412 */
413 public void addXHeader(String key, String value) {
414 if (xHeaders == null) {
415 xHeaders = new HashMap();
416 }
417 if (key.startsWith("X-")) {
418 xHeaders.put(key, value);
419 } else {
420 xHeaders.put("X-" + key, value);
421 }
422 }
423
424 /***
425 * ¥á¡¼¥?¤ÎǤ°Õ¥Ø¥Ã¥À̾¤ÈÃͤÎMap¥¤¥ó¥¹¥¿¥ó¥¹¤òÊÖ¤·¤Þ¤¹¡£
426 * Ǥ°Õ¥Ø¥Ã¥À¤¬°?·?¤â¥»¥Ã¥È¤µ¤?¤Æ¤¤¤Ê¤¤¤È¤¤Ïnull¤òÊÖ¤·¤Þ¤¹¡£
427 * <p>
428 * ¤³¤ÎMap¥¤¥ó¥¹¥¿¥ó¥¹¤Ø¤Î½¤Àµ¤Ï¤Ç¤¤Þ¤»¤ó¡£(unmodifiableMap¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£)
429 *
430 * @return ¥á¡¼¥?¤ÎǤ°Õ¥Ø¥Ã¥À̾¤ÈÃͤÎMap¥¤¥ó¥¹¥¿¥ó¥¹¡£¤Þ¤¿¤Ïnull¡£
431 */
432 public Map getXHeaders() {
433 if (xHeaders == null) {
434 return null;
435 }
436 return Collections.unmodifiableMap(xHeaders);
437 }
438
439 /***
440 * ¥á¡¼¥?ÆâÍÆ¤ò½ÐÎϤ·¤Þ¤¹¡£<br>
441 * ¥á¡¼¥?¤Î¥½¡¼¥¹¤Ë»÷¤¿¥Õ¥©¡¼¥Þ¥Ã¥È¤Ç½ÐÎϤµ¤?¤Þ¤¹¡£
442 *
443 * @see java.lang.Object#toString()
444 */
445 public String toString() {
446 StringBuffer buf = new StringBuffer(1000);
447 buf.append("Mail\n");
448 buf.append("Return-Path: ").append(returnPath).append("\n");
449 buf.append("From: ").append(from != null ? from.toUnicodeString() : null).append("\n");
450 buf.append("To: ").append(arrayToCommaDelimitedString(to)).append("\n");
451 buf.append("Cc: ").append(arrayToCommaDelimitedString(cc)).append("\n");
452 buf.append("Bcc: ").append(arrayToCommaDelimitedString(bcc)).append("\n");
453 buf.append("Subject: ").append(subject).append("\n");
454
455 if (xHeaders != null) {
456 for (Iterator itr = xHeaders.keySet().iterator(); itr.hasNext();) {
457 String header = (String)itr.next();
458 String value = (String)xHeaders.get(header);
459 buf.append(header).append(": ").append(value).append("\n");
460 }
461 }
462
463 buf.append("\n");
464 buf.append(text);
465
466 return buf.toString();
467 }
468
469 /***
470 * @param list
471 * @return
472 */
473 private String arrayToCommaDelimitedString(List list) {
474 if (list == null) {
475 return "null";
476 } else {
477 StringBuffer sb = new StringBuffer();
478 for (int i = 0, num = list.size(); i < num; i++) {
479 if (i > 0) {
480 sb.append(", ");
481 }
482 sb.append(((InternetAddress)list.get(i)).toUnicodeString());
483 }
484 return sb.toString();
485 }
486 }
487
488 /***
489 * ¥á¡¼¥?¤Î½ÅÍ×ÅÙ¡£Ä?¿ô¤Î¤ß¤òÄ?µÁ¡£
490 *
491 * @author Tomohiro Otsuka
492 * @version $Id: Mail.java,v 1.4 2004/09/10 22:41:42 otsuka Exp $
493 */
494 public static class Importance {
495
496 /*** ½ÅÍ×ÅÙ¡Ö¹â¡× */
497 public static final String HIGH = "high";
498
499 /*** ½ÅÍ×ÅÙ¡ÖÃæ¡× */
500 public static final String NORMAL = "normal";
501
502 /*** ½ÅÍ×ÅÙ¡ÖÄã¡× */
503 public static final String LOW = "low";
504
505 }
506 }