1 package com.ozacc.mail.fetch;
2
3 import java.io.File;
4 import java.util.Date;
5 import java.util.Iterator;
6
7 import javax.mail.Flags;
8 import javax.mail.MessagingException;
9 import javax.mail.internet.MimeMessage;
10
11 import com.ozacc.mail.Mail;
12
13 /***
14 * 受信メール。
15 * <p>
16 * <code>FetchMail</code>、<code>FetchMailPro</code>の実装クラスで受信したメールが、
17 * インターネットメールとしての仕様を満たしていないヘッダ(FromやToなど)の値がセットされていた場合、
18 * そのヘッダに該当する<code>ReceivedMail</code>インスタンスのプロパティには何もセットされません。
19 *
20 * @since 1.2
21 * @author Tomohiro Otsuka
22 * @version $Id: ReceivedMail.java,v 1.1.2.6 2005/01/19 14:23:25 otsuka Exp $
23 */
24 public class ReceivedMail extends Mail {
25
26 private Date date;
27
28 private String messageId;
29
30 private int size;
31
32 private MimeMessage message;
33
34 /***
35 * コンストラクタ。
36 */
37 public ReceivedMail() {
38 super();
39 }
40
41 /***
42 * コンストラクタ。
43 *
44 * @param charset
45 */
46 public ReceivedMail(String charset) {
47 super(charset);
48 }
49
50 /***
51 * コピーコンストラクタ。
52 *
53 * @param original
54 */
55 public ReceivedMail(Mail original) {
56 super(original);
57 }
58
59 /***
60 * 送信日時を返します。
61 * <p>
62 * 注: メールの受信日時ではありません。
63 *
64 * @return 送信日時
65 */
66 public Date getDate() {
67 return date;
68 }
69
70 /***
71 * 送信日時をセットします。
72 *
73 * @param date 送信日時
74 */
75 public void setDate(Date date) {
76 this.date = date;
77 }
78
79 /***
80 * 前後に<>が付いたメッセージIDを返します。
81 *
82 * @return 前後に<>が付いたメッセージID
83 */
84 public String getMessageId() {
85 return messageId;
86 }
87
88 /***
89 * メッセージIDを返します。前後に<>は付きません。
90 *
91 * @return メッセージID
92 */
93 public String getMessageIdOnly() {
94 return messageId.substring(1, messageId.length() - 1);
95 }
96
97 /***
98 * メッセージIDをセットします。
99 *
100 * @param messageId メッセージID
101 */
102 public void setMessageId(String messageId) {
103 this.messageId = messageId;
104 }
105
106 /***
107 * メール内容を出力します。<br>
108 * メールのソースに似たフォーマットで出力されます。
109 *
110 * @see java.lang.Object#toString()
111 */
112 public String toString() {
113 StringBuffer buf = new StringBuffer(1000);
114 buf.append("Mail\n");
115 buf.append("Return-Path: ").append(returnPath).append("\n");
116 buf.append("Message-ID: ").append(messageId).append("\n");
117 buf.append("Date: ").append(date).append("\n");
118 buf.append("From: ").append(from != null ? from.toUnicodeString() : null).append("\n");
119 buf.append("To: ").append(arrayToCommaDelimitedString(to)).append("\n");
120 buf.append("Cc: ").append(arrayToCommaDelimitedString(cc)).append("\n");
121 buf.append("Bcc: ").append(arrayToCommaDelimitedString(bcc)).append("\n");
122 buf.append("Reply-To: ").append(replyTo != null ? replyTo.toUnicodeString() : null).append(
123 "\n");
124 buf.append("Subject: ").append(subject).append("\n");
125
126 if (xHeaders != null) {
127 for (Iterator itr = xHeaders.keySet().iterator(); itr.hasNext();) {
128 String header = (String)itr.next();
129 String value = (String)xHeaders.get(header);
130 buf.append(header).append(": ").append(value).append("\n");
131 }
132 }
133
134 buf.append("\n");
135 buf.append(text);
136
137 if (htmlText != null) {
138 buf.append("\n\n-----\n\n");
139 buf.append(htmlText);
140 }
141
142 if (isFileAttached()) {
143 buf.append("\n\nAttachments\n");
144 for (int i = 0, num = attachmentFiles.size(); i < num; i++) {
145 AttachmentFile f = (AttachmentFile)attachmentFiles.get(i);
146 buf.append("[").append(i + 1).append("] ").append(f.getName()).append("\n");
147 }
148 }
149
150 return buf.toString();
151 }
152
153 /***
154 * @return Returns the message.
155 */
156 public MimeMessage getMessage() {
157 return message;
158 }
159
160 /***
161 * @param message The message to set.
162 */
163 public void setMessage(MimeMessage message) {
164 this.message = message;
165 }
166
167 /***
168 * メールサーバとの接続切断時に、このメールをメールサーバから削除します。
169 * 削除できるように設定ができた場合に true を返します。
170 * <p>
171 * このメソッドは、<code>FetchMailPro</code>のメソッドによって取得された
172 * <code>ReceivedMail</code>インスタンスでのみ有効です。
173 * また、<code>FetchMailPro</code>インスタンスがメールサーバに
174 * 接続されている状態での呼び出しのみ有効です。<br>
175 * これらの条件が満たされない時にこのメソッドが呼び出された場合
176 * false を返します。
177 *
178 * TODO: うまく動いてない。
179 *
180 * @see FetchMailPro
181 * @param delete 削除するように設定する場合 true
182 * @return 削除設定が正常に行われた場合 true
183 */
184 public boolean setDelete(boolean delete) {
185 if (message != null) {
186 try {
187 message.setFlag(Flags.Flag.DELETED, delete);
188 } catch (MessagingException e) {
189 return false;
190 }
191 return true;
192 }
193 return false;
194 }
195
196 /***
197 * メールのサイズ(容量)を返します。単位はbyte。
198 * この値は厳密なものではないので注意してください。
199 *
200 * @see MimeMessage#getSize()
201 * @return メールのサイズ(単位はbyte)
202 */
203 public int getSize() {
204 return size;
205 }
206
207 /***
208 * メールのサイズ(容量)をセットします。単位はbyte。
209 *
210 * @param size メールのサイズ(単位はbyte)
211 */
212 public void setSize(int size) {
213 this.size = size;
214 }
215
216 /***
217 * 添付ファイルのFileインスタンス配列を返します。
218 * 添付ファイルがない場合は空の配列を返します。
219 *
220 * @return 添付ファイルのFileインスタンス配列
221 */
222 public File[] getFiles() {
223 AttachmentFile[] aFiles = getAttachmentFiles();
224 File[] files = new File[aFiles.length];
225 for (int i = 0; i < aFiles.length; i++) {
226 AttachmentFile aFile = aFiles[i];
227 files[i] = aFile.getFile();
228 }
229 return files;
230 }
231 }