1 package com.atlassian.mail.server.impl.util;
2
3 import com.atlassian.mail.Email;
4 import com.atlassian.mail.MailException;
5 import com.atlassian.mail.MailUtils;
6 import javax.mail.internet.MimeMessage;
7 import org.apache.commons.lang.StringUtils;
8
9 import javax.mail.MessagingException;
10 import javax.mail.Multipart;
11 import javax.mail.Part;
12 import javax.mail.internet.InternetAddress;
13 import javax.mail.internet.MimeBodyPart;
14 import java.io.UnsupportedEncodingException;
15 import java.util.Map;
16 import java.util.Calendar;
17 import java.util.Iterator;
18
19 public class MessageCreator
20 {
21 public void updateMimeMessage(Email email, String defaultFrom, String prefix, MimeMessage message) throws MailException, MessagingException, UnsupportedEncodingException
22 {
23 String from = StringUtils.trim(email.getFrom());
24 String fromName = email.getFromName();
25 String to = email.getTo();
26 String cc = email.getCc();
27 String bcc = email.getBcc();
28 String replyTo = email.getReplyTo();
29 String inReplyTo = email.getInReplyTo();
30 String subject = email.getSubject();
31 String body = email.getBody();
32 String mimeType = email.getMimeType();
33 String encoding = email.getEncoding();
34 Map headers = email.getHeaders();
35 Multipart multipart = email.getMultipart();
36
37 if (StringUtils.isBlank(StringUtils.trim(to)) && StringUtils.isBlank(StringUtils.trim(cc)) && StringUtils.isBlank(StringUtils.trim(bcc)))
38 {
39 throw new MailException("Tried to send mail (" + subject + ") with no recipients.");
40 }
41
42 message.setSentDate(Calendar.getInstance().getTime());
43 if (to != null)
44 message.setRecipients(javax.mail.Message.RecipientType.TO, MailUtils.parseAddresses(to));
45
46 if (cc != null)
47 message.setRecipients(javax.mail.Message.RecipientType.CC, MailUtils.parseAddresses(cc));
48
49 if (bcc != null)
50 message.setRecipients(javax.mail.Message.RecipientType.BCC, MailUtils.parseAddresses(bcc));
51
52 if (replyTo != null)
53 message.setReplyTo(MailUtils.parseAddresses(replyTo));
54
55 if (inReplyTo != null)
56 message.setHeader("In-Reply-To", inReplyTo);
57
58 if (StringUtils.isNotBlank(from))
59 {
60
61 InternetAddress internetAddress = new InternetAddress(from);
62 if (StringUtils.isNotBlank(fromName) && internetAddress.getPersonal() == null)
63 {
64 if(encoding != null)
65 {
66 internetAddress.setPersonal(fromName, encoding);
67 }
68 else
69 {
70 internetAddress.setPersonal(fromName);
71 }
72 }
73 message.setFrom(internetAddress);
74 }
75 else if (StringUtils.isNotBlank(defaultFrom))
76 {
77
78 InternetAddress internetAddress = new InternetAddress(defaultFrom);
79 if (StringUtils.isNotBlank(fromName) && internetAddress.getPersonal() == null)
80 {
81 if(encoding != null)
82 {
83 internetAddress.setPersonal(fromName, encoding);
84 }
85 else
86 {
87 internetAddress.setPersonal(fromName);
88 }
89 }
90 message.setFrom(internetAddress);
91 }
92 else
93 {
94 throw new MailException("Tried to send mail (" + subject + ") from no one (no 'from' and 'default from' specified).");
95 }
96
97 String fullSubject = subject;
98 if (StringUtils.isNotBlank(prefix))
99 fullSubject = prefix + " " + fullSubject;
100
101 if (encoding != null)
102 message.setSubject(fullSubject, encoding);
103 else
104 message.setSubject(fullSubject);
105
106 String mimeTypeAndEncoding = mimeType;
107
108 if (encoding != null)
109 mimeTypeAndEncoding += "; charset=" + encoding + "";
110
111 if (multipart != null)
112 {
113
114 if (StringUtils.isNotBlank(body))
115 {
116 MimeBodyPart messageBodyPart = new MimeBodyPart();
117 messageBodyPart.setContent(body, mimeTypeAndEncoding);
118 messageBodyPart.setDisposition(Part.INLINE);
119
120 multipart.addBodyPart(messageBodyPart, 0);
121 }
122
123 message.setContent(multipart);
124 }
125 else
126 {
127 message.setContent(body, mimeTypeAndEncoding);
128 }
129
130
131 if (headers != null)
132 {
133 for (Iterator iterator = headers.entrySet().iterator(); iterator.hasNext();)
134 {
135 Map.Entry entry = (Map.Entry) iterator.next();
136 message.addHeader((String) entry.getKey(), (String) entry.getValue());
137 }
138 }
139 }
140
141 }