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.8 2006/05/06 04:45:27 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 * <b>このメソッドはユーザが使用することを想定していません。</b>
487 * 使用する際は、To や From などのフィールドをセットしないように注意してください。
488 * <p>
489 * このメソッドで設定した同じヘッダ名の値は上書きされます。
490 *
491 * @since 1.2
492 * @param name 任意ヘッダ名
493 * @param value 任意ヘッダの値
494 */
495 public void addHeader(String name, String value) {
496 if (headers == null) {
497 headers = new HashMap();
498 }
499 headers.put(name, value);
500 }
501
502 /***
503 * メールの任意ヘッダ名と値のMapインスタンスを返します。
504 * 任意ヘッダが一件もセットされていないときはnullを返します。
505 * <p>
506 * このMapインスタンスへの修正はできません。(unmodifiableMapになっています。)
507 *
508 * @return メールの任意ヘッダ名と値のMapインスタンス。またはnull。
509 */
510 public Map getHeaders() {
511 if (headers == null) {
512 return null;
513 }
514 return Collections.unmodifiableMap(headers);
515 }
516
517 /***
518 * メール内容を出力します。<br>
519 * メールのソースに似たフォーマットで出力されます。
520 *
521 * @see java.lang.Object#toString()
522 */
523 public String toString() {
524 StringBuffer buf = new StringBuffer(1000);
525 buf.append("Mail\n");
526 buf.append("Return-Path: ").append(returnPath).append("\n");
527 buf.append("From: ").append(from != null ? from.toUnicodeString() : null).append("\n");
528 buf.append("To: ").append(arrayToCommaDelimitedString(to)).append("\n");
529 buf.append("Cc: ").append(arrayToCommaDelimitedString(cc)).append("\n");
530 buf.append("Bcc: ").append(arrayToCommaDelimitedString(bcc)).append("\n");
531 buf.append("Subject: ").append(subject).append("\n");
532
533 if (headers != null) {
534 for (Iterator itr = headers.keySet().iterator(); itr.hasNext();) {
535 String header = (String)itr.next();
536 String value = (String)headers.get(header);
537 buf.append(header).append(": ").append(value).append("\n");
538 }
539 }
540
541 buf.append("\n");
542 buf.append(text);
543
544 if (htmlText != null) {
545 buf.append("\n\n-----\n\n");
546 buf.append(htmlText);
547 }
548
549 return buf.toString();
550 }
551
552 /***
553 * 指定されたリストの要素をコンマ区切りの文字列に変換します。
554 * nullが指定された場合は「null」文字列を返します。
555 *
556 * @param list
557 * @return リスト要素のコンマ区切り文字列
558 */
559 protected String arrayToCommaDelimitedString(List list) {
560 if (list == null) {
561 return "null";
562 } else {
563 StringBuffer sb = new StringBuffer();
564 for (int i = 0, num = list.size(); i < num; i++) {
565 if (i > 0) {
566 sb.append(", ");
567 }
568 sb.append(((InternetAddress)list.get(i)).toUnicodeString());
569 }
570 return sb.toString();
571 }
572 }
573
574 /***
575 * セットされている送信先アドレス(Toアドレス)を全てクリアします。
576 *
577 * @since 1.0.2
578 */
579 public void clearTo() {
580 to = null;
581 }
582
583 /***
584 * セットされているCCアドレスを全てクリアします。
585 *
586 * @since 1.0.2
587 */
588 public void clearCc() {
589 cc = null;
590 }
591
592 /***
593 * セットされているBCCアドレスを全てクリアします。
594 *
595 * @since 1.0.2
596 */
597 public void clearBcc() {
598 bcc = null;
599 }
600
601 /***
602 * HTMLの本文をセットします。
603 *
604 * @since 1.1
605 *
606 * @param htmlText HTMLの本文
607 */
608 public void setHtmlText(String htmlText) {
609 this.htmlText = htmlText;
610 }
611
612 /***
613 * HTMLの本文を返します。
614 *
615 * @since 1.1
616 *
617 * @return HTMLの本文。またはnull。
618 */
619 public String getHtmlText() {
620 return htmlText;
621 }
622
623 /***
624 * 指定されたファイルを添付します。
625 * 添付ファイル名には、指定されたファイルの名前が使用されます。
626 * このファイルの名前は適切な拡張子が付けられている必要があります。
627 *
628 * @since 1.1
629 *
630 * @param file 添付ファイル
631 */
632 public void addFile(File file) {
633 if (attachmentFiles == null) {
634 initAttachmentFiles();
635 }
636 addFile(file, file.getName());
637 }
638
639 /***
640 * 指定されたファイルを添付します。
641 * 指定するファイル名には適切な拡張子が付けられている必要があります。
642 *
643 * @since 1.1
644 *
645 * @param file 添付ファイル
646 * @param fileName ファイル名
647 */
648 public void addFile(File file, String fileName) {
649 if (attachmentFiles == null) {
650 initAttachmentFiles();
651 }
652 attachmentFiles.add(new AttachmentFile(fileName, file));
653 }
654
655 /***
656 * 指定されたURLのファイルを添付します。
657 * 指定するファイル名には適切な拡張子が付けられている必要があります。
658 *
659 * @since 1.1
660 *
661 * @param url 添付ファイル
662 * @param fileName ファイル名
663 */
664 public void addFile(URL url, String fileName) {
665 if (attachmentFiles == null) {
666 initAttachmentFiles();
667 }
668 attachmentFiles.add(new AttachmentFile(fileName, url));
669 }
670
671 /***
672 * 指定されたInputStreamをファイルとして添付します。
673 * 指定するファイル名には適切な拡張子が付けられている必要があります。
674 *
675 * @since 1.1
676 *
677 * @param is 添付ファイルを生成するInputStream
678 * @param fileName ファイル名
679 */
680 public void addFile(InputStream is, String fileName) {
681 if (attachmentFiles == null) {
682 initAttachmentFiles();
683 }
684 attachmentFiles.add(new AttachmentFile(fileName, is));
685 }
686
687 /***
688 * 指定されたbyte配列をファイルとして添付します。
689 * 指定するファイル名には適切な拡張子が付けられている必要があります。
690 *
691 * @since 1.2
692 *
693 * @param bytes 添付ファイルを生成するbyte配列
694 * @param fileName ファイル名
695 */
696 public void addFile(byte[] bytes, String fileName) {
697 if (attachmentFiles == null) {
698 initAttachmentFiles();
699 }
700 attachmentFiles.add(new AttachmentFile(fileName, bytes));
701 }
702
703 /***
704 * attachmentFilesプロパティを初期化。
705 */
706 private void initAttachmentFiles() {
707 attachmentFiles = new ArrayList();
708 }
709
710 /***
711 * 添付ファイルの配列を返します。
712 * 添付ファイルがセットされていない場合は、空の配列を返します。
713 *
714 * @since 1.1
715 *
716 * @return 添付ファイルの配列。または空の配列。
717 */
718 public AttachmentFile[] getAttachmentFiles() {
719 if (attachmentFiles == null) {
720 return new AttachmentFile[0];
721 }
722 return (AttachmentFile[])attachmentFiles
723 .toArray(new AttachmentFile[attachmentFiles.size()]);
724 }
725
726 /***
727 * HTMLの本文がセットされているかどうか判定します。
728 *
729 * @since 1.1
730 *
731 * @return HTMLの本文がセットされている場合 true
732 */
733 public boolean isHtmlMail() {
734 return (htmlText != null);
735 }
736
737 /***
738 * ファイルが添付されているかどうか判定します。
739 *
740 * @since 1.1
741 *
742 * @return ファイルが添付されている場合 true
743 */
744 public boolean isFileAttached() {
745 return attachmentFiles != null && attachmentFiles.size() > 0;
746 }
747
748 /***
749 * マルチパート・メールかどうか判定します。<br>
750 * HTML本文がセットされているか、ファイルが添付されている場合に true が返されます。
751 * <p>
752 * 注: ここで判定されるマルチパートは、厳密な意味でのマルチパートではありません。
753 *
754 * @since 1.1
755 *
756 * @return マルチパート・メールの場合 true
757 */
758 public boolean isMultipartMail() {
759 return isHtmlMail() || isFileAttached();
760 }
761
762 /***
763 * セットされている添付ファイルを全てクリアします。
764 *
765 * @since 1.1
766 */
767 public void clearFile() {
768 initAttachmentFiles();
769 }
770
771 /***
772 * envelope-toの宛先アドレスを追加します。
773 * <p>
774 * envelope-toアドレスがセットされている場合、envelope-toのアドレスにのみメールを送信し、
775 * To、Cc、Bccアドレスには実際には送信されません。
776 *
777 * @since 1.2
778 * @param address
779 */
780 public void addEnvelopeTo(InternetAddress address) {
781 if (envelopeTo == null) {
782 envelopeTo = new ArrayList();
783 }
784 envelopeTo.add(address);
785 }
786
787 /***
788 * envelope-toの宛先アドレスを追加します。
789 * <p>
790 * envelope-toアドレスがセットされている場合、envelope-toのアドレスにのみメールを送信し、
791 * To、Cc、Bccアドレスには実際には送信されません。
792 *
793 * @since 1.2
794 * @param email
795 * @throws IllegalArgumentException 不正なフォーマットのアドレスが指定された場合
796 */
797 public void addEnvelopeTo(String email) {
798 try {
799 addEnvelopeTo(new InternetAddress(email));
800 } catch (AddressException e) {
801 throw new IllegalArgumentException(e.getMessage());
802 }
803 }
804
805 /***
806 * envelope-toの宛先アドレスを追加します。
807 * <p>
808 * envelope-toアドレスがセットされている場合、envelope-toのアドレスにのみメールを送信し、
809 * To、Cc、Bccアドレスには実際には送信されません。
810 *
811 * @since 1.2
812 * @param addresses
813 */
814 public void addEnvelopeTo(InternetAddress[] addresses) {
815 for (int i = 0; i < addresses.length; i++) {
816 addEnvelopeTo(addresses[i]);
817 }
818 }
819
820 /***
821 * envelope-toの宛先アドレスを追加します。
822 * <p>
823 * envelope-toアドレスがセットされている場合、envelope-toのアドレスにのみメールを送信し、
824 * To、Cc、Bccアドレスには実際には送信されません。
825 *
826 * @since 1.2
827 * @param emails
828 * @throws IllegalArgumentException 不正なフォーマットのアドレスが指定された場合
829 */
830 public void addEnvelopeTo(String[] emails) {
831 for (int i = 0; i < emails.length; i++) {
832 addEnvelopeTo(emails[i]);
833 }
834 }
835
836 /***
837 * セットされているenvelope-toアドレスを全てクリアします。
838 *
839 * @since 1.2
840 */
841 public void clearEnvelopeTo() {
842 envelopeTo = null;
843 }
844
845 /***
846 * envelope-toアドレス配列を返します。
847 * envelope-toアドレスが一件もセットされていないときは空の配列を返します。
848 *
849 * @since 1.2
850 * @return envelope-toアドレスの配列
851 */
852 public InternetAddress[] getEnvelopeTo() {
853 if (envelopeTo == null) {
854 return new InternetAddress[0];
855 }
856 return (InternetAddress[])envelopeTo.toArray(new InternetAddress[envelopeTo.size()]);
857 }
858
859 /***
860 * 添付ファイル。
861 * <p>
862 * 受信メール(ReceivedMail)の添付ファイルは、常に<code>getFile()</code>メソッドで取得します。
863 * <code>getInputStream()</code>、<code>getUrl()</code>メソッドはnullを返します。
864 * 受信メールに対しては、<code>ReceivedMail.getFiles()</code>メソッドを使うと添付ファイルの
865 * <code>File</code>インスタンス配列を取得することができます。
866 *
867 * @since 1.1
868 * @author Tomohiro Otsuka
869 * @version $Id: Mail.java,v 1.10.2.8 2006/05/06 04:45:27 otsuka Exp $
870 */
871 public class AttachmentFile {
872
873 private String name;
874
875 private File file;
876
877 private InputStream is;
878
879 private URL url;
880
881 private byte[] bytes = null;
882
883 /***
884 * ファイル名とファイルを指定して、このクラスのインタンスを生成します。
885 * ファイル名には適切な拡張子が付けられている必要があります。
886 *
887 * @param name メールに表示するファイル名
888 * @param file 添付ファイル
889 */
890 public AttachmentFile(String name, File file) {
891 this.name = name;
892 this.file = file;
893 }
894
895 /***
896 * ファイル名とInputStreamを指定して、このクラスのインタンスを生成します。
897 * ファイル名には適切な拡張子が付けられている必要があります。
898 *
899 * @param name メールに表示するファイル名
900 * @param is 添付ファイルを生成するInputStream
901 */
902 public AttachmentFile(String name, InputStream is) {
903 this.name = name;
904 this.is = is;
905 }
906
907 /***
908 * ファイル名とファイルロケーションのURLを指定して、このクラスのインタンスを生成します。
909 * ファイル名には適切な拡張子が付けられている必要があります。
910 *
911 * @param name メールに表示するファイル名
912 * @param url 添付ファイルのロケーションURL
913 */
914 public AttachmentFile(String name, URL url) {
915 this.name = name;
916 this.url = url;
917 }
918
919 /***
920 * ファイル名とbyte配列を指定して、このクラスのインタンスを生成します。
921 * ファイル名には適切な拡張子が付けられている必要があります。
922 *
923 * @param name メールに表示するファイル名
924 * @param bytes 添付ファイルを生成するbyte配列
925 */
926 public AttachmentFile(String name, byte[] bytes) {
927 this.name = name;
928 this.bytes = bytes;
929 }
930
931 /***
932 * 添付ファイルのDataSourceインスタンスを生成して返します。
933 *
934 * @return 添付ファイルのDataSourceインスタンス
935 */
936 public DataSource getDataSource() {
937 if (file != null) {
938 return new FileDataSource(file);
939 }
940
941 if (url != null) {
942 return new URLDataSource(url);
943 }
944
945
946 String contentType = FileTypeMap.getDefaultFileTypeMap().getContentType(name);
947 if (is != null) {
948
949 return new ByteArrayDataSource(is, contentType);
950 } else {
951
952 return new ByteArrayDataSource(bytes, contentType);
953 }
954 }
955
956 /***
957 * 添付ファイル名を返します。
958 *
959 * @return 添付ファイル名
960 */
961 public String getName() {
962 return name;
963 }
964
965 /***
966 * @return セットされたファイル。またはnull。
967 */
968 public File getFile() {
969 return file;
970 }
971
972 /***
973 * @return セットされたInputStream。またはnull。
974 */
975 public InputStream getInputStream() {
976 return is;
977 }
978
979 /***
980 * @return セットされたURL。またはnull。
981 */
982 public URL getUrl() {
983 return url;
984 }
985
986 /***
987 * @return セットされたbyte配列。またはnull。
988 */
989 public byte[] getBytes() {
990 return bytes;
991 }
992 }
993
994 /***
995 * メールの重要度。定数のみを定義。
996 *
997 * @author Tomohiro Otsuka
998 * @version $Id: Mail.java,v 1.10.2.8 2006/05/06 04:45:27 otsuka Exp $
999 */
1000 public static class Importance {
1001
1002 /*** 重要度「高」 */
1003 public static final String HIGH = "high";
1004
1005 /*** 重要度「中」 */
1006 public static final String NORMAL = "normal";
1007
1008 /*** 重要度「低」 */
1009 public static final String LOW = "low";
1010
1011 }
1012 }