View Javadoc

1   package com.ozacc.mail.impl;
2   
3   import java.io.UnsupportedEncodingException;
4   import java.util.Iterator;
5   import java.util.Map;
6   
7   import javax.mail.MessagingException;
8   import javax.mail.internet.InternetAddress;
9   import javax.mail.internet.MimeMessage;
10  import javax.mail.internet.MimeUtility;
11  
12  import com.ozacc.mail.Mail;
13  
14  /***
15   * MimeMessage¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤¹¤?¥¯¥é¥¹¡£
16   * 
17   * @since 1.0
18   * @author Tomohiro Otsuka
19   * @version $Id: MimeMessageBuilder.java,v 1.5 2004/09/14 00:04:06 otsuka Exp $
20   */
21  public class MimeMessageBuilder {
22  
23  	private MimeMessage message;
24  
25  	private String charset = Mail.JIS_CHARSET;
26  
27  	private boolean hasRecipient = false;
28  
29  	/***
30  	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
31  	 * ¥Ç¥Õ¥©¥?¥È¤Îʸ»ú¥³¡¼¥É ISO-2022-JP ¤¬¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤµ¤?¤Þ¤¹¡£
32  	 * 
33  	 * @param mimeMessage
34  	 */
35  	public MimeMessageBuilder(MimeMessage mimeMessage) {
36  		this.message = mimeMessage;
37  	}
38  
39  	/***
40  	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
41  	 * ËÜʸ¤ä·?̾¤Î¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É¤ò»ØÄꤷ¤Þ¤¹¡£
42  	 * 
43  	 * @param mimeMessage
44  	 * @param charset ¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É
45  	 */
46  	public MimeMessageBuilder(MimeMessage mimeMessage, String charset) {
47  		this.message = mimeMessage;
48  		this.charset = charset;
49  	}
50  
51  	/***
52  	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¤Î°ú¿ô¤ÇÅϤµ¤?¤¿MimeMessage¤ò¤½¤Î¤Þ¤ÞÊÖ¤·¤Þ¤¹¡£
53  	 * 
54  	 * @return MimeMessage
55  	 */
56  	public MimeMessage getMimeMessage() {
57  		return this.message;
58  	}
59  
60  	/***
61  	 * »ØÄꤵ¤?¤¿¥á¡¼¥?¤«¤éMimeMessage¤òÀ¸À®¤·¤Þ¤¹¡£
62  	 * 
63  	 * @param mail MimeMessage¤Î¥½¡¼¥¹¤È¤Ê¤?Mail
64  	 * @throws MessagingException
65  	 * @throws UnsupportedEncodingException
66  	 */
67  	public void buildMimeMessage(Mail mail) throws UnsupportedEncodingException, MessagingException {
68  
69  		setFrom(mail);
70  
71  		setSubject(mail);
72  
73  		setText(mail);
74  
75  		setTo(mail);
76  
77  		setCc(mail);
78  
79  		setBcc(mail);
80  
81  		setReplyTo(mail);
82  
83  		setXHeaders(mail);
84  
85  		setImportance(mail);
86  
87  		if (!hasRecipient) {
88  			throw new MessagingException("°¸Àè¤Î»ØÄ꤬¤¢¤ê¤Þ¤»¤ó¡£To¡¢Cc¡¢Bcc¤Î¤¤¤º¤?¤«°?¤Ä¤Ï»ØÄꤹ¤?ɬÍפ¬¤¢¤ê¤Þ¤¹¡£");
89  		}
90  
91  	}
92  
93  	/***
94  	 * @param mail 
95  	 * @throws MessagingException
96  	 */
97  	private void setXHeaders(Mail mail) throws MessagingException {
98  		Map headers = mail.getXHeaders();
99  		if (headers == null) {
100 			return;
101 		}
102 
103 		Iterator itr = headers.keySet().iterator();
104 		while (itr.hasNext()) {
105 			String key = (String)itr.next();
106 			String value = (String)headers.get(key);
107 			message.setHeader(key, value);
108 		}
109 	}
110 
111 	/***
112 	 * @param mail 
113 	 * @throws MessagingException
114 	 */
115 	private void setImportance(Mail mail) throws MessagingException {
116 		if (mail.getImportance() != null) {
117 			message.setHeader("Importance", mail.getImportance());
118 
119 			int level = 3;
120 			if (Mail.Importance.HIGH.equals(mail.getImportance())) {
121 				level = 1;
122 			} else if (Mail.Importance.LOW.equals(mail.getImportance())) {
123 				level = 5;
124 			}
125 			message.setHeader("X-Priority", String.valueOf(level));
126 		}
127 	}
128 
129 	/***
130 	 * @param mail 
131 	 * @throws MessagingException
132 	 */
133 	private void setReplyTo(Mail mail) throws MessagingException {
134 		if (mail.getReplyTo() != null) {
135 			message.setReplyTo(new InternetAddress[] { mail.getReplyTo() });
136 		}
137 	}
138 
139 	/***
140 	 * @param mail 
141 	 * @throws MessagingException
142 	 */
143 	private void setBcc(Mail mail) throws MessagingException {
144 		if (mail.getBcc().length > 0) {
145 			message.setRecipients(MimeMessage.RecipientType.BCC, mail.getBcc());
146 			hasRecipient = true;
147 		}
148 	}
149 
150 	/***
151 	 * @param mail 
152 	 * @throws MessagingException
153 	 */
154 	private void setCc(Mail mail) throws MessagingException {
155 		if (mail.getCc().length > 0) {
156 			message.setRecipients(MimeMessage.RecipientType.CC, mail.getCc());
157 			hasRecipient = true;
158 		}
159 	}
160 
161 	/***
162 	 * @param mail 
163 	 * @throws MessagingException
164 	 */
165 	private void setTo(Mail mail) throws MessagingException {
166 		if (mail.getTo().length > 0) {
167 			message.setRecipients(MimeMessage.RecipientType.TO, mail.getTo());
168 			hasRecipient = true;
169 		}
170 	}
171 
172 	/***
173 	 * ËÜʸ¤ò¥»¥Ã¥È¡£
174 	 * <p>
175 	 * NOTE: ËÜʸ¤ÎºÇ¸å¤Ë²?¹Ô¤¬¤Ê¤¤¤ÈMozilla·Ï¤Î¥á¡¼¥é¡¼¤ÇºÇ½ª¹Ô¤ÎÆ?Ëܸ?¤¬Ê¸»ú²½¤±¤·¤Æ¤·¤Þ¤¦°Ù¡¢
176 	 * message.setText¤Î°ú¿ô¤ÇºÇ¸å¤Ë\n¤òÄɲ䷤Ƥ¤¤?¡£
177 	 * 
178 	 * @param mail 
179 	 * @throws MessagingException
180 	 */
181 	private void setText(Mail mail) throws MessagingException {
182 		if (charset != null) {
183 			if (charset.equalsIgnoreCase(Mail.JIS_CHARSET)) {
184 				// Cp932¥¯¥é¥¹¤ò»ÈÍѤ·¤Æ¡¢²ø¤·¤¤µ­¹æ¤ò¶¯À©Åª¤ËJISÊÑ´¹
185 				message.setText(Cp932.toJIS(mail.getText()) + "\n", charset);
186 
187 				message.setHeader("Content-Transfer-Encoding", "7bit");
188 			} else {
189 				message.setText(mail.getText() + "\n", charset);
190 			}
191 		} else {
192 			message.setText(mail.getText());
193 		}
194 	}
195 
196 	/***
197 	 * @param mail
198 	 * @throws MessagingException
199 	 * @throws UnsupportedEncodingException
200 	 */
201 	private void setSubject(Mail mail) throws UnsupportedEncodingException, MessagingException {
202 		if (charset != null) {
203 			if (charset.equalsIgnoreCase(Mail.JIS_CHARSET)) {
204 				String subject = Cp932.toJIS(mail.getSubject());
205 				message.setSubject(MimeUtility.encodeText(subject, charset, "B"));
206 			} else {
207 				message.setSubject(mail.getSubject(), charset);
208 			}
209 		} else {
210 			message.setSubject(mail.getSubject());
211 		}
212 	}
213 
214 	/***
215 	 * @param mail
216 	 * @throws MessagingException 
217 	 */
218 	private void setFrom(Mail mail) throws MessagingException {
219 		message.setFrom(mail.getFrom());
220 	}
221 
222 }