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