View Javadoc

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 com.opensymphony.util.TextUtils;
7   import alt.javax.mail.internet.MimeMessage;
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 = TextUtils.noNull(email.getFrom()).trim();
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 (!TextUtils.stringSet(TextUtils.noNull(to).trim()) && !TextUtils.stringSet(TextUtils.noNull(cc).trim()) && !TextUtils.stringSet(TextUtils.noNull(bcc).trim()))
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 (TextUtils.stringSet(from))
59          {
60              // Checks if the email address has a personal name attached to it
61              InternetAddress internetAddress = new InternetAddress(from);
62              if (TextUtils.stringSet(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 (TextUtils.stringSet(defaultFrom))
76          {
77              // Checks if the email address has a personal name attached to it            
78              InternetAddress internetAddress = new InternetAddress(defaultFrom);
79              if (TextUtils.stringSet(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 (TextUtils.stringSet(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             // Create a MimeBodyPart for the body
114             MimeBodyPart messageBodyPart = new MimeBodyPart();
115             messageBodyPart.setContent(body, mimeTypeAndEncoding);
116             messageBodyPart.setDisposition(Part.INLINE);
117 
118             // add to multipart passed in
119             multipart.addBodyPart(messageBodyPart, 0);
120 
121             message.setContent(multipart);
122         }
123         else
124         {
125             message.setContent(body, mimeTypeAndEncoding);
126         }
127 
128         // Add the custom headers (if any)
129         if (headers != null)
130         {
131             for (Iterator iterator = headers.entrySet().iterator(); iterator.hasNext();)
132             {
133                 Map.Entry entry = (Map.Entry) iterator.next();
134                 message.addHeader((String) entry.getKey(), (String) entry.getValue());
135             }
136         }
137     }
138 
139 }