View Javadoc

1   package com.ozacc.mail.impl;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.IOException;
5   import java.io.InputStream;
6   import java.io.OutputStream;
7   import java.io.UnsupportedEncodingException;
8   import java.util.Iterator;
9   import java.util.Map;
10  
11  import javax.activation.DataHandler;
12  import javax.activation.DataSource;
13  import javax.activation.FileDataSource;
14  import javax.mail.MessagingException;
15  import javax.mail.internet.InternetAddress;
16  import javax.mail.internet.MimeBodyPart;
17  import javax.mail.internet.MimeMessage;
18  import javax.mail.internet.MimeMultipart;
19  import javax.mail.internet.MimePart;
20  import javax.mail.internet.MimeUtility;
21  
22  import com.ozacc.mail.Mail;
23  import com.ozacc.mail.MultipartMail;
24  
25  /***
26   * MimeMessage¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤¹¤?¥¯¥é¥¹¡£
27   * 
28   * @since 1.0
29   * @author Tomohiro Otsuka
30   * @version $Id: MimeMessageBuilder.java,v 1.8 2004/09/15 09:03:36 otsuka Exp $
31   */
32  public class MimeMessageBuilder {
33  
34  	private MimeMessage mimeMessage;
35  
36  	private String charset = Mail.JIS_CHARSET;
37  
38  	private boolean hasRecipient = false;
39  
40  	private MimeMultipart mimeMultipart;
41  
42  	/***
43  	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
44  	 * ¥Ç¥Õ¥©¥?¥È¤Îʸ»ú¥³¡¼¥É ISO-2022-JP ¤¬¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤµ¤?¤Þ¤¹¡£
45  	 * 
46  	 * @param mimeMessage
47  	 */
48  	public MimeMessageBuilder(MimeMessage mimeMessage) {
49  		this.mimeMessage = mimeMessage;
50  	}
51  
52  	/***
53  	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
54  	 * ËÜʸ¤ä·?̾¤Î¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É¤ò»ØÄꤷ¤Þ¤¹¡£
55  	 * 
56  	 * @param mimeMessage
57  	 * @param charset ¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É
58  	 */
59  	public MimeMessageBuilder(MimeMessage mimeMessage, String charset) {
60  		this.mimeMessage = mimeMessage;
61  		this.charset = charset;
62  	}
63  
64  	/***
65  	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¤Î°ú¿ô¤ÇÅϤµ¤?¤¿MimeMessage¤ò¤½¤Î¤Þ¤ÞÊÖ¤·¤Þ¤¹¡£
66  	 * 
67  	 * @return MimeMessage
68  	 */
69  	public MimeMessage getMimeMessage() {
70  		return this.mimeMessage;
71  	}
72  
73  	/***
74  	 * »ØÄꤵ¤?¤¿¥á¡¼¥?¤«¤éMimeMessage¤òÀ¸À®¤·¤Þ¤¹¡£
75  	 * 
76  	 * @param mail MimeMessage¤Î¥½¡¼¥¹¤È¤Ê¤?Mail
77  	 * @throws MessagingException
78  	 * @throws UnsupportedEncodingException
79  	 */
80  	public void buildMimeMessage(Mail mail) throws UnsupportedEncodingException, MessagingException {
81  
82  		setTo(mail);
83  
84  		setCc(mail);
85  
86  		setBcc(mail);
87  
88  		// °¸Àè¤Î»ØÄ꤬¤Ê¤¤¾?¹ç¥¨¥é¡¼
89  		if (!hasRecipient) {
90  			throw new MessagingException("°¸Àè¤Î»ØÄ꤬¤¢¤ê¤Þ¤»¤ó¡£To¡¢Cc¡¢Bcc¤Î¤¤¤º¤?¤«°?¤Ä¤Ï»ØÄꤹ¤?ɬÍפ¬¤¢¤ê¤Þ¤¹¡£");
91  		}
92  
93  		setFrom(mail);
94  
95  		setSubject(mail);
96  
97  		setReplyTo(mail);
98  
99  		setXHeaders(mail);
100 
101 		setImportance(mail);
102 
103 		if (mail instanceof MultipartMail) {
104 			MultipartMail mm = (MultipartMail)mail;
105 
106 			if (mm.getAttachmentFiles().length == 0 && mm.isHtmlMail()) { // Plain, HTML
107 
108 				MimeMultipart textAndHtmlMultipart = new MimeMultipart("alternative");
109 				setPlainText(mm, textAndHtmlMultipart);
110 				setHtmlText(mm, textAndHtmlMultipart);
111 				mimeMessage.setContent(textAndHtmlMultipart);
112 
113 			} else if (mm.getAttachmentFiles().length > 0 && mm.isHtmlMail()) { // Plain, HMTL, File
114 
115 				MimeMultipart textAndHtmlMultipart = new MimeMultipart("alternative");
116 				setPlainText(mm, textAndHtmlMultipart);
117 				setHtmlText(mm, textAndHtmlMultipart);
118 
119 				MimeMultipart containingMultipart = new MimeMultipart();
120 				MimeBodyPart textBodyPart = createMimeBodyPart(containingMultipart);
121 				textBodyPart.setContent(textAndHtmlMultipart);
122 				setAttachmentFiles(mm, containingMultipart);
123 
124 				mimeMessage.setContent(containingMultipart);
125 
126 			} else { // Plain, File
127 
128 				MimeMultipart mimeMultipart = new MimeMultipart();
129 				setPlainText(mm, mimeMultipart);
130 				setAttachmentFiles(mm, mimeMultipart);
131 				mimeMessage.setContent(mimeMultipart);
132 
133 			}
134 
135 		} else {
136 			setText(mail.getText(), this.mimeMessage);
137 		}
138 
139 	}
140 
141 	/***
142 	 * 
143 	 * @since 1.1
144 	 * 
145 	 * @param mm
146 	 * @param mimeMultipart
147 	 * @throws MessagingException
148 	 * @throws UnsupportedEncodingException 
149 	 */
150 	private void setAttachmentFiles(MultipartMail mm, MimeMultipart mimeMultipart)
151 																					throws MessagingException,
152 																					UnsupportedEncodingException {
153 		MultipartMail.AttachmentFile[] files = mm.getAttachmentFiles();
154 		for (int i = 0; i < files.length; i++) {
155 			MimeBodyPart bodyPart = createMimeBodyPart(mimeMultipart);
156 			MultipartMail.AttachmentFile attachmentFile = files[i];
157 			addAttachment(attachmentFile.getName(), new FileDataSource(attachmentFile.getFile()),
158 					bodyPart);
159 		}
160 	}
161 
162 	/***
163 	 * 
164 	 * @since 1.1
165 	 * 
166 	 * @param mm
167 	 * @param mimeMultipart
168 	 * @throws MessagingException 
169 	 */
170 	private void setHtmlText(MultipartMail mm, MimeMultipart mimeMultipart)
171 																			throws MessagingException {
172 		if (mm.isHtmlMail()) {
173 			MimeBodyPart bodyPart = createMimeBodyPart(mimeMultipart);
174 			setHtmlText(mm.getHtmlText(), bodyPart);
175 		}
176 	}
177 
178 	/***
179 	 * 
180 	 * @since 1.1
181 	 * 
182 	 * @param mm
183 	 * @param mimeMultipart
184 	 * @throws MessagingException 
185 	 */
186 	private void setPlainText(MultipartMail mm, MimeMultipart mimeMultipart)
187 																			throws MessagingException {
188 		if (mm.getText() != null && mm.getText().length() > 0) {
189 			MimeBodyPart bodyPart = createMimeBodyPart(mimeMultipart);
190 			setText(mm.getText(), bodyPart);
191 		}
192 	}
193 
194 	/***
195 	 * ¿·¤·¤¤MimeBodyPart¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤·¡¢»ØÄꤵ¤?¤¿MimeMultipart¤ËÅÐÏ¿¤·¤Þ¤¹¡£
196 	 * 
197 	 * ¤³¤Î¥á¥½¥Ã¥É¤Ï¥Þ¥?¥Á¥Ñ¡¼¥È¥á¡¼¥?À¸À®»?¤Ë¤Î¤ß¸Æ¤Ó½Ð¤¹¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£
198 	 * ¥×¥?¡¼¥ó¥Æ¥­¥¹¥È¥á¡¼¥?À¸À®»?¤Ë¤Ï¡¢mimeMulipart¤¬null¤Ê¤Î¤Ç¡¢
199 	 * NullPointerException¤¬¥¹¥ú½¼¤µ¤?¤Þ¤¹¡£
200 	 * 
201 	 * @since 1.1
202 	 * 
203 	 * @param mm
204 	 * @return À¸À®¤µ¤?¤¿MimeBodyPart
205 	 * @throws MessagingException 
206 	 */
207 	private MimeBodyPart createMimeBodyPart(MimeMultipart mm) throws MessagingException {
208 		MimeBodyPart bodyPart = new MimeBodyPart();
209 		mm.addBodyPart(bodyPart);
210 		return bodyPart;
211 	}
212 
213 	/***
214 	 * @since 1.1
215 	 * 
216 	 * @param htmlText
217 	 * @param bodyPart 
218 	 * @throws MessagingException
219 	 */
220 	private void setHtmlText(final String htmlText, MimeBodyPart bodyPart)
221 																			throws MessagingException {
222 
223 		// ¤³¤ÎÉôʬ¤Ï Spring 1.0.x ¤«¤é¥³¥Ô¡¼
224 		bodyPart.setDataHandler(new DataHandler(new DataSource() {
225 
226 			public InputStream getInputStream() throws IOException {
227 				return new ByteArrayInputStream(charset != null ? htmlText.getBytes(charset)
228 						: htmlText.getBytes());
229 			}
230 
231 			public OutputStream getOutputStream() throws IOException {
232 				throw new UnsupportedOperationException("Read-only javax.activation.DataSource");
233 			}
234 
235 			public String getContentType() {
236 				return charset == null ? "text/html" : "text/html; charset=" + charset;
237 			}
238 
239 			public String getName() {
240 				return "text";
241 			}
242 		}));
243 
244 		bodyPart.setHeader("Content-Transfer-Encoding", "7bit");
245 	}
246 
247 	/***
248 	 * @param mail 
249 	 * @throws MessagingException
250 	 */
251 	private void setXHeaders(Mail mail) throws MessagingException {
252 		Map headers = mail.getXHeaders();
253 		if (headers == null) {
254 			return;
255 		}
256 
257 		Iterator itr = headers.keySet().iterator();
258 		while (itr.hasNext()) {
259 			String key = (String)itr.next();
260 			String value = (String)headers.get(key);
261 			mimeMessage.setHeader(key, value);
262 		}
263 	}
264 
265 	/***
266 	 * @param mail 
267 	 * @throws MessagingException
268 	 */
269 	private void setImportance(Mail mail) throws MessagingException {
270 		if (mail.getImportance() != null) {
271 			mimeMessage.setHeader("Importance", mail.getImportance());
272 
273 			int level = 3;
274 			if (Mail.Importance.HIGH.equals(mail.getImportance())) {
275 				level = 1;
276 			} else if (Mail.Importance.LOW.equals(mail.getImportance())) {
277 				level = 5;
278 			}
279 			mimeMessage.setHeader("X-Priority", String.valueOf(level));
280 		}
281 	}
282 
283 	/***
284 	 * @param mail 
285 	 * @throws MessagingException
286 	 */
287 	private void setReplyTo(Mail mail) throws MessagingException {
288 		if (mail.getReplyTo() != null) {
289 			mimeMessage.setReplyTo(new InternetAddress[] { mail.getReplyTo() });
290 		}
291 	}
292 
293 	/***
294 	 * @param mail 
295 	 * @throws MessagingException
296 	 */
297 	private void setBcc(Mail mail) throws MessagingException {
298 		if (mail.getBcc().length > 0) {
299 			mimeMessage.setRecipients(MimeMessage.RecipientType.BCC, mail.getBcc());
300 			hasRecipient = true;
301 		}
302 	}
303 
304 	/***
305 	 * @param mail 
306 	 * @throws MessagingException
307 	 */
308 	private void setCc(Mail mail) throws MessagingException {
309 		if (mail.getCc().length > 0) {
310 			mimeMessage.setRecipients(MimeMessage.RecipientType.CC, mail.getCc());
311 			hasRecipient = true;
312 		}
313 	}
314 
315 	/***
316 	 * @param mail 
317 	 * @throws MessagingException
318 	 */
319 	private void setTo(Mail mail) throws MessagingException {
320 		if (mail.getTo().length > 0) {
321 			mimeMessage.setRecipients(MimeMessage.RecipientType.TO, mail.getTo());
322 			hasRecipient = true;
323 		}
324 	}
325 
326 	/***
327 	 * ËÜʸ¤ò¥»¥Ã¥È¡£
328 	 * <p>
329 	 * NOTE: ËÜʸ¤ÎºÇ¸å¤Ë²?¹Ô¤¬¤Ê¤¤¤ÈMozilla·Ï¤Î¥á¡¼¥é¡¼¤ÇºÇ½ª¹Ô¤ÎÆ?Ëܸ?¤¬Ê¸»ú²½¤±¤·¤Æ¤·¤Þ¤¦°Ù¡¢
330 	 * message.setText¤Î°ú¿ô¤ÇºÇ¸å¤Ë\n¤òÄɲ䷤Ƥ¤¤?¡£
331 	 * 
332 	 * @since 1.1
333 	 * 
334 	 * @param text ËÜʸ
335 	 * @param mimePart ËÜʸ¤ò¥»¥Ã¥È¤¹¤?MimePart 
336 	 * @throws MessagingException
337 	 */
338 	private void setText(String text, MimePart mimePart) throws MessagingException {
339 		if (charset != null) {
340 			if (charset.equalsIgnoreCase(Mail.JIS_CHARSET)) {
341 				// Cp932¥¯¥é¥¹¤ò»ÈÍѤ·¤Æ¡¢²ø¤·¤¤µ­¹æ¤ò¶¯À©Åª¤ËJISÊÑ´¹
342 				mimePart.setText(Cp932.toJIS(text) + "\n", charset);
343 			} else {
344 				mimePart.setText(text + "\n", charset);
345 			}
346 		} else {
347 			mimePart.setText(text);
348 		}
349 		mimePart.setHeader("Content-Transfer-Encoding", "7bit");
350 	}
351 
352 	/***
353 	 * @param mail
354 	 * @throws MessagingException
355 	 * @throws UnsupportedEncodingException
356 	 */
357 	private void setSubject(Mail mail) throws UnsupportedEncodingException, MessagingException {
358 		if (charset != null) {
359 			if (charset.equalsIgnoreCase(Mail.JIS_CHARSET)) {
360 				String subject = Cp932.toJIS(mail.getSubject());
361 				mimeMessage.setSubject(MimeUtility.encodeText(subject, charset, "B"));
362 			} else {
363 				mimeMessage.setSubject(mail.getSubject(), charset);
364 			}
365 		} else {
366 			mimeMessage.setSubject(mail.getSubject());
367 		}
368 	}
369 
370 	/***
371 	 * @param mail
372 	 * @throws MessagingException 
373 	 */
374 	private void setFrom(Mail mail) throws MessagingException {
375 		mimeMessage.setFrom(mail.getFrom());
376 	}
377 
378 	/***
379 	 * źÉÕ¥Õ¥¡¥¤¥?¥Ç¡¼¥¿¤ò»ØÄꤵ¤?¤¿MimeBodyPart¤Ë¥»¥Ã¥È¤·¤Þ¤¹¡£
380 	 * 
381 	 * @since 1.1
382 	 * 
383 	 * @param fileName
384 	 * @param dataSource
385 	 * @param mimeBodyPart ¥Õ¥¡¥¤¥?¥Ç¡¼¥¿¤ò¥»¥Ã¥È¤¹¤?MimeBodyPart
386 	 * @throws UnsupportedEncodingException
387 	 * @throws MessagingException
388 	 */
389 	private void addAttachment(String fileName, DataSource dataSource, MimeBodyPart mimeBodyPart)
390 																									throws UnsupportedEncodingException,
391 																									MessagingException {
392 		if (charset != null) {
393 			// ¥Õ¥¡¥¤¥?̾¤Î¥¨¥ó¥³¡¼¥É
394 			mimeBodyPart.setFileName(MimeUtility.encodeText(fileName, charset, "B"));
395 		} else {
396 			mimeBodyPart.setFileName(fileName);
397 		}
398 
399 		mimeBodyPart.setDataHandler(new DataHandler(dataSource));
400 	}
401 }