1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.email;
12
13 import org.mule.config.i18n.CoreMessages;
14 import org.mule.util.StringUtils;
15
16 import java.io.IOException;
17 import java.io.UnsupportedEncodingException;
18 import java.util.Map;
19
20 import javax.mail.Address;
21 import javax.mail.MessagingException;
22 import javax.mail.Multipart;
23 import javax.mail.Part;
24 import javax.mail.internet.AddressException;
25 import javax.mail.internet.InternetAddress;
26 import javax.mail.internet.MimeUtility;
27
28
29
30
31 public class MailUtils
32 {
33 public static String internetAddressesToString(InternetAddress[] addresses)
34 {
35 if (addresses == null || addresses.length == 0)
36 {
37 return StringUtils.EMPTY;
38 }
39
40 StringBuffer buf = new StringBuffer(80);
41
42 for (int i = 0; i < addresses.length; i++)
43 {
44 InternetAddress address = addresses[i];
45 buf.append(address.getAddress());
46
47 if (i < addresses.length - 1)
48 {
49 buf.append(", ");
50 }
51 }
52
53 return buf.toString();
54 }
55
56 public static String internetAddressesToString(InternetAddress address)
57 {
58 return internetAddressesToString(new InternetAddress[]{address});
59 }
60
61 public static String mailAddressesToString(Address[] addresses)
62 {
63 if (addresses == null || addresses.length == 0)
64 {
65 return StringUtils.EMPTY;
66 }
67
68 StringBuffer buf = new StringBuffer(80);
69
70 for (int i = 0; i < addresses.length; i++)
71 {
72 Address address = addresses[i];
73 buf.append(address.toString());
74
75 if (i < addresses.length - 1)
76 {
77 buf.append(", ");
78 }
79 }
80
81 return buf.toString();
82 }
83
84 public static String mailAddressesToString(Address address)
85 {
86 return mailAddressesToString(new Address[]{address});
87 }
88
89 public static InternetAddress[] stringToInternetAddresses(String address) throws AddressException
90 {
91 if (StringUtils.isNotBlank(address))
92 {
93 return InternetAddress.parse(address, false);
94 }
95 else
96 {
97 throw new IllegalArgumentException(CoreMessages.objectIsNull("Email address").toString());
98 }
99 }
100
101 public static void getAttachments(Multipart content, Map<String, Part> attachments) throws MessagingException, IOException
102 {
103 int x = 0;
104 for(int i = 0; i < content.getCount(); i++)
105 {
106 Part part = content.getBodyPart(i);
107 if (part.getContentType().indexOf("multipart/mixed") > -1)
108 {
109 Multipart m = (Multipart) part.getContent();
110 getAttachments(m, attachments);
111 }
112 else
113 {
114 String key;
115 if (StringUtils.isNotEmpty(part.getDescription()))
116 {
117 key = part.getDescription();
118 }
119 else if (StringUtils.isNotEmpty(part.getFileName()))
120 {
121 try
122 {
123 key = MimeUtility.decodeText(part.getFileName());
124 }
125 catch (UnsupportedEncodingException e)
126 {
127 key = part.getFileName();
128 }
129 }
130 else if (StringUtils.isNotEmpty(part.getDisposition()))
131 {
132 key = part.getDisposition();
133 }
134 else
135 {
136 key = String.valueOf(x++);
137 }
138
139 key = getAttachmentName(key, attachments);
140
141 attachments.put(key, part);
142 }
143 }
144 }
145
146
147
148
149
150
151
152
153
154
155 public static String getAttachmentName(String key, Map<String, Part> attachments)
156 {
157 if (attachments.containsKey(key))
158 {
159 int x = 0;
160 while (attachments.containsKey(x + "_" + key))
161 {
162 x++;
163 }
164 return x + "_" + key;
165
166 } else
167 {
168 return key;
169 }
170 }
171
172
173
174
175
176
177
178
179
180
181 public static boolean isListHeader(String name)
182 {
183 return null != name && name.startsWith(MailMuleMessageFactory.HEADER_LIST_PREFIX);
184 }
185
186
187
188
189
190
191
192
193 public static String toListHeader(String header)
194 {
195 if (isListHeader(header))
196 {
197 return header;
198 }
199 else
200 {
201 return MailMuleMessageFactory.HEADER_LIST_PREFIX + header;
202 }
203 }
204 }