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 | |
|
32 | |
public static String internetAddressesToString(InternetAddress[] addresses) |
33 | |
{ |
34 | 4 | if (addresses == null || addresses.length == 0) |
35 | |
{ |
36 | 0 | return StringUtils.EMPTY; |
37 | |
} |
38 | |
|
39 | 4 | StringBuffer buf = new StringBuffer(80); |
40 | |
|
41 | 10 | for (int i = 0; i < addresses.length; i++) |
42 | |
{ |
43 | 6 | InternetAddress address = addresses[i]; |
44 | 6 | buf.append(address.getAddress()); |
45 | |
|
46 | 6 | if (i < addresses.length - 1) |
47 | |
{ |
48 | 2 | buf.append(", "); |
49 | |
} |
50 | |
} |
51 | |
|
52 | 4 | return buf.toString(); |
53 | |
} |
54 | |
|
55 | |
public static String internetAddressesToString(InternetAddress address) |
56 | |
{ |
57 | 2 | return internetAddressesToString(new InternetAddress[]{address}); |
58 | |
} |
59 | |
|
60 | |
public static String mailAddressesToString(Address[] addresses) |
61 | |
{ |
62 | 276 | if (addresses == null || addresses.length == 0) |
63 | |
{ |
64 | 230 | return StringUtils.EMPTY; |
65 | |
} |
66 | |
|
67 | 46 | StringBuffer buf = new StringBuffer(80); |
68 | |
|
69 | 92 | for (int i = 0; i < addresses.length; i++) |
70 | |
{ |
71 | 46 | Address address = addresses[i]; |
72 | 46 | buf.append(address.toString()); |
73 | |
|
74 | 46 | if (i < addresses.length - 1) |
75 | |
{ |
76 | 0 | buf.append(", "); |
77 | |
} |
78 | |
} |
79 | |
|
80 | 46 | return buf.toString(); |
81 | |
} |
82 | |
|
83 | |
public static String mailAddressesToString(Address address) |
84 | |
{ |
85 | 0 | return mailAddressesToString(new Address[]{address}); |
86 | |
} |
87 | |
|
88 | |
public static InternetAddress[] stringToInternetAddresses(String address) throws AddressException |
89 | |
{ |
90 | 20 | if (StringUtils.isNotBlank(address)) |
91 | |
{ |
92 | 20 | return InternetAddress.parse(address, false); |
93 | |
} |
94 | |
else |
95 | |
{ |
96 | 0 | throw new IllegalArgumentException(CoreMessages.objectIsNull("Email address").toString()); |
97 | |
} |
98 | |
} |
99 | |
|
100 | |
public static void getAttachments(Multipart content, Map attachments) throws MessagingException, IOException |
101 | |
{ |
102 | 0 | int x = 0; |
103 | 0 | for(int i=0; i < content.getCount(); i++) |
104 | |
{ |
105 | 0 | Part p = content.getBodyPart(i); |
106 | 0 | if(p.getContentType().indexOf("multipart/mixed") > -1) |
107 | |
{ |
108 | 0 | Multipart m = (Multipart)p.getContent(); |
109 | 0 | getAttachments(m, attachments); |
110 | 0 | } |
111 | |
else |
112 | |
{ |
113 | |
String key; |
114 | 0 | if(StringUtils.isNotEmpty(p.getDescription())) |
115 | |
{ |
116 | 0 | key = p.getDescription(); |
117 | |
} |
118 | 0 | else if(StringUtils.isNotEmpty(p.getFileName())) |
119 | |
{ |
120 | 0 | key = p.getFileName(); |
121 | |
} |
122 | 0 | else if(StringUtils.isNotEmpty(p.getDisposition())) |
123 | |
{ |
124 | 0 | key = p.getDisposition(); |
125 | |
} |
126 | |
else |
127 | |
{ |
128 | 0 | key = String.valueOf(x++); |
129 | |
} |
130 | 0 | attachments.put(key, p); |
131 | |
} |
132 | |
} |
133 | 0 | } |
134 | |
} |