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.7 2005/05/11 10:06:18 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 * attachmentFilesプロパティを初期化。
689 */
690 private void initAttachmentFiles() {
691 attachmentFiles = new ArrayList();
692 }
693
694 /***
695 * 添付ファイルの配列を返します。
696 * 添付ファイルがセットされていない場合は、空の配列を返します。
697 *
698 * @since 1.1
699 *
700 * @return 添付ファイルの配列。または空の配列。
701 */
702 public AttachmentFile[] getAttachmentFiles() {
703 if (attachmentFiles == null) {
704 return new AttachmentFile[0];
705 }
706 return (AttachmentFile[])attachmentFiles
707 .toArray(new AttachmentFile[attachmentFiles.size()]);
708 }
709
710 /***
711 * HTMLの本文がセットされているかどうか判定します。
712 *
713 * @since 1.1
714 *
715 * @return HTMLの本文がセットされている場合 true
716 */
717 public boolean isHtmlMail() {
718 return (htmlText != null);
719 }
720
721 /***
722 * ファイルが添付されているかどうか判定します。
723 *
724 * @since 1.1
725 *
726 * @return ファイルが添付されている場合 true
727 */
728 public boolean isFileAttached() {
729 return attachmentFiles != null && attachmentFiles.size() > 0;
730 }
731
732 /***
733 * マルチパート・メールかどうか判定します。<br>
734 * HTML本文がセットされているか、ファイルが添付されている場合に true が返されます。
735 * <p>
736 * 注: ここで判定されるマルチパートは、厳密な意味でのマルチパートではありません。
737 *
738 * @since 1.1
739 *
740 * @return マルチパート・メールの場合 true
741 */
742 public boolean isMultipartMail() {
743 return isHtmlMail() || isFileAttached();
744 }
745
746 /***
747 * セットされている添付ファイルを全てクリアします。
748 *
749 * @since 1.1
750 */
751 public void clearFile() {
752 initAttachmentFiles();
753 }
754
755 /***
756 * envelope-toの宛先アドレスを追加します。
757 * <p>
758 * envelope-toアドレスがセットされている場合、envelope-toのアドレスにのみメールを送信し、
759 * To、Cc、Bccアドレスには実際には送信されません。
760 *
761 * @since 1.2
762 * @param address
763 */
764 public void addEnvelopeTo(InternetAddress address) {
765 if (envelopeTo == null) {
766 envelopeTo = new ArrayList();
767 }
768 envelopeTo.add(address);
769 }
770
771 /***
772 * envelope-toの宛先アドレスを追加します。
773 * <p>
774 * envelope-toアドレスがセットされている場合、envelope-toのアドレスにのみメールを送信し、
775 * To、Cc、Bccアドレスには実際には送信されません。
776 *
777 * @since 1.2
778 * @param email
779 * @throws IllegalArgumentException 不正なフォーマットのアドレスが指定された場合
780 */
781 public void addEnvelopeTo(String email) {
782 try {
783 addEnvelopeTo(new InternetAddress(email));
784 } catch (AddressException e) {
785 throw new IllegalArgumentException(e.getMessage());
786 }
787 }
788
789 /***
790 * envelope-toの宛先アドレスを追加します。
791 * <p>
792 * envelope-toアドレスがセットされている場合、envelope-toのアドレスにのみメールを送信し、
793 * To、Cc、Bccアドレスには実際には送信されません。
794 *
795 * @since 1.2
796 * @param addresses
797 */
798 public void addEnvelopeTo(InternetAddress[] addresses) {
799 for (int i = 0; i < addresses.length; i++) {
800 addEnvelopeTo(addresses[i]);
801 }
802 }
803
804 /***
805 * envelope-toの宛先アドレスを追加します。
806 * <p>
807 * envelope-toアドレスがセットされている場合、envelope-toのアドレスにのみメールを送信し、
808 * To、Cc、Bccアドレスには実際には送信されません。
809 *
810 * @since 1.2
811 * @param emails
812 * @throws IllegalArgumentException 不正なフォーマットのアドレスが指定された場合
813 */
814 public void addEnvelopeTo(String[] emails) {
815 for (int i = 0; i < emails.length; i++) {
816 addEnvelopeTo(emails[i]);
817 }
818 }
819
820 /***
821 * セットされているenvelope-toアドレスを全てクリアします。
822 *
823 * @since 1.2
824 */
825 public void clearEnvelopeTo() {
826 envelopeTo = null;
827 }
828
829 /***
830 * envelope-toアドレス配列を返します。
831 * envelope-toアドレスが一件もセットされていないときは空の配列を返します。
832 *
833 * @since 1.2
834 * @return envelope-toアドレスの配列
835 */
836 public InternetAddress[] getEnvelopeTo() {
837 if (envelopeTo == null) {
838 return new InternetAddress[0];
839 }
840 return (InternetAddress[])envelopeTo.toArray(new InternetAddress[envelopeTo.size()]);
841 }
842
843 /***
844 * 添付ファイル。
845 * <p>
846 * 受信メール(ReceivedMail)の添付ファイルは、常に<code>getFile()</code>メソッドで取得します。
847 * <code>getInputStream()</code>、<code>getUrl()</code>メソッドはnullを返します。
848 * 受信メールに対しては、<code>ReceivedMail.getFiles()</code>メソッドを使うと添付ファイルの
849 * <code>File</code>インスタンス配列を取得することができます。
850 *
851 * @since 1.1
852 * @author Tomohiro Otsuka
853 * @version $Id: Mail.java,v 1.10.2.7 2005/05/11 10:06:18 otsuka Exp $
854 */
855 public class AttachmentFile {
856
857 private String name;
858
859 private File file;
860
861 private InputStream is;
862
863 private URL url;
864
865 /***
866 * ファイル名とファイルを指定して、このクラスのインタンスを生成します。
867 * ファイル名には適切な拡張子が付けられている必要があります。
868 *
869 * @param name メールに表示するファイル名
870 * @param file 添付ファイル
871 */
872 public AttachmentFile(String name, File file) {
873 this.name = name;
874 this.file = file;
875 }
876
877 /***
878 * ファイル名とInputStreamを指定して、このクラスのインタンスを生成します。
879 * ファイル名には適切な拡張子が付けられている必要があります。
880 *
881 * @param name メールに表示するファイル名
882 * @param is 添付ファイルを生成するInputStream
883 */
884 public AttachmentFile(String name, InputStream is) {
885 this.name = name;
886 this.is = is;
887 }
888
889 /***
890 * ファイル名とファイルロケーションのURLを指定して、このクラスのインタンスを生成します。
891 * ファイル名には適切な拡張子が付けられている必要があります。
892 *
893 * @param name メールに表示するファイル名
894 * @param url 添付ファイルのロケーションURL
895 */
896 public AttachmentFile(String name, URL url) {
897 this.name = name;
898 this.url = url;
899 }
900
901 /***
902 * 添付ファイルのDataSourceインスタンスを生成して返します。
903 *
904 * @return 添付ファイルのDataSourceインスタンス
905 */
906 public DataSource getDataSource() {
907 if (file != null) {
908 return new FileDataSource(file);
909 }
910
911 if (url != null) {
912 return new URLDataSource(url);
913 }
914
915
916 String contentType = FileTypeMap.getDefaultFileTypeMap().getContentType(name);
917 return new ByteArrayDataSource(is, contentType);
918 }
919
920 /***
921 * 添付ファイル名を返します。
922 *
923 * @return 添付ファイル名
924 */
925 public String getName() {
926 return name;
927 }
928
929 /***
930 * @return セットされたファイル。またはnull。
931 */
932 public File getFile() {
933 return file;
934 }
935
936 /***
937 * @return セットされたInputStream。またはnull。
938 */
939 public InputStream getInputStream() {
940 return is;
941 }
942
943 /***
944 * @return セットされたURL。またはnull。
945 */
946 public URL getUrl() {
947 return url;
948 }
949 }
950
951 /***
952 * メールの重要度。定数のみを定義。
953 *
954 * @author Tomohiro Otsuka
955 * @version $Id: Mail.java,v 1.10.2.7 2005/05/11 10:06:18 otsuka Exp $
956 */
957 public static class Importance {
958
959 /*** 重要度「高」 */
960 public static final String HIGH = "high";
961
962 /*** 重要度「中」 */
963 public static final String NORMAL = "normal";
964
965 /*** 重要度「低」 */
966 public static final String LOW = "low";
967
968 }
969 }