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