View Javadoc

1   package com.ozacc.mail;
2   
3   import java.io.File;
4   import java.io.InputStream;
5   import java.io.UnsupportedEncodingException;
6   import java.net.URL;
7   import java.util.ArrayList;
8   import java.util.Collections;
9   import java.util.HashMap;
10  import java.util.Iterator;
11  import java.util.List;
12  import java.util.Map;
13  
14  import javax.activation.DataSource;
15  import javax.activation.FileDataSource;
16  import javax.activation.FileTypeMap;
17  import javax.activation.URLDataSource;
18  import javax.mail.internet.AddressException;
19  import javax.mail.internet.InternetAddress;
20  
21  import com.ozacc.mail.impl.ByteArrayDataSource;
22  import com.ozacc.mail.impl.Cp932;
23  
24  /***
25   * メール。
26   * 
27   * @since 1.0
28   * @author Tomohiro Otsuka
29   * @version $Id: Mail.java,v 1.10.2.5 2005/01/23 10:39:11 otsuka Exp $
30   */
31  public class Mail {
32  
33  	/*** <code>ISO-2022-JP</code> */
34  	public static final String JIS_CHARSET = "ISO-2022-JP";
35  
36  	public static final String DOCTYPE_PUBLIC = "-//OZACC//DTD MAIL//EN";
37  
38  	public static final String DOCTYPE_SYSTEM = "http://www.ozacc.com/library/dtd/ozacc-mail.dtd";
39  
40  	public static final String DOCTYPE_PUBLIC_MULTIPLE = "-//OZACC//DTD MULTIPLE MAILS//EN";
41  
42  	public static final String DOCTYPE_SYSTEM_MULTIPLE = "http://www.ozacc.com/library/dtd/ozacc-multiple-mails.dtd";
43  
44  	private String charset = JIS_CHARSET;
45  
46  	protected String text;
47  
48  	protected InternetAddress from;
49  
50  	protected String subject;
51  
52  	protected List to;
53  
54  	protected List cc;
55  
56  	protected List bcc;
57  
58  	protected List envelopeTo;
59  
60  	protected InternetAddress returnPath;
61  
62  	protected InternetAddress replyTo;
63  
64  	protected String importance;
65  
66  	protected Map headers;
67  
68  	protected String htmlText;
69  
70  	protected List attachmentFiles;
71  
72  	/***
73  	 * コンストラクタ。
74  	 */
75  	public Mail() {}
76  
77  	/***
78  	 * コンストラクタ。
79  	 * 宛先や差出人の名前をエンコードする時に使用する文字コードを指定します。
80  	 * デフォルトは<code>ISO-2022-JP</code>です。
81  	 * <p>
82  	 * 日本語環境で利用する場合は通常変更する必要はありません。
83  	 * 
84  	 * @param charset エンコードに使用する文字コード
85  	 */
86  	public Mail(String charset) {
87  		this();
88  		this.charset = charset;
89  	}
90  
91  	/***
92  	 * コピーコンストラクタ。
93  	 * シャローコピー(shallow copy)です。
94  	 * 
95  	 * @since 1.0.2
96  	 * 
97  	 * @param original コピー元のMailインスタンス
98  	 */
99  	public Mail(Mail original) {
100 		this.bcc = original.bcc;
101 		this.cc = original.cc;
102 		this.charset = original.charset;
103 		this.from = original.from;
104 		this.importance = original.importance;
105 		this.replyTo = original.replyTo;
106 		this.returnPath = original.returnPath;
107 		this.subject = original.subject;
108 		this.text = original.text;
109 		this.to = original.to;
110 		this.headers = original.headers;
111 		this.htmlText = original.htmlText;
112 		this.attachmentFiles = original.attachmentFiles;
113 		this.envelopeTo = original.envelopeTo;
114 	}
115 
116 	/***
117 	 * エンコードに使用する文字コードを返します。
118 	 * 
119 	 * @return エンコードに使用する文字コード
120 	 */
121 	public String getCharset() {
122 		return charset;
123 	}
124 
125 	/***
126 	 * メールの重要度をセットします。
127 	 * 引数で指定可能な値は「high」、「normal」、「low」のいずれかです。
128 	 * 
129 	 * @param importance メールの重要度。「high」、「normal」、「low」のいずれか。
130 	 * @throws IllegalArgumentException 指定可能な値以外が指定された場合
131 	 * 
132 	 * @see Mail.Importance
133 	 */
134 	public void setImportance(String importance) throws IllegalArgumentException {
135 		if ("high".equals(importance) || "normal".equals(importance) || "low".equals(importance)) {
136 			this.importance = importance;
137 		} else {
138 			throw new IllegalArgumentException("'" + importance + "'は、メール重要度には指定できない値です。");
139 		}
140 	}
141 
142 	/***
143 	 * メールの重要度を返します。
144 	 * 値は「high」、「normal」、「low」のいずれかです。
145 	 * 
146 	 * @return メールの重要度。「high」、「normal」、「low」のいずれか。
147 	 */
148 	public String getImportance() {
149 		return importance;
150 	}
151 
152 	/***
153 	 * メールの送信先アドレスを追加します。
154 	 * 
155 	 * @param address 送信先アドレス
156 	 */
157 	public void addTo(InternetAddress address) {
158 		if (to == null) {
159 			to = new ArrayList();
160 		}
161 		to.add(address);
162 	}
163 
164 	/***
165 	 * メールの送信先アドレスを追加します。
166 	 * 
167 	 * @param email 送信先アドレス
168 	 * @throws IllegalArgumentException 不正なフォーマットのアドレスが指定された場合
169 	 */
170 	public void addTo(String email) throws IllegalArgumentException {
171 		try {
172 			addTo(new InternetAddress(email));
173 		} catch (AddressException e) {
174 			throw new IllegalArgumentException(e.getMessage());
175 		}
176 	}
177 
178 	/***
179 	 * メールの送信先名とアドレスを追加します。
180 	 * 名前はJIS_CHARSETでエンコードされます。
181 	 * 
182 	 * @param email 送信先アドレス
183 	 * @param name 送信先名
184 	 * @throws IllegalArgumentException 不正なフォーマットのアドレスが指定された場合
185 	 */
186 	public void addTo(String email, String name) throws IllegalArgumentException {
187 		if (charset.equals(JIS_CHARSET)) {
188 			name = Cp932.toJIS(name);
189 		}
190 		try {
191 			addTo(new InternetAddress(email, name, charset));
192 		} catch (UnsupportedEncodingException e) {
193 			throw new IllegalArgumentException(e.getMessage());
194 		}
195 	}
196 
197 	/***
198 	 * メールの送信先アドレスの配列を返します。
199 	 * 送信先アドレスが一件もセットされていないときは空の配列を返します。
200 	 * 
201 	 * @return 送信先アドレスの配列
202 	 */
203 	public InternetAddress[] getTo() {
204 		if (to == null) {
205 			return new InternetAddress[0];
206 		}
207 		return (InternetAddress[])to.toArray(new InternetAddress[to.size()]);
208 	}
209 
210 	/***
211 	 * CCアドレスを追加します。
212 	 * 
213 	 * @param address CCのアドレス
214 	 */
215 	public void addCc(InternetAddress address) {
216 		if (cc == null) {
217 			cc = new ArrayList();
218 		}
219 		cc.add(address);
220 	}
221 
222 	/***
223 	 * CCアドレスを追加します。
224 	 * 
225 	 * @param email CCのアドレス
226 	 * @throws IllegalArgumentException 不正なフォーマットのアドレスが指定された場合
227 	 */
228 	public void addCc(String email) throws IllegalArgumentException {
229 		try {
230 			addCc(new InternetAddress(email));
231 		} catch (AddressException e) {
232 			throw new IllegalArgumentException(e.getMessage());
233 		}
234 	}
235 
236 	/***
237 	 * CCの宛名とアドレスを追加します。
238 	 * 名前はJIS_CHARSETでエンコードされます。
239 	 * 
240 	 * @param email CCのアドレス
241 	 * @param name CCの宛名
242 	 * @throws IllegalArgumentException 不正なフォーマットのアドレスが指定された場合
243 	 */
244 	public void addCc(String email, String name) throws IllegalArgumentException {
245 		if (charset.equals(JIS_CHARSET)) {
246 			name = Cp932.toJIS(name);
247 		}
248 		try {
249 			addCc(new InternetAddress(email, name, charset));
250 		} catch (UnsupportedEncodingException e) {
251 			throw new IllegalArgumentException(e.getMessage());
252 		}
253 	}
254 
255 	/***
256 	 * メールのCCアドレス配列を返します。
257 	 * CCアドレスが一件もセットされていないときは空の配列を返します。
258 	 * 
259 	 * @return CCアドレスの配列
260 	 */
261 	public InternetAddress[] getCc() {
262 		if (cc == null) {
263 			return new InternetAddress[0];
264 		}
265 		return (InternetAddress[])cc.toArray(new InternetAddress[cc.size()]);
266 	}
267 
268 	/***
269 	 * BCCアドレスを追加します。
270 	 * 
271 	 * @param address BCCのアドレス
272 	 */
273 	public void addBcc(InternetAddress address) {
274 		if (bcc == null) {
275 			bcc = new ArrayList();
276 		}
277 		bcc.add(address);
278 	}
279 
280 	/***
281 	 * BCCアドレスを追加します。
282 	 * 
283 	 * @param email BCCのアドレス
284 	 * @throws IllegalArgumentException 不正なフォーマットのアドレスが指定された場合
285 	 */
286 	public void addBcc(String email) throws IllegalArgumentException {
287 		try {
288 			addBcc(new InternetAddress(email));
289 		} catch (AddressException e) {
290 			throw new IllegalArgumentException(e.getMessage());
291 		}
292 	}
293 
294 	/***
295 	 * メールのBCCアドレスの配列を返します。
296 	 * BCCアドレスが一件もセットされていないときは空の配列を返します。
297 	 * 
298 	 * @return BCCアドレスの配列
299 	 */
300 	public InternetAddress[] getBcc() {
301 		if (bcc == null) {
302 			return new InternetAddress[0];
303 		}
304 		return (InternetAddress[])bcc.toArray(new InternetAddress[bcc.size()]);
305 	}
306 
307 	/***
308 	 * メールの差出人アドレスをセットします。
309 	 * 
310 	 * @param address 差出人アドレス
311 	 */
312 	public void setFrom(InternetAddress address) {
313 		from = address;
314 	}
315 
316 	/***
317 	 * メールの差出人アドレスをセットします。
318 	 * 
319 	 * @param email 差出人アドレス
320 	 * @throws IllegalArgumentException 不正なフォーマットのアドレスが指定された場合
321 	 */
322 	public void setFrom(String email) throws IllegalArgumentException {
323 		try {
324 			setFrom(new InternetAddress(email));
325 		} catch (AddressException e) {
326 			throw new IllegalArgumentException(e.getMessage());
327 		}
328 	}
329 
330 	/***
331 	 * メールの差出人名とアドレスをセットします。
332 	 * 名前はJIS_CHARSETでエンコードされます。
333 	 * 
334 	 * @param email 差出人アドレス
335 	 * @param name 差出人名
336 	 * @throws IllegalArgumentException 不正なフォーマットのアドレスが指定された場合
337 	 */
338 	public void setFrom(String email, String name) throws IllegalArgumentException {
339 		if (charset.equals(JIS_CHARSET)) {
340 			name = Cp932.toJIS(name);
341 		}
342 		try {
343 			setFrom(new InternetAddress(email, name, charset));
344 		} catch (UnsupportedEncodingException e) {
345 			throw new IllegalArgumentException(e.getMessage());
346 		}
347 	}
348 
349 	/***
350 	 * メールの差出人アドレスを返します。セットされていない場合はnullを返します。
351 	 * 
352 	 * @return メールの差出人アドレス
353 	 */
354 	public InternetAddress getFrom() {
355 		return from;
356 	}
357 
358 	/***
359 	 * Return-Pathアドレスをセットします。
360 	 * 
361 	 * @param address Return-Pathアドレス
362 	 */
363 	public void setReturnPath(InternetAddress address) {
364 		returnPath = address;
365 	}
366 
367 	/***
368 	 * Return-Pathアドレスをセットします。
369 	 * 
370 	 * @param email Return-Pathアドレス
371 	 * @throws IllegalArgumentException 不正なフォーマットのアドレスが指定された場合
372 	 */
373 	public void setReturnPath(String email) throws IllegalArgumentException {
374 		try {
375 			setReturnPath(new InternetAddress(email));
376 		} catch (AddressException e) {
377 			throw new IllegalArgumentException(e.getMessage());
378 		}
379 	}
380 
381 	/***
382 	 * Return-Pathアドレスを返します。
383 	 * 
384 	 * @return Return-Pathアドレス
385 	 */
386 	public InternetAddress getReturnPath() {
387 		return returnPath;
388 	}
389 
390 	/***
391 	 * 返信先アドレスをセットします。
392 	 * 
393 	 * @param address 返信先アドレス
394 	 */
395 	public void setReplyTo(InternetAddress address) {
396 		replyTo = address;
397 	}
398 
399 	/***
400 	 * 返信先アドレスをセットします。
401 	 * 
402 	 * @param email 返信先アドレス
403 	 * @throws IllegalArgumentException 不正なフォーマットのアドレスが指定された場合
404 	 */
405 	public void setReplyTo(String email) throws IllegalArgumentException {
406 		try {
407 			setReplyTo(new InternetAddress(email));
408 		} catch (AddressException e) {
409 			throw new IllegalArgumentException(e.getMessage());
410 		}
411 	}
412 
413 	/***
414 	 * メールの返信先アドレスを返します。セットされていない場合はnullを返します。
415 	 * 
416 	 * @return 返信先アドレス
417 	 */
418 	public InternetAddress getReplyTo() {
419 		return replyTo;
420 	}
421 
422 	/***
423 	 * メールの件名を返します。セットされていない場合は空文字列を返します。
424 	 * 
425 	 * @return メールの件名
426 	 */
427 	public String getSubject() {
428 		if (subject == null) {
429 			return "";
430 		}
431 		return subject;
432 	}
433 
434 	/***
435 	 * メールの件名をセットします。
436 	 * 
437 	 * @param subject メールの件名
438 	 */
439 	public void setSubject(String subject) {
440 		this.subject = subject;
441 	}
442 
443 	/***
444 	 * メール本文を返します。
445 	 * 本文セットされていない場合は空文字列を返します。
446 	 * 
447 	 * @return メール本文
448 	 */
449 	public String getText() {
450 		if (text == null) {
451 			return "";
452 		}
453 		return text;
454 	}
455 
456 	/***
457 	 * メール本文をセットします。
458 	 * 
459 	 * @param text メール本文
460 	 */
461 	public void setText(String text) {
462 		this.text = text;
463 	}
464 
465 	/***
466 	 * メールヘッダに任意のヘッダフィールドを追加します。
467 	 * 任意ヘッダは「X-key: value」のフォーマットでメールヘッダに組み込まれます。<br>
468 	 * 同じヘッダ名の値は上書きされます。
469 	 *  
470 	 * @param name 任意ヘッダ名。頭が"X-"で始まっていなければ、自動的に付与されます。
471 	 * @param value 任意ヘッダの値
472 	 */
473 	public void addXHeader(String name, String value) {
474 		if (headers == null) {
475 			headers = new HashMap();
476 		}
477 		if (name.startsWith("X-")) {
478 			headers.put(name, value);
479 		} else {
480 			headers.put("X-" + name, value);
481 		}
482 	}
483 
484 	/***
485 	 * メールヘッダに任意のヘッダフィールドを追加します。<br>
486 	 * 同じヘッダ名の値は上書きされます。
487 	 * 
488 	 * @since 1.2
489 	 * @param name 任意ヘッダ名
490 	 * @param value 任意ヘッダの値
491 	 */
492 	public void addHeader(String name, String value) {
493 		if (headers == null) {
494 			headers = new HashMap();
495 		}
496 		headers.put(name, value);
497 	}
498 
499 	/***
500 	 * メールの任意ヘッダ名と値のMapインスタンスを返します。
501 	 * 任意ヘッダが一件もセットされていないときはnullを返します。
502 	 * <p>
503 	 * このMapインスタンスへの修正はできません。(unmodifiableMapになっています。)
504 	 * 
505 	 * @return メールの任意ヘッダ名と値のMapインスタンス。またはnull。
506 	 */
507 	public Map getHeaders() {
508 		if (headers == null) {
509 			return null;
510 		}
511 		return Collections.unmodifiableMap(headers);
512 	}
513 
514 	/***
515 	 * メール内容を出力します。<br>
516 	 * メールのソースに似たフォーマットで出力されます。
517 	 * 
518 	 * @see java.lang.Object#toString()
519 	 */
520 	public String toString() {
521 		StringBuffer buf = new StringBuffer(1000);
522 		buf.append("Mail\n");
523 		buf.append("Return-Path: ").append(returnPath).append("\n");
524 		buf.append("From: ").append(from != null ? from.toUnicodeString() : null).append("\n");
525 		buf.append("To: ").append(arrayToCommaDelimitedString(to)).append("\n");
526 		buf.append("Cc: ").append(arrayToCommaDelimitedString(cc)).append("\n");
527 		buf.append("Bcc: ").append(arrayToCommaDelimitedString(bcc)).append("\n");
528 		buf.append("Subject: ").append(subject).append("\n");
529 
530 		if (headers != null) {
531 			for (Iterator itr = headers.keySet().iterator(); itr.hasNext();) {
532 				String header = (String)itr.next();
533 				String value = (String)headers.get(header);
534 				buf.append(header).append(": ").append(value).append("\n");
535 			}
536 		}
537 
538 		buf.append("\n");
539 		buf.append(text);
540 
541 		if (htmlText != null) {
542 			buf.append("\n\n-----\n\n");
543 			buf.append(htmlText);
544 		}
545 
546 		return buf.toString();
547 	}
548 
549 	/***
550 	 * @param list
551 	 * @return 
552 	 */
553 	protected String arrayToCommaDelimitedString(List list) {
554 		if (list == null) {
555 			return "null";
556 		} else {
557 			StringBuffer sb = new StringBuffer();
558 			for (int i = 0, num = list.size(); i < num; i++) {
559 				if (i > 0) {
560 					sb.append(", ");
561 				}
562 				sb.append(((InternetAddress)list.get(i)).toUnicodeString());
563 			}
564 			return sb.toString();
565 		}
566 	}
567 
568 	/***
569 	 * セットされている送信先アドレス(Toアドレス)を全てクリアします。
570 	 *
571 	 * @since 1.0.2
572 	 */
573 	public void clearTo() {
574 		to = null;
575 	}
576 
577 	/***
578 	 * セットされているCCアドレスを全てクリアします。
579 	 *
580 	 * @since 1.0.2
581 	 */
582 	public void clearCc() {
583 		cc = null;
584 	}
585 
586 	/***
587 	 * セットされているBCCアドレスを全てクリアします。
588 	 *
589 	 * @since 1.0.2
590 	 */
591 	public void clearBcc() {
592 		bcc = null;
593 	}
594 
595 	/***
596 	 * HTMLの本文をセットします。
597 	 * 
598 	 * @since 1.1
599 	 * 
600 	 * @param htmlText HTMLの本文
601 	 */
602 	public void setHtmlText(String htmlText) {
603 		this.htmlText = htmlText;
604 	}
605 
606 	/***
607 	 * HTMLの本文を返します。
608 	 * 
609 	 * @since 1.1
610 	 * 
611 	 * @return HTMLの本文。またはnull。
612 	 */
613 	public String getHtmlText() {
614 		return htmlText;
615 	}
616 
617 	/***
618 	 * 指定されたファイルを添付します。
619 	 * 添付ファイル名には、指定されたファイルの名前が使用されます。
620 	 * このファイルの名前は適切な拡張子が付けられている必要があります。
621 	 * 
622 	 * @since 1.1
623 	 * 
624 	 * @param file 添付ファイル
625 	 */
626 	public void addFile(File file) {
627 		if (attachmentFiles == null) {
628 			initAttachmentFiles();
629 		}
630 		addFile(file, file.getName());
631 	}
632 
633 	/***
634 	 * 指定されたファイルを添付します。
635 	 * 指定するファイル名には適切な拡張子が付けられている必要があります。
636 	 * 
637 	 * @since 1.1
638 	 * 
639 	 * @param file 添付ファイル
640 	 * @param fileName ファイル名
641 	 */
642 	public void addFile(File file, String fileName) {
643 		if (attachmentFiles == null) {
644 			initAttachmentFiles();
645 		}
646 		attachmentFiles.add(new AttachmentFile(fileName, file));
647 	}
648 
649 	/***
650 	 * 指定されたURLのファイルを添付します。
651 	 * 指定するファイル名には適切な拡張子が付けられている必要があります。
652 	 * 
653 	 * @since 1.1
654 	 * 
655 	 * @param url 添付ファイル
656 	 * @param fileName ファイル名
657 	 */
658 	public void addFile(URL url, String fileName) {
659 		if (attachmentFiles == null) {
660 			initAttachmentFiles();
661 		}
662 		attachmentFiles.add(new AttachmentFile(fileName, url));
663 	}
664 
665 	/***
666 	 * 指定されたInputStreamをファイルとして添付します。
667 	 * 指定するファイル名には適切な拡張子が付けられている必要があります。
668 	 * 
669 	 * @since 1.1
670 	 * 
671 	 * @param is 添付ファイルを生成するInputStream
672 	 * @param fileName ファイル名
673 	 */
674 	public void addFile(InputStream is, String fileName) {
675 		if (attachmentFiles == null) {
676 			initAttachmentFiles();
677 		}
678 		attachmentFiles.add(new AttachmentFile(fileName, is));
679 	}
680 
681 	/***
682 	 * attachmentFilesプロパティを初期化。
683 	 */
684 	private void initAttachmentFiles() {
685 		attachmentFiles = new ArrayList();
686 	}
687 
688 	/***
689 	 * 添付ファイルの配列を返します。
690 	 * 添付ファイルがセットされていない場合は、空の配列を返します。
691 	 * 
692 	 * @since 1.1
693 	 * 
694 	 * @return 添付ファイルの配列。または空の配列。
695 	 */
696 	public AttachmentFile[] getAttachmentFiles() {
697 		if (attachmentFiles == null) {
698 			return new AttachmentFile[0];
699 		}
700 		return (AttachmentFile[])attachmentFiles
701 				.toArray(new AttachmentFile[attachmentFiles.size()]);
702 	}
703 
704 	/***
705 	 * HTMLの本文がセットされているかどうか判定します。
706 	 * 
707 	 * @since 1.1
708 	 * 
709 	 * @return HTMLの本文がセットされている場合 true
710 	 */
711 	public boolean isHtmlMail() {
712 		return (htmlText != null);
713 	}
714 
715 	/***
716 	 * ファイルが添付されているかどうか判定します。
717 	 * 
718 	 * @since 1.1
719 	 * 
720 	 * @return ファイルが添付されている場合 true
721 	 */
722 	public boolean isFileAttached() {
723 		return attachmentFiles != null && attachmentFiles.size() > 0;
724 	}
725 
726 	/***
727 	 * マルチパート・メールかどうか判定します。<br>
728 	 * HTML本文がセットされているか、ファイルが添付されている場合に true が返されます。
729 	 * <p>
730 	 * 注: ここで判定されるマルチパートは、厳密な意味でのマルチパートではありません。
731 	 * 
732 	 * @since 1.1
733 	 * 
734 	 * @return マルチパート・メールの場合 true
735 	 */
736 	public boolean isMultipartMail() {
737 		return isHtmlMail() || isFileAttached();
738 	}
739 
740 	/***
741 	 * セットされている添付ファイルを全てクリアします。
742 	 * 
743 	 * @since 1.1
744 	 */
745 	public void clearFile() {
746 		initAttachmentFiles();
747 	}
748 
749 	/***
750 	 * envelope-toの宛先アドレスを追加します。
751 	 * <p>
752 	 * envelope-toアドレスがセットされている場合、envelope-toのアドレスにのみメールを送信し、
753 	 * To、Cc、Bccアドレスには実際には送信されません。
754 	 * 
755 	 * @since 1.2
756 	 * @param address
757 	 */
758 	public void addEnvelopeTo(InternetAddress address) {
759 		if (envelopeTo == null) {
760 			envelopeTo = new ArrayList();
761 		}
762 		envelopeTo.add(address);
763 	}
764 
765 	/***
766 	 * envelope-toの宛先アドレスを追加します。
767 	 * <p>
768 	 * envelope-toアドレスがセットされている場合、envelope-toのアドレスにのみメールを送信し、
769 	 * To、Cc、Bccアドレスには実際には送信されません。
770 	 * 
771 	 * @since 1.2
772 	 * @param email
773 	 * @throws IllegalArgumentException 不正なフォーマットのアドレスが指定された場合
774 	 */
775 	public void addEnvelopeTo(String email) {
776 		try {
777 			addEnvelopeTo(new InternetAddress(email));
778 		} catch (AddressException e) {
779 			throw new IllegalArgumentException(e.getMessage());
780 		}
781 	}
782 
783 	/***
784 	 * envelope-toの宛先アドレスを追加します。
785 	 * <p>
786 	 * envelope-toアドレスがセットされている場合、envelope-toのアドレスにのみメールを送信し、
787 	 * To、Cc、Bccアドレスには実際には送信されません。
788 	 * 
789 	 * @since 1.2
790 	 * @param addresses
791 	 */
792 	public void addEnvelopeTo(InternetAddress[] addresses) {
793 		for (int i = 0; i < addresses.length; i++) {
794 			addEnvelopeTo(addresses[i]);
795 		}
796 	}
797 
798 	/***
799 	 * envelope-toの宛先アドレスを追加します。
800 	 * <p>
801 	 * envelope-toアドレスがセットされている場合、envelope-toのアドレスにのみメールを送信し、
802 	 * To、Cc、Bccアドレスには実際には送信されません。
803 	 * 
804 	 * @since 1.2
805 	 * @param emails
806 	 * @throws IllegalArgumentException 不正なフォーマットのアドレスが指定された場合
807 	 */
808 	public void addEnvelopeTo(String[] emails) {
809 		for (int i = 0; i < emails.length; i++) {
810 			addEnvelopeTo(emails[i]);
811 		}
812 	}
813 
814 	/***
815 	 * セットされているenvelope-toアドレスを全てクリアします。
816 	 *
817 	 * @since 1.2
818 	 */
819 	public void clearEnvelopeTo() {
820 		envelopeTo = null;
821 	}
822 
823 	/***
824 	 * envelope-toアドレス配列を返します。
825 	 * envelope-toアドレスが一件もセットされていないときは空の配列を返します。
826 	 * 
827 	 * @since 1.2
828 	 * @return envelope-toアドレスの配列
829 	 */
830 	public InternetAddress[] getEnvelopeTo() {
831 		if (envelopeTo == null) {
832 			return new InternetAddress[0];
833 		}
834 		return (InternetAddress[])envelopeTo.toArray(new InternetAddress[envelopeTo.size()]);
835 	}
836 
837 	/***
838 	 * 添付ファイル。
839 	 * <p>
840 	 * 受信メール(ReceivedMail)の添付ファイルは、常に<code>getFile()</code>メソッドで取得します。
841 	 * <code>getInputStream()</code>、<code>getUrl()</code>メソッドはnullを返します。
842 	 * 受信メールに対しては、<code>ReceivedMail.getFiles()</code>メソッドを使うと添付ファイルの
843 	 * <code>File</code>インスタンス配列を取得することができます。
844 	 * 
845 	 * @since 1.1
846 	 * @author Tomohiro Otsuka
847 	 * @version $Id: Mail.java,v 1.10.2.5 2005/01/23 10:39:11 otsuka Exp $
848 	 */
849 	public class AttachmentFile {
850 
851 		private String name;
852 
853 		private File file;
854 
855 		private InputStream is;
856 
857 		private URL url;
858 
859 		/***
860 		 * ファイル名とファイルを指定して、このクラスのインタンスを生成します。
861 		 * ファイル名には適切な拡張子が付けられている必要があります。
862 		 * 
863 		 * @param name メールに表示するファイル名
864 		 * @param file 添付ファイル
865 		 */
866 		public AttachmentFile(String name, File file) {
867 			this.name = name;
868 			this.file = file;
869 		}
870 
871 		/***
872 		 * ファイル名とInputStreamを指定して、このクラスのインタンスを生成します。
873 		 * ファイル名には適切な拡張子が付けられている必要があります。
874 		 * 
875 		 * @param name メールに表示するファイル名
876 		 * @param is 添付ファイルを生成するInputStream
877 		 */
878 		public AttachmentFile(String name, InputStream is) {
879 			this.name = name;
880 			this.is = is;
881 		}
882 
883 		/***
884 		 * ファイル名とファイルロケーションのURLを指定して、このクラスのインタンスを生成します。
885 		 * ファイル名には適切な拡張子が付けられている必要があります。
886 		 * 
887 		 * @param name メールに表示するファイル名
888 		 * @param url 添付ファイルのロケーションURL
889 		 */
890 		public AttachmentFile(String name, URL url) {
891 			this.name = name;
892 			this.url = url;
893 		}
894 
895 		/***
896 		 * 添付ファイルのDataSourceインスタンスを生成して返します。
897 		 * 
898 		 * @return 添付ファイルのDataSourceインスタンス
899 		 */
900 		public DataSource getDataSource() {
901 			if (file != null) {
902 				return new FileDataSource(file);
903 			}
904 
905 			if (url != null) {
906 				return new URLDataSource(url);
907 			}
908 
909 			// InputStreamからDataSourceを生成
910 			String contentType = FileTypeMap.getDefaultFileTypeMap().getContentType(name);
911 			return new ByteArrayDataSource(is, contentType);
912 		}
913 
914 		/***
915 		 * 添付ファイル名を返します。
916 		 * 
917 		 * @return 添付ファイル名
918 		 */
919 		public String getName() {
920 			return name;
921 		}
922 
923 		/***
924 		 * @return セットされたファイル。またはnull。
925 		 */
926 		public File getFile() {
927 			return file;
928 		}
929 
930 		/***
931 		 * @return セットされたInputStream。またはnull。
932 		 */
933 		public InputStream getInputStream() {
934 			return is;
935 		}
936 
937 		/***
938 		 * @return セットされたURL。またはnull。
939 		 */
940 		public URL getUrl() {
941 			return url;
942 		}
943 	}
944 
945 	/***
946 	 * メールの重要度。定数のみを定義。
947 	 * 
948 	 * @author Tomohiro Otsuka
949 	 * @version $Id: Mail.java,v 1.10.2.5 2005/01/23 10:39:11 otsuka Exp $
950 	 */
951 	public static class Importance {
952 
953 		/*** 重要度「高」 */
954 		public static final String HIGH = "high";
955 
956 		/*** 重要度「中」 */
957 		public static final String NORMAL = "normal";
958 
959 		/*** 重要度「低」 */
960 		public static final String LOW = "low";
961 
962 	}
963 }