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