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