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