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