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.util.Map; |
18 | |
|
19 | |
import javax.mail.Address; |
20 | |
import javax.mail.MessagingException; |
21 | |
import javax.mail.Multipart; |
22 | |
import javax.mail.Part; |
23 | |
import javax.mail.internet.AddressException; |
24 | |
import javax.mail.internet.InternetAddress; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 0 | public class MailUtils |
30 | |
{ |
31 | |
public static String internetAddressesToString(InternetAddress[] addresses) |
32 | |
{ |
33 | 0 | if (addresses == null || addresses.length == 0) |
34 | |
{ |
35 | 0 | return StringUtils.EMPTY; |
36 | |
} |
37 | |
|
38 | 0 | StringBuffer buf = new StringBuffer(80); |
39 | |
|
40 | 0 | for (int i = 0; i < addresses.length; i++) |
41 | |
{ |
42 | 0 | InternetAddress address = addresses[i]; |
43 | 0 | buf.append(address.getAddress()); |
44 | |
|
45 | 0 | if (i < addresses.length - 1) |
46 | |
{ |
47 | 0 | buf.append(", "); |
48 | |
} |
49 | |
} |
50 | |
|
51 | 0 | return buf.toString(); |
52 | |
} |
53 | |
|
54 | |
public static String internetAddressesToString(InternetAddress address) |
55 | |
{ |
56 | 0 | return internetAddressesToString(new InternetAddress[]{address}); |
57 | |
} |
58 | |
|
59 | |
public static String mailAddressesToString(Address[] addresses) |
60 | |
{ |
61 | 0 | if (addresses == null || addresses.length == 0) |
62 | |
{ |
63 | 0 | return StringUtils.EMPTY; |
64 | |
} |
65 | |
|
66 | 0 | StringBuffer buf = new StringBuffer(80); |
67 | |
|
68 | 0 | for (int i = 0; i < addresses.length; i++) |
69 | |
{ |
70 | 0 | Address address = addresses[i]; |
71 | 0 | buf.append(address.toString()); |
72 | |
|
73 | 0 | if (i < addresses.length - 1) |
74 | |
{ |
75 | 0 | buf.append(", "); |
76 | |
} |
77 | |
} |
78 | |
|
79 | 0 | return buf.toString(); |
80 | |
} |
81 | |
|
82 | |
public static String mailAddressesToString(Address address) |
83 | |
{ |
84 | 0 | return mailAddressesToString(new Address[]{address}); |
85 | |
} |
86 | |
|
87 | |
public static InternetAddress[] stringToInternetAddresses(String address) throws AddressException |
88 | |
{ |
89 | 0 | if (StringUtils.isNotBlank(address)) |
90 | |
{ |
91 | 0 | return InternetAddress.parse(address, false); |
92 | |
} |
93 | |
else |
94 | |
{ |
95 | 0 | throw new IllegalArgumentException(CoreMessages.objectIsNull("Email address").toString()); |
96 | |
} |
97 | |
} |
98 | |
|
99 | |
public static void getAttachments(Multipart content, Map attachments) throws MessagingException, IOException |
100 | |
{ |
101 | 0 | int x = 0; |
102 | 0 | for(int i=0; i < content.getCount(); i++) |
103 | |
{ |
104 | 0 | Part p = content.getBodyPart(i); |
105 | 0 | if(p.getContentType().indexOf("multipart/mixed") > -1) |
106 | |
{ |
107 | 0 | Multipart m = (Multipart)p.getContent(); |
108 | 0 | getAttachments(m, attachments); |
109 | 0 | } |
110 | |
else |
111 | |
{ |
112 | |
String key; |
113 | 0 | if(StringUtils.isNotEmpty(p.getDescription())) |
114 | |
{ |
115 | 0 | key = p.getDescription(); |
116 | |
} |
117 | 0 | else if(StringUtils.isNotEmpty(p.getFileName())) |
118 | |
{ |
119 | 0 | key = p.getFileName(); |
120 | |
} |
121 | 0 | else if(StringUtils.isNotEmpty(p.getDisposition())) |
122 | |
{ |
123 | 0 | key = p.getDisposition(); |
124 | |
} |
125 | |
else |
126 | |
{ |
127 | 0 | key = String.valueOf(x++); |
128 | |
} |
129 | 0 | attachments.put(key, p); |
130 | |
} |
131 | |
} |
132 | 0 | } |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
public static boolean isListHeader(String name) |
144 | |
{ |
145 | 0 | return null != name && name.startsWith(MailMuleMessageFactory.HEADER_LIST_PREFIX); |
146 | |
} |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
public static String toListHeader(String header) |
156 | |
{ |
157 | 0 | if (isListHeader(header)) |
158 | |
{ |
159 | 0 | return header; |
160 | |
} |
161 | |
else |
162 | |
{ |
163 | 0 | return MailMuleMessageFactory.HEADER_LIST_PREFIX + header; |
164 | |
} |
165 | |
} |
166 | |
} |