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