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.6 2005/02/05 09:25:25 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 * 指定されたリストの要素をコンマ区切りの文字列に変換します。
551 * nullが指定された場合は「null」文字列を返します。
552 *
553 * @param list
554 * @return リスト要素のコンマ区切り文字列
555 */
556 protected String arrayToCommaDelimitedString(List list) {
557 if (list == null) {
558 return "null";
559 } else {
560 StringBuffer sb = new StringBuffer();
561 for (int i = 0, num = list.size(); i < num; i++) {
562 if (i > 0) {
563 sb.append(", ");
564 }
565 sb.append(((InternetAddress)list.get(i)).toUnicodeString());
566 }
567 return sb.toString();
568 }
569 }
570
571 /***
572 * セットされている送信先アドレス(Toアドレス)を全てクリアします。
573 *
574 * @since 1.0.2
575 */
576 public void clearTo() {
577 to = null;
578 }
579
580 /***
581 * セットされているCCアドレスを全てクリアします。
582 *
583 * @since 1.0.2
584 */
585 public void clearCc() {
586 cc = null;
587 }
588
589 /***
590 * セットされているBCCアドレスを全てクリアします。
591 *
592 * @since 1.0.2
593 */
594 public void clearBcc() {
595 bcc = null;
596 }
597
598 /***
599 * HTMLの本文をセットします。
600 *
601 * @since 1.1
602 *
603 * @param htmlText HTMLの本文
604 */
605 public void setHtmlText(String htmlText) {
606 this.htmlText = htmlText;
607 }
608
609 /***
610 * HTMLの本文を返します。
611 *
612 * @since 1.1
613 *
614 * @return HTMLの本文。またはnull。
615 */
616 public String getHtmlText() {
617 return htmlText;
618 }
619
620 /***
621 * 指定されたファイルを添付します。
622 * 添付ファイル名には、指定されたファイルの名前が使用されます。
623 * このファイルの名前は適切な拡張子が付けられている必要があります。
624 *
625 * @since 1.1
626 *
627 * @param file 添付ファイル
628 */
629 public void addFile(File file) {
630 if (attachmentFiles == null) {
631 initAttachmentFiles();
632 }
633 addFile(file, file.getName());
634 }
635
636 /***
637 * 指定されたファイルを添付します。
638 * 指定するファイル名には適切な拡張子が付けられている必要があります。
639 *
640 * @since 1.1
641 *
642 * @param file 添付ファイル
643 * @param fileName ファイル名
644 */
645 public void addFile(File file, String fileName) {
646 if (attachmentFiles == null) {
647 initAttachmentFiles();
648 }
649 attachmentFiles.add(new AttachmentFile(fileName, file));
650 }
651
652 /***
653 * 指定されたURLのファイルを添付します。
654 * 指定するファイル名には適切な拡張子が付けられている必要があります。
655 *
656 * @since 1.1
657 *
658 * @param url 添付ファイル
659 * @param fileName ファイル名
660 */
661 public void addFile(URL url, String fileName) {
662 if (attachmentFiles == null) {
663 initAttachmentFiles();
664 }
665 attachmentFiles.add(new AttachmentFile(fileName, url));
666 }
667
668 /***
669 * 指定されたInputStreamをファイルとして添付します。
670 * 指定するファイル名には適切な拡張子が付けられている必要があります。
671 *
672 * @since 1.1
673 *
674 * @param is 添付ファイルを生成するInputStream
675 * @param fileName ファイル名
676 */
677 public void addFile(InputStream is, String fileName) {
678 if (attachmentFiles == null) {
679 initAttachmentFiles();
680 }
681 attachmentFiles.add(new AttachmentFile(fileName, is));
682 }
683
684 /***
685 * attachmentFilesプロパティを初期化。
686 */
687 private void initAttachmentFiles() {
688 attachmentFiles = new ArrayList();
689 }
690
691 /***
692 * 添付ファイルの配列を返します。
693 * 添付ファイルがセットされていない場合は、空の配列を返します。
694 *
695 * @since 1.1
696 *
697 * @return 添付ファイルの配列。または空の配列。
698 */
699 public AttachmentFile[] getAttachmentFiles() {
700 if (attachmentFiles == null) {
701 return new AttachmentFile[0];
702 }
703 return (AttachmentFile[])attachmentFiles
704 .toArray(new AttachmentFile[attachmentFiles.size()]);
705 }
706
707 /***
708 * HTMLの本文がセットされているかどうか判定します。
709 *
710 * @since 1.1
711 *
712 * @return HTMLの本文がセットされている場合 true
713 */
714 public boolean isHtmlMail() {
715 return (htmlText != null);
716 }
717
718 /***
719 * ファイルが添付されているかどうか判定します。
720 *
721 * @since 1.1
722 *
723 * @return ファイルが添付されている場合 true
724 */
725 public boolean isFileAttached() {
726 return attachmentFiles != null && attachmentFiles.size() > 0;
727 }
728
729 /***
730 * マルチパート・メールかどうか判定します。<br>
731 * HTML本文がセットされているか、ファイルが添付されている場合に true が返されます。
732 * <p>
733 * 注: ここで判定されるマルチパートは、厳密な意味でのマルチパートではありません。
734 *
735 * @since 1.1
736 *
737 * @return マルチパート・メールの場合 true
738 */
739 public boolean isMultipartMail() {
740 return isHtmlMail() || isFileAttached();
741 }
742
743 /***
744 * セットされている添付ファイルを全てクリアします。
745 *
746 * @since 1.1
747 */
748 public void clearFile() {
749 initAttachmentFiles();
750 }
751
752 /***
753 * envelope-toの宛先アドレスを追加します。
754 * <p>
755 * envelope-toアドレスがセットされている場合、envelope-toのアドレスにのみメールを送信し、
756 * To、Cc、Bccアドレスには実際には送信されません。
757 *
758 * @since 1.2
759 * @param address
760 */
761 public void addEnvelopeTo(InternetAddress address) {
762 if (envelopeTo == null) {
763 envelopeTo = new ArrayList();
764 }
765 envelopeTo.add(address);
766 }
767
768 /***
769 * envelope-toの宛先アドレスを追加します。
770 * <p>
771 * envelope-toアドレスがセットされている場合、envelope-toのアドレスにのみメールを送信し、
772 * To、Cc、Bccアドレスには実際には送信されません。
773 *
774 * @since 1.2
775 * @param email
776 * @throws IllegalArgumentException 不正なフォーマットのアドレスが指定された場合
777 */
778 public void addEnvelopeTo(String email) {
779 try {
780 addEnvelopeTo(new InternetAddress(email));
781 } catch (AddressException e) {
782 throw new IllegalArgumentException(e.getMessage());
783 }
784 }
785
786 /***
787 * envelope-toの宛先アドレスを追加します。
788 * <p>
789 * envelope-toアドレスがセットされている場合、envelope-toのアドレスにのみメールを送信し、
790 * To、Cc、Bccアドレスには実際には送信されません。
791 *
792 * @since 1.2
793 * @param addresses
794 */
795 public void addEnvelopeTo(InternetAddress[] addresses) {
796 for (int i = 0; i < addresses.length; i++) {
797 addEnvelopeTo(addresses[i]);
798 }
799 }
800
801 /***
802 * envelope-toの宛先アドレスを追加します。
803 * <p>
804 * envelope-toアドレスがセットされている場合、envelope-toのアドレスにのみメールを送信し、
805 * To、Cc、Bccアドレスには実際には送信されません。
806 *
807 * @since 1.2
808 * @param emails
809 * @throws IllegalArgumentException 不正なフォーマットのアドレスが指定された場合
810 */
811 public void addEnvelopeTo(String[] emails) {
812 for (int i = 0; i < emails.length; i++) {
813 addEnvelopeTo(emails[i]);
814 }
815 }
816
817 /***
818 * セットされているenvelope-toアドレスを全てクリアします。
819 *
820 * @since 1.2
821 */
822 public void clearEnvelopeTo() {
823 envelopeTo = null;
824 }
825
826 /***
827 * envelope-toアドレス配列を返します。
828 * envelope-toアドレスが一件もセットされていないときは空の配列を返します。
829 *
830 * @since 1.2
831 * @return envelope-toアドレスの配列
832 */
833 public InternetAddress[] getEnvelopeTo() {
834 if (envelopeTo == null) {
835 return new InternetAddress[0];
836 }
837 return (InternetAddress[])envelopeTo.toArray(new InternetAddress[envelopeTo.size()]);
838 }
839
840 /***
841 * 添付ファイル。
842 * <p>
843 * 受信メール(ReceivedMail)の添付ファイルは、常に<code>getFile()</code>メソッドで取得します。
844 * <code>getInputStream()</code>、<code>getUrl()</code>メソッドはnullを返します。
845 * 受信メールに対しては、<code>ReceivedMail.getFiles()</code>メソッドを使うと添付ファイルの
846 * <code>File</code>インスタンス配列を取得することができます。
847 *
848 * @since 1.1
849 * @author Tomohiro Otsuka
850 * @version $Id: Mail.java,v 1.10.2.6 2005/02/05 09:25:25 otsuka Exp $
851 */
852 public class AttachmentFile {
853
854 private String name;
855
856 private File file;
857
858 private InputStream is;
859
860 private URL url;
861
862 /***
863 * ファイル名とファイルを指定して、このクラスのインタンスを生成します。
864 * ファイル名には適切な拡張子が付けられている必要があります。
865 *
866 * @param name メールに表示するファイル名
867 * @param file 添付ファイル
868 */
869 public AttachmentFile(String name, File file) {
870 this.name = name;
871 this.file = file;
872 }
873
874 /***
875 * ファイル名とInputStreamを指定して、このクラスのインタンスを生成します。
876 * ファイル名には適切な拡張子が付けられている必要があります。
877 *
878 * @param name メールに表示するファイル名
879 * @param is 添付ファイルを生成するInputStream
880 */
881 public AttachmentFile(String name, InputStream is) {
882 this.name = name;
883 this.is = is;
884 }
885
886 /***
887 * ファイル名とファイルロケーションのURLを指定して、このクラスのインタンスを生成します。
888 * ファイル名には適切な拡張子が付けられている必要があります。
889 *
890 * @param name メールに表示するファイル名
891 * @param url 添付ファイルのロケーションURL
892 */
893 public AttachmentFile(String name, URL url) {
894 this.name = name;
895 this.url = url;
896 }
897
898 /***
899 * 添付ファイルのDataSourceインスタンスを生成して返します。
900 *
901 * @return 添付ファイルのDataSourceインスタンス
902 */
903 public DataSource getDataSource() {
904 if (file != null) {
905 return new FileDataSource(file);
906 }
907
908 if (url != null) {
909 return new URLDataSource(url);
910 }
911
912
913 String contentType = FileTypeMap.getDefaultFileTypeMap().getContentType(name);
914 return new ByteArrayDataSource(is, contentType);
915 }
916
917 /***
918 * 添付ファイル名を返します。
919 *
920 * @return 添付ファイル名
921 */
922 public String getName() {
923 return name;
924 }
925
926 /***
927 * @return セットされたファイル。またはnull。
928 */
929 public File getFile() {
930 return file;
931 }
932
933 /***
934 * @return セットされたInputStream。またはnull。
935 */
936 public InputStream getInputStream() {
937 return is;
938 }
939
940 /***
941 * @return セットされたURL。またはnull。
942 */
943 public URL getUrl() {
944 return url;
945 }
946 }
947
948 /***
949 * メールの重要度。定数のみを定義。
950 *
951 * @author Tomohiro Otsuka
952 * @version $Id: Mail.java,v 1.10.2.6 2005/02/05 09:25:25 otsuka Exp $
953 */
954 public static class Importance {
955
956 /*** 重要度「高」 */
957 public static final String HIGH = "high";
958
959 /*** 重要度「中」 */
960 public static final String NORMAL = "normal";
961
962 /*** 重要度「低」 */
963 public static final String LOW = "low";
964
965 }
966 }