| 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.activation.DataHandler; |
| 8 |
|
import javax.activation.DataSource; |
| 9 |
|
import javax.mail.MessagingException; |
| 10 |
|
import javax.mail.internet.InternetAddress; |
| 11 |
|
import javax.mail.internet.MimeBodyPart; |
| 12 |
|
import javax.mail.internet.MimeMessage; |
| 13 |
|
import javax.mail.internet.MimeMultipart; |
| 14 |
|
import javax.mail.internet.MimePart; |
| 15 |
|
import javax.mail.internet.MimeUtility; |
| 16 |
|
|
| 17 |
|
import com.ozacc.mail.Mail; |
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
public class MimeMessageBuilder { |
| 27 |
|
|
| 28 |
|
private MimeMessage mimeMessage; |
| 29 |
|
|
| 30 |
16 |
private String charset = Mail.JIS_CHARSET; |
| 31 |
|
|
| 32 |
16 |
private boolean hasRecipient = false; |
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
16 |
public MimeMessageBuilder(MimeMessage mimeMessage) { |
| 41 |
16 |
this.mimeMessage = mimeMessage; |
| 42 |
16 |
} |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
0 |
public MimeMessageBuilder(MimeMessage mimeMessage, String charset) { |
| 52 |
0 |
this.mimeMessage = mimeMessage; |
| 53 |
0 |
this.charset = charset; |
| 54 |
0 |
} |
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
public MimeMessage getMimeMessage() { |
| 62 |
0 |
return this.mimeMessage; |
| 63 |
|
} |
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
public void buildMimeMessage(Mail mail) throws UnsupportedEncodingException, MessagingException { |
| 73 |
|
|
| 74 |
16 |
setTo(mail); |
| 75 |
|
|
| 76 |
16 |
setCc(mail); |
| 77 |
|
|
| 78 |
16 |
setBcc(mail); |
| 79 |
|
|
| 80 |
|
|
| 81 |
16 |
if (!hasRecipient) { |
| 82 |
1 |
throw new MessagingException("°¸Àè¤Î»ØÄ꤬¤¢¤ê¤Þ¤»¤ó¡£To¡¢Cc¡¢Bcc¤Î¤¤¤º¤?¤«°?¤Ä¤Ï»ØÄꤹ¤?ɬÍפ¬¤¢¤ê¤Þ¤¹¡£"); |
| 83 |
|
} |
| 84 |
|
|
| 85 |
15 |
setFrom(mail); |
| 86 |
|
|
| 87 |
15 |
setSubject(mail); |
| 88 |
|
|
| 89 |
15 |
setReplyTo(mail); |
| 90 |
|
|
| 91 |
15 |
setXHeaders(mail); |
| 92 |
|
|
| 93 |
15 |
setImportance(mail); |
| 94 |
|
|
| 95 |
15 |
if (mail.isMultipartMail()) { |
| 96 |
|
|
| 97 |
3 |
if (!mail.isFileAttached() && mail.isHtmlMail()) { |
| 98 |
|
|
| 99 |
3 |
if (mail.getText() != null && mail.getText().length() > 0) { |
| 100 |
|
|
| 101 |
3 |
MimeMultipart textAndHtmlMultipart = new MimeMultipart("alternative"); |
| 102 |
3 |
setPlainText(mail, textAndHtmlMultipart); |
| 103 |
3 |
setHtmlText(mail, textAndHtmlMultipart); |
| 104 |
3 |
this.mimeMessage.setContent(textAndHtmlMultipart); |
| 105 |
|
|
| 106 |
|
} else { |
| 107 |
|
|
| 108 |
0 |
setHtmlText(mail.getHtmlText(), this.mimeMessage); |
| 109 |
|
|
| 110 |
|
} |
| 111 |
|
|
| 112 |
0 |
} else if (mail.isFileAttached() && mail.isHtmlMail()) { |
| 113 |
|
|
| 114 |
0 |
MimeMultipart textAndHtmlMultipart = new MimeMultipart("alternative"); |
| 115 |
0 |
setPlainText(mail, textAndHtmlMultipart); |
| 116 |
0 |
setHtmlText(mail, textAndHtmlMultipart); |
| 117 |
|
|
| 118 |
0 |
MimeMultipart containingMultipart = new MimeMultipart(); |
| 119 |
0 |
MimeBodyPart textBodyPart = createMimeBodyPart(containingMultipart); |
| 120 |
0 |
textBodyPart.setContent(textAndHtmlMultipart); |
| 121 |
0 |
setAttachmentFiles(mail, containingMultipart); |
| 122 |
|
|
| 123 |
0 |
this.mimeMessage.setContent(containingMultipart); |
| 124 |
|
|
| 125 |
0 |
} else if (mail.isFileAttached() && !mail.isHtmlMail()) { |
| 126 |
|
|
| 127 |
0 |
MimeMultipart textAndFileMultipart = new MimeMultipart(); |
| 128 |
0 |
setPlainText(mail, textAndFileMultipart); |
| 129 |
0 |
setAttachmentFiles(mail, textAndFileMultipart); |
| 130 |
0 |
this.mimeMessage.setContent(textAndFileMultipart); |
| 131 |
|
|
| 132 |
|
} else { |
| 133 |
|
|
| 134 |
0 |
setText(mail.getText(), this.mimeMessage); |
| 135 |
|
|
| 136 |
|
} |
| 137 |
|
|
| 138 |
|
} else { |
| 139 |
|
|
| 140 |
12 |
setText(mail.getText(), this.mimeMessage); |
| 141 |
|
|
| 142 |
|
} |
| 143 |
|
|
| 144 |
15 |
} |
| 145 |
|
|
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
|
|
| 154 |
|
|
| 155 |
|
private void setAttachmentFiles(Mail mail, MimeMultipart mimeMultipart) |
| 156 |
|
throws MessagingException, |
| 157 |
|
UnsupportedEncodingException { |
| 158 |
0 |
Mail.AttachmentFile[] files = mail.getAttachmentFiles(); |
| 159 |
0 |
for (int i = 0; i < files.length; i++) { |
| 160 |
0 |
MimeBodyPart bodyPart = createMimeBodyPart(mimeMultipart); |
| 161 |
0 |
Mail.AttachmentFile attachmentFile = files[i]; |
| 162 |
0 |
addAttachment(attachmentFile.getName(), attachmentFile.getDataSource(), bodyPart); |
| 163 |
|
} |
| 164 |
0 |
} |
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
|
|
| 172 |
|
|
| 173 |
|
|
| 174 |
|
private void setHtmlText(Mail mail, MimeMultipart mimeMultipart) throws MessagingException { |
| 175 |
3 |
if (mail.isHtmlMail()) { |
| 176 |
3 |
MimeBodyPart bodyPart = createMimeBodyPart(mimeMultipart); |
| 177 |
3 |
setHtmlText(mail.getHtmlText(), bodyPart); |
| 178 |
|
} |
| 179 |
3 |
} |
| 180 |
|
|
| 181 |
|
|
| 182 |
|
|
| 183 |
|
|
| 184 |
|
|
| 185 |
|
|
| 186 |
|
|
| 187 |
|
|
| 188 |
|
|
| 189 |
|
private void setPlainText(Mail mail, MimeMultipart mimeMultipart) throws MessagingException { |
| 190 |
3 |
if (mail.getText() != null && mail.getText().length() > 0) { |
| 191 |
3 |
MimeBodyPart bodyPart = createMimeBodyPart(mimeMultipart); |
| 192 |
3 |
setText(mail.getText(), bodyPart); |
| 193 |
|
} |
| 194 |
3 |
} |
| 195 |
|
|
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
|
|
| 201 |
|
|
| 202 |
|
|
| 203 |
|
|
| 204 |
|
|
| 205 |
|
|
| 206 |
|
|
| 207 |
|
|
| 208 |
|
|
| 209 |
|
private MimeBodyPart createMimeBodyPart(MimeMultipart mm) throws MessagingException { |
| 210 |
6 |
MimeBodyPart bodyPart = new MimeBodyPart(); |
| 211 |
6 |
mm.addBodyPart(bodyPart); |
| 212 |
6 |
return bodyPart; |
| 213 |
|
} |
| 214 |
|
|
| 215 |
|
|
| 216 |
|
|
| 217 |
|
|
| 218 |
|
|
| 219 |
|
|
| 220 |
|
|
| 221 |
|
|
| 222 |
|
private void setHtmlText(final String htmlText, MimePart mimePart) throws MessagingException { |
| 223 |
3 |
if (charset != null) { |
| 224 |
3 |
mimePart.setContent(htmlText, "text/html; charset=" + charset); |
| 225 |
|
} else { |
| 226 |
0 |
mimePart.setContent(htmlText, "text/html"); |
| 227 |
|
} |
| 228 |
3 |
mimePart.setHeader("Content-Transfer-Encoding", "7bit"); |
| 229 |
3 |
} |
| 230 |
|
|
| 231 |
|
|
| 232 |
|
|
| 233 |
|
|
| 234 |
|
|
| 235 |
|
private void setXHeaders(Mail mail) throws MessagingException { |
| 236 |
15 |
Map headers = mail.getHeaders(); |
| 237 |
15 |
if (headers == null) { |
| 238 |
15 |
return; |
| 239 |
|
} |
| 240 |
|
|
| 241 |
0 |
Iterator itr = headers.keySet().iterator(); |
| 242 |
0 |
while (itr.hasNext()) { |
| 243 |
0 |
String key = (String)itr.next(); |
| 244 |
0 |
String value = (String)headers.get(key); |
| 245 |
0 |
mimeMessage.setHeader(key, value); |
| 246 |
|
} |
| 247 |
0 |
} |
| 248 |
|
|
| 249 |
|
|
| 250 |
|
|
| 251 |
|
|
| 252 |
|
|
| 253 |
|
private void setImportance(Mail mail) throws MessagingException { |
| 254 |
15 |
if (mail.getImportance() != null) { |
| 255 |
1 |
mimeMessage.setHeader("Importance", mail.getImportance()); |
| 256 |
|
|
| 257 |
1 |
int level = 3; |
| 258 |
1 |
if (Mail.Importance.HIGH.equals(mail.getImportance())) { |
| 259 |
1 |
level = 1; |
| 260 |
0 |
} else if (Mail.Importance.LOW.equals(mail.getImportance())) { |
| 261 |
0 |
level = 5; |
| 262 |
|
} |
| 263 |
1 |
mimeMessage.setHeader("X-Priority", String.valueOf(level)); |
| 264 |
|
} |
| 265 |
15 |
} |
| 266 |
|
|
| 267 |
|
|
| 268 |
|
|
| 269 |
|
|
| 270 |
|
|
| 271 |
|
private void setReplyTo(Mail mail) throws MessagingException { |
| 272 |
15 |
if (mail.getReplyTo() != null) { |
| 273 |
0 |
mimeMessage.setReplyTo(new InternetAddress[] { mail.getReplyTo() }); |
| 274 |
|
} |
| 275 |
15 |
} |
| 276 |
|
|
| 277 |
|
|
| 278 |
|
|
| 279 |
|
|
| 280 |
|
|
| 281 |
|
private void setBcc(Mail mail) throws MessagingException { |
| 282 |
16 |
if (mail.getBcc().length > 0) { |
| 283 |
0 |
mimeMessage.setRecipients(MimeMessage.RecipientType.BCC, mail.getBcc()); |
| 284 |
0 |
hasRecipient = true; |
| 285 |
|
} |
| 286 |
16 |
} |
| 287 |
|
|
| 288 |
|
|
| 289 |
|
|
| 290 |
|
|
| 291 |
|
|
| 292 |
|
private void setCc(Mail mail) throws MessagingException { |
| 293 |
16 |
if (mail.getCc().length > 0) { |
| 294 |
0 |
mimeMessage.setRecipients(MimeMessage.RecipientType.CC, mail.getCc()); |
| 295 |
0 |
hasRecipient = true; |
| 296 |
|
} |
| 297 |
16 |
} |
| 298 |
|
|
| 299 |
|
|
| 300 |
|
|
| 301 |
|
|
| 302 |
|
|
| 303 |
|
private void setTo(Mail mail) throws MessagingException { |
| 304 |
16 |
if (mail.getTo().length > 0) { |
| 305 |
15 |
mimeMessage.setRecipients(MimeMessage.RecipientType.TO, mail.getTo()); |
| 306 |
15 |
hasRecipient = true; |
| 307 |
|
} |
| 308 |
16 |
} |
| 309 |
|
|
| 310 |
|
|
| 311 |
|
|
| 312 |
|
|
| 313 |
|
|
| 314 |
|
|
| 315 |
|
|
| 316 |
|
|
| 317 |
|
|
| 318 |
|
|
| 319 |
|
|
| 320 |
|
|
| 321 |
|
|
| 322 |
|
private void setText(String text, MimePart mimePart) throws MessagingException { |
| 323 |
15 |
if (charset != null) { |
| 324 |
15 |
if (charset.equalsIgnoreCase(Mail.JIS_CHARSET)) { |
| 325 |
|
|
| 326 |
15 |
mimePart.setText(Cp932.toJIS(text) + "\n", charset); |
| 327 |
|
} else { |
| 328 |
0 |
mimePart.setText(text + "\n", charset); |
| 329 |
|
} |
| 330 |
|
} else { |
| 331 |
0 |
mimePart.setText(text + "\n"); |
| 332 |
|
} |
| 333 |
15 |
mimePart.setHeader("Content-Transfer-Encoding", "7bit"); |
| 334 |
15 |
} |
| 335 |
|
|
| 336 |
|
|
| 337 |
|
|
| 338 |
|
|
| 339 |
|
|
| 340 |
|
|
| 341 |
|
private void setSubject(Mail mail) throws UnsupportedEncodingException, MessagingException { |
| 342 |
15 |
if (charset != null) { |
| 343 |
15 |
if (charset.equalsIgnoreCase(Mail.JIS_CHARSET)) { |
| 344 |
15 |
String subject = Cp932.toJIS(mail.getSubject()); |
| 345 |
15 |
mimeMessage.setSubject(MimeUtility.encodeText(subject, charset, "B")); |
| 346 |
|
} else { |
| 347 |
0 |
mimeMessage.setSubject(mail.getSubject(), charset); |
| 348 |
|
} |
| 349 |
|
} else { |
| 350 |
0 |
mimeMessage.setSubject(mail.getSubject()); |
| 351 |
|
} |
| 352 |
15 |
} |
| 353 |
|
|
| 354 |
|
|
| 355 |
|
|
| 356 |
|
|
| 357 |
|
|
| 358 |
|
private void setFrom(Mail mail) throws MessagingException { |
| 359 |
15 |
mimeMessage.setFrom(mail.getFrom()); |
| 360 |
15 |
} |
| 361 |
|
|
| 362 |
|
|
| 363 |
|
|
| 364 |
|
|
| 365 |
|
|
| 366 |
|
|
| 367 |
|
|
| 368 |
|
|
| 369 |
|
|
| 370 |
|
|
| 371 |
|
|
| 372 |
|
|
| 373 |
|
private void addAttachment(String fileName, DataSource dataSource, MimeBodyPart mimeBodyPart) |
| 374 |
|
throws UnsupportedEncodingException, |
| 375 |
|
MessagingException { |
| 376 |
0 |
if (charset != null) { |
| 377 |
|
|
| 378 |
0 |
mimeBodyPart.setFileName(MimeUtility.encodeText(fileName, charset, "B")); |
| 379 |
|
} else { |
| 380 |
0 |
mimeBodyPart.setFileName(fileName); |
| 381 |
|
} |
| 382 |
|
|
| 383 |
0 |
mimeBodyPart.setDataHandler(new DataHandler(dataSource)); |
| 384 |
0 |
} |
| 385 |
|
|
| 386 |
|
} |