View Javadoc

1   package com.ozacc.mail;
2   
3   import java.io.UnsupportedEncodingException;
4   import java.util.ArrayList;
5   import java.util.Collections;
6   import java.util.HashMap;
7   import java.util.List;
8   import java.util.Map;
9   
10  import javax.mail.internet.AddressException;
11  import javax.mail.internet.InternetAddress;
12  
13  /***
14   * ¥á¡¼¥?¡£
15   * 
16   * @author Tomohiro Otsuka
17   * @version $Id: Mail.java,v 1.3 2004/09/05 16:15:35 otsuka Exp $
18   */
19  public class Mail {
20  
21  	/*** <code>ISO-2022-JP</code> */
22  	public static final String JIS_CHARSET = "ISO-2022-JP";
23  
24  	private String charset = JIS_CHARSET;
25  
26  	private String text;
27  
28  	private InternetAddress from;
29  
30  	private String subject;
31  
32  	private List to;
33  
34  	private List cc;
35  
36  	private List bcc;
37  
38  	private InternetAddress returnPath;
39  
40  	private InternetAddress replyTo;
41  
42  	private String importance;
43  
44  	private Map xHeaders;
45  
46  	/***
47  	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
48  	 */
49  	public Mail() {}
50  
51  	/***
52  	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
53  	 * °¸Àè¤äº¹½Ð¿Í¤Î̾Á°¤ò¥¨¥ó¥³¡¼¥É¤¹¤?»?¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É¤ò»ØÄꤷ¤Þ¤¹¡£
54  	 * ¥Ç¥Õ¥©¥?¥È¤Ï<code>ISO-2022-JP</code>¤Ç¤¹¡£
55  	 * <p>
56  	 * Æ?Ëܸ?´Ä¶­¤ÇÍøÍѤ¹¤?¾?¹ç¤ÏÄ̾?Êѹ¹¤¹¤?ɬÍפϤ¢¤ê¤Þ¤»¤ó¡£
57  	 * 
58  	 * @param charset ¥¨¥ó¥³¡¼¥É¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É
59  	 */
60  	public Mail(String charset) {
61  		this();
62  		this.charset = charset;
63  	}
64  
65  	/***
66  	 * ¥¨¥ó¥³¡¼¥É¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É¤òÊÖ¤·¤Þ¤¹¡£
67  	 * 
68  	 * @return ¥¨¥ó¥³¡¼¥É¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É
69  	 */
70  	public String getCharset() {
71  		return charset;
72  	}
73  
74  	/***
75  	 * ¥á¡¼¥?¤Î½ÅÍ×ÅÙ¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
76  	 * °ú¿ô¤Ç»ØÄ?²Äǽ¤ÊÃͤϡÖhigh¡×¡¢¡Önormal¡×¡¢¡Ölow¡×¤Î¤¤¤º¤?¤«¤Ç¤¹¡£
77  	 * 
78  	 * @param importance ¥á¡¼¥?¤Î½ÅÍ×ÅÙ¡£¡Öhigh¡×¡¢¡Önormal¡×¡¢¡Ölow¡×¤Î¤¤¤º¤?¤«¡£
79  	 * @throws IllegalArgumentException »ØÄ?²Äǽ¤ÊÃͰʳ°¤¬»ØÄꤵ¤?¤¿¾?¹?
80  	 * 
81  	 * @see Mail.Importance
82  	 */
83  	public void setImportance(String importance) throws IllegalArgumentException {
84  		if ("high".equals(importance) || "normal".equals(importance) || "low".equals(importance)) {
85  			this.importance = importance;
86  		} else {
87  			throw new IllegalArgumentException("'" + importance + "'¤Ï¡¢¥á¡¼¥?½ÅÍ×Å٤ˤϻØÄê¤Ç¤­¤Ê¤¤ÃͤǤ¹¡£");
88  		}
89  	}
90  
91  	/***
92  	 * ¥á¡¼¥?¤Î½ÅÍ×ÅÙ¤òÊÖ¤·¤Þ¤¹¡£
93  	 * ÃͤϡÖhigh¡×¡¢¡Önormal¡×¡¢¡Ölow¡×¤Î¤¤¤º¤?¤«¤Ç¤¹¡£
94  	 * 
95  	 * @return ¥á¡¼¥?¤Î½ÅÍ×ÅÙ¡£¡Öhigh¡×¡¢¡Önormal¡×¡¢¡Ölow¡×¤Î¤¤¤º¤?¤«¡£
96  	 */
97  	public String getImportance() {
98  		return importance;
99  	}
100 
101 	/***
102 	 * ¥á¡¼¥?¤ÎÁ÷¿®À襢¥É¥?¥¹¤òÄɲä·¤Þ¤¹¡£
103 	 * 
104 	 * @param email Á÷¿®À襢¥É¥?¥¹
105 	 * @throws IllegalArgumentException ÉÔÀµ¤Ê¥Õ¥©¡¼¥Þ¥Ã¥È¤Î¥¢¥É¥?¥¹¤¬»ØÄꤵ¤?¤¿¾?¹?
106 	 */
107 	public void addTo(String email) throws IllegalArgumentException {
108 		if (to == null) {
109 			to = new ArrayList();
110 		}
111 		try {
112 			to.add(new InternetAddress(email));
113 		} catch (AddressException e) {
114 			throw new IllegalArgumentException(e.getMessage());
115 		}
116 	}
117 
118 	/***
119 	 * ¥á¡¼¥?¤ÎÁ÷¿®Àè̾¤È¥¢¥É¥?¥¹¤òÄɲä·¤Þ¤¹¡£
120 	 * ̾Á°¤ÏJIS_CHARSET¤Ç¥¨¥ó¥³¡¼¥É¤µ¤?¤Þ¤¹¡£
121 	 * 
122 	 * @param email Á÷¿®À襢¥É¥?¥¹
123 	 * @param name Á÷¿®Àè̾
124 	 * @throws IllegalArgumentException ÉÔÀµ¤Ê¥Õ¥©¡¼¥Þ¥Ã¥È¤Î¥¢¥É¥?¥¹¤¬»ØÄꤵ¤?¤¿¾?¹?
125 	 */
126 	public void addTo(String email, String name) throws IllegalArgumentException {
127 		if (to == null) {
128 			to = new ArrayList();
129 		}
130 		try {
131 			to.add(new InternetAddress(email, name, charset));
132 		} catch (UnsupportedEncodingException e) {
133 			throw new IllegalArgumentException(e.getMessage());
134 		}
135 	}
136 
137 	/***
138 	 * ¥á¡¼¥?¤ÎÁ÷¿®À襢¥É¥?¥¹¤ÎÇÛÎó¤òÊÖ¤·¤Þ¤¹¡£
139 	 * Á÷¿®À襢¥É¥?¥¹¤¬°?·?¤â¥»¥Ã¥È¤µ¤?¤Æ¤¤¤Ê¤¤¤È¤­¤Ï¶õ¤ÎÇÛÎó¤òÊÖ¤·¤Þ¤¹¡£
140 	 * 
141 	 * @return Á÷¿®À襢¥É¥?¥¹¤ÎÇÛÎ?
142 	 */
143 	public InternetAddress[] getTo() {
144 		if (to == null) {
145 			return new InternetAddress[0];
146 		}
147 		return (InternetAddress[])to.toArray(new InternetAddress[to.size()]);
148 	}
149 
150 	/***
151 	 * CC¤Î¥¢¥É¥?¥¹¤òÄɲä·¤Þ¤¹¡£
152 	 * 
153 	 * @param email CC¤Î¥¢¥É¥?¥¹
154 	 * @throws IllegalArgumentException ÉÔÀµ¤Ê¥Õ¥©¡¼¥Þ¥Ã¥È¤Î¥¢¥É¥?¥¹¤¬»ØÄꤵ¤?¤¿¾?¹?
155 	 */
156 	public void addCc(String email) throws IllegalArgumentException {
157 		if (cc == null) {
158 			cc = new ArrayList();
159 		}
160 		try {
161 			cc.add(new InternetAddress(email));
162 		} catch (AddressException e) {
163 			throw new IllegalArgumentException(e.getMessage());
164 		}
165 	}
166 
167 	/***
168 	 * CC¤Î°¸Ì¾¤È¥¢¥É¥?¥¹¤òÄɲä·¤Þ¤¹¡£
169 	 * ̾Á°¤ÏJIS_CHARSET¤Ç¥¨¥ó¥³¡¼¥É¤µ¤?¤Þ¤¹¡£
170 	 * 
171 	 * @param email CC¤Î¥¢¥É¥?¥¹
172 	 * @param name CC¤Î°¸Ì¾
173 	 * @throws IllegalArgumentException ÉÔÀµ¤Ê¥Õ¥©¡¼¥Þ¥Ã¥È¤Î¥¢¥É¥?¥¹¤¬»ØÄꤵ¤?¤¿¾?¹?
174 	 */
175 	public void addCc(String email, String name) throws IllegalArgumentException {
176 		if (cc == null) {
177 			cc = new ArrayList();
178 		}
179 		try {
180 			cc.add(new InternetAddress(email, name, charset));
181 		} catch (UnsupportedEncodingException e) {
182 			throw new IllegalArgumentException(e.getMessage());
183 		}
184 	}
185 
186 	/***
187 	 * ¥á¡¼¥?¤ÎCC¥¢¥É¥?¥¹¤ÎÇÛÎó¤òÊÖ¤·¤Þ¤¹¡£
188 	 * CC¥¢¥É¥?¥¹¤¬°?·?¤â¥»¥Ã¥È¤µ¤?¤Æ¤¤¤Ê¤¤¤È¤­¤Ï¶õ¤ÎÇÛÎó¤òÊÖ¤·¤Þ¤¹¡£
189 	 * 
190 	 * @return CC¥¢¥É¥?¥¹¤ÎÇÛÎ?
191 	 */
192 	public InternetAddress[] getCc() {
193 		if (cc == null) {
194 			return new InternetAddress[0];
195 		}
196 		return (InternetAddress[])cc.toArray(new InternetAddress[cc.size()]);
197 	}
198 
199 	/***
200 	 * BCC¤Î¥¢¥É¥?¥¹¤òÄɲä·¤Þ¤¹¡£
201 	 * 
202 	 * @param email BCC¤Î¥¢¥É¥?¥¹
203 	 * @throws IllegalArgumentException ÉÔÀµ¤Ê¥Õ¥©¡¼¥Þ¥Ã¥È¤Î¥¢¥É¥?¥¹¤¬»ØÄꤵ¤?¤¿¾?¹?
204 	 */
205 	public void addBcc(String email) throws IllegalArgumentException {
206 		if (bcc == null) {
207 			bcc = new ArrayList();
208 		}
209 		try {
210 			bcc.add(new InternetAddress(email));
211 		} catch (AddressException e) {
212 			throw new IllegalArgumentException(e.getMessage());
213 		}
214 	}
215 
216 	/***
217 	 * ¥á¡¼¥?¤ÎBCC¥¢¥É¥?¥¹¤ÎÇÛÎó¤òÊÖ¤·¤Þ¤¹¡£
218 	 * BCC¥¢¥É¥?¥¹¤¬°?·?¤â¥»¥Ã¥È¤µ¤?¤Æ¤¤¤Ê¤¤¤È¤­¤Ï¶õ¤ÎÇÛÎó¤òÊÖ¤·¤Þ¤¹¡£
219 	 * 
220 	 * @return BCC¥¢¥É¥?¥¹¤ÎÇÛÎ?
221 	 */
222 	public InternetAddress[] getBcc() {
223 		if (bcc == null) {
224 			return new InternetAddress[0];
225 		}
226 		return (InternetAddress[])bcc.toArray(new InternetAddress[bcc.size()]);
227 	}
228 
229 	/***
230 	 * ¥á¡¼¥?¤Îº¹½Ð¿Í¥¢¥É¥?¥¹¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
231 	 * 
232 	 * @param email º¹½Ð¿Í¥¢¥É¥?¥¹
233 	 * @throws IllegalArgumentException ÉÔÀµ¤Ê¥Õ¥©¡¼¥Þ¥Ã¥È¤Î¥¢¥É¥?¥¹¤¬»ØÄꤵ¤?¤¿¾?¹?
234 	 */
235 	public void setFrom(String email) throws IllegalArgumentException {
236 		try {
237 			from = new InternetAddress(email);
238 		} catch (AddressException e) {
239 			throw new IllegalArgumentException(e.getMessage());
240 		}
241 	}
242 
243 	/***
244 	 * ¥á¡¼¥?¤Îº¹½Ð¿Í̾¤È¥¢¥É¥?¥¹¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
245 	 * ̾Á°¤ÏJIS_CHARSET¤Ç¥¨¥ó¥³¡¼¥É¤µ¤?¤Þ¤¹¡£
246 	 * 
247 	 * @param email º¹½Ð¿Í¥¢¥É¥?¥¹
248 	 * @param name º¹½Ð¿Í̾
249 	 * @throws IllegalArgumentException ÉÔÀµ¤Ê¥Õ¥©¡¼¥Þ¥Ã¥È¤Î¥¢¥É¥?¥¹¤¬»ØÄꤵ¤?¤¿¾?¹?
250 	 */
251 	public void setFrom(String email, String name) throws IllegalArgumentException {
252 		try {
253 			from = new InternetAddress(email, name, charset);
254 		} catch (UnsupportedEncodingException e) {
255 			throw new IllegalArgumentException(e.getMessage());
256 		}
257 	}
258 
259 	/***
260 	 * ¥á¡¼¥?¤Îº¹½Ð¿Í¥¢¥É¥?¥¹¤òÊÖ¤·¤Þ¤¹¡£¥»¥Ã¥È¤µ¤?¤Æ¤¤¤Ê¤¤¾?¹ç¤Ïnull¤òÊÖ¤·¤Þ¤¹¡£
261 	 * 
262 	 * @return ¥á¡¼¥?¤Îº¹½Ð¿Í¥¢¥É¥?¥¹
263 	 */
264 	public InternetAddress getFrom() {
265 		return from;
266 	}
267 
268 	/***
269 	 * Return-Path¥¢¥É¥?¥¹¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
270 	 * 
271 	 * @param email Return-Path¥¢¥É¥?¥¹
272 	 * @throws IllegalArgumentException ÉÔÀµ¤Ê¥Õ¥©¡¼¥Þ¥Ã¥È¤Î¥¢¥É¥?¥¹¤¬»ØÄꤵ¤?¤¿¾?¹?
273 	 */
274 	public void setReturnPath(String email) throws IllegalArgumentException {
275 		try {
276 			returnPath = new InternetAddress(email);
277 		} catch (AddressException e) {
278 			throw new IllegalArgumentException(e.getMessage());
279 		}
280 	}
281 
282 	/***
283 	 * Return-Path¥¢¥É¥?¥¹¤òÊÖ¤·¤Þ¤¹¡£
284 	 * 
285 	 * @return Return-Path¥¢¥É¥?¥¹
286 	 */
287 	public InternetAddress getReturnPath() {
288 		return returnPath;
289 	}
290 
291 	/***
292 	 * ÊÖ¿®À襢¥É¥?¥¹¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
293 	 * 
294 	 * @param email ÊÖ¿®À襢¥É¥?¥¹
295 	 * @throws IllegalArgumentException ÉÔÀµ¤Ê¥Õ¥©¡¼¥Þ¥Ã¥È¤Î¥¢¥É¥?¥¹¤¬»ØÄꤵ¤?¤¿¾?¹?
296 	 */
297 	public void setReplyTo(String email) throws IllegalArgumentException {
298 		try {
299 			replyTo = new InternetAddress(email);
300 		} catch (AddressException e) {
301 			throw new IllegalArgumentException(e.getMessage());
302 		}
303 	}
304 
305 	/***
306 	 * ¥á¡¼¥?¤ÎÊÖ¿®À襢¥É¥?¥¹¤òÊÖ¤·¤Þ¤¹¡£¥»¥Ã¥È¤µ¤?¤Æ¤¤¤Ê¤¤¾?¹ç¤Ïnull¤òÊÖ¤·¤Þ¤¹¡£
307 	 * 
308 	 * @return ÊÖ¿®À襢¥É¥?¥¹
309 	 */
310 	public InternetAddress getReplyTo() {
311 		return replyTo;
312 	}
313 
314 	/***
315 	 * ¥á¡¼¥?¤Î·?̾¤òÊÖ¤·¤Þ¤¹¡£¥»¥Ã¥È¤µ¤?¤Æ¤¤¤Ê¤¤¾?¹ç¤Ï¶õʸ»úÎó¤òÊÖ¤·¤Þ¤¹¡£
316 	 * 
317 	 * @return ¥á¡¼¥?¤Î·?̾
318 	 */
319 	public String getSubject() {
320 		if (subject == null) {
321 			return "";
322 		}
323 		return subject;
324 	}
325 
326 	/***
327 	 * ¥á¡¼¥?¤Î·?̾¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
328 	 * 
329 	 * @param subject ¥á¡¼¥?¤Î·?̾
330 	 */
331 	public void setSubject(String subject) {
332 		this.subject = subject;
333 	}
334 
335 	/***
336 	 * ¥á¡¼¥?ËÜʸ¤òÊÖ¤·¤Þ¤¹¡£
337 	 * ËÜʸ¥»¥Ã¥È¤µ¤?¤Æ¤¤¤Ê¤¤¾?¹ç¤Ï¶õʸ»úÎó¤òÊÖ¤·¤Þ¤¹¡£
338 	 * 
339 	 * @return ¥á¡¼¥?ËÜʸ
340 	 */
341 	public String getText() {
342 		if (text == null) {
343 			return "";
344 		}
345 		return text;
346 	}
347 
348 	/***
349 	 * ¥á¡¼¥?ËÜʸ¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
350 	 * 
351 	 * @param text ¥á¡¼¥?ËÜʸ
352 	 */
353 	public void setText(String text) {
354 		this.text = text;
355 	}
356 
357 	/***
358 	 * ¥á¡¼¥?¥Ø¥Ã¥À¤ËǤ°Õ¤Î¥Ø¥Ã¥À¤òÄɲä·¤Þ¤¹¡£
359 	 * Ǥ°Õ¥Ø¥Ã¥À¤Ï¡ÖX-key: value¡×¤Î¥Õ¥©¡¼¥Þ¥Ã¥È¤Ç¥á¡¼¥?¥Ø¥Ã¥À¤ËÁȤ߹?¤Þ¤?¤Þ¤¹¡£
360 	 *  
361 	 * @param key Ǥ°Õ¥Ø¥Ã¥À̾¡£Æ¬¤¬"X-"¤Ç»Ï¤Þ¤Ã¤Æ¤¤¤Ê¤±¤?¤Ð¡¢¼«Æ°Åª¤ËÉÕÍ¿¤µ¤?¤Þ¤¹¡£
362 	 * @param value Ǥ°Õ¥Ø¥Ã¥À¤ÎÃÍ
363 	 */
364 	public void addXHeader(String key, String value) {
365 		if (xHeaders == null) {
366 			xHeaders = new HashMap();
367 		}
368 		if (key.startsWith("X-")) {
369 			xHeaders.put(key, value);
370 		} else {
371 			xHeaders.put("X-" + key, value);
372 		}
373 	}
374 
375 	/***
376 	 * ¥á¡¼¥?¤ÎǤ°Õ¥Ø¥Ã¥À̾¤ÈÃͤÎMap¥¤¥ó¥¹¥¿¥ó¥¹¤òÊÖ¤·¤Þ¤¹¡£
377 	 * Ǥ°Õ¥Ø¥Ã¥À¤¬°?·?¤â¥»¥Ã¥È¤µ¤?¤Æ¤¤¤Ê¤¤¤È¤­¤Ïnull¤òÊÖ¤·¤Þ¤¹¡£
378 	 * <p>
379 	 * ¤³¤ÎMap¥¤¥ó¥¹¥¿¥ó¥¹¤Ø¤Î½¤Àµ¤Ï¤Ç¤­¤Þ¤»¤ó¡£(unmodifiableMap¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£)
380 	 * 
381 	 * @return ¥á¡¼¥?¤ÎǤ°Õ¥Ø¥Ã¥À̾¤ÈÃͤÎMap¥¤¥ó¥¹¥¿¥ó¥¹¡£¤Þ¤¿¤Ïnull¡£
382 	 */
383 	public Map getXHeaders() {
384 		if (xHeaders == null) {
385 			return null;
386 		}
387 		return Collections.unmodifiableMap(xHeaders);
388 	}
389 
390 	/***
391 	 * @see java.lang.Object#toString()
392 	 */
393 	public String toString() {
394 		StringBuffer sb = new StringBuffer("Mail: ");
395 		sb.append("from=").append(this.from == null ? "null" : this.from.toUnicodeString()).append(
396 				"; ");
397 		sb.append("replyTo=")
398 				.append(this.replyTo == null ? "null" : this.replyTo.toUnicodeString())
399 				.append("; ");
400 		sb.append("to=").append(arrayToCommaDelimitedString(this.to)).append("; ");
401 		sb.append("cc=").append(arrayToCommaDelimitedString(this.cc)).append("; ");
402 		sb.append("bcc=").append(arrayToCommaDelimitedString(this.bcc)).append("; ");
403 		sb.append("subject=").append(this.subject).append("; ");
404 		sb.append("text=").append(this.text);
405 		return sb.toString();
406 	}
407 
408 	/***
409 	 * @param list
410 	 * @return 
411 	 */
412 	private String arrayToCommaDelimitedString(List list) {
413 		if (list == null) {
414 			return "null";
415 		} else {
416 			StringBuffer sb = new StringBuffer();
417 			for (int i = 0, num = list.size(); i < num; i++) {
418 				if (i > 0) {
419 					sb.append(", ");
420 				}
421 				sb.append(((InternetAddress)list.get(i)).toUnicodeString());
422 			}
423 			return sb.toString();
424 		}
425 	}
426 
427 	/***
428 	 * ¥á¡¼¥?¤Î½ÅÍ×ÅÙ¡£Ä?¿ô¤Î¤ß¤òÄ?µÁ¡£
429 	 * 
430 	 * @author Tomohiro Otsuka
431 	 * @version $Id: Mail.java,v 1.3 2004/09/05 16:15:35 otsuka Exp $
432 	 */
433 	public static class Importance {
434 
435 		/*** ½ÅÍ×ÅÙ¡Ö¹â¡× */
436 		public static final String HIGH = "high";
437 
438 		/*** ½ÅÍ×ÅÙ¡ÖÃæ¡× */
439 		public static final String NORMAL = "normal";
440 
441 		/*** ½ÅÍ×ÅÙ¡ÖÄã¡× */
442 		public static final String LOW = "low";
443 
444 	}
445 }