1 10 11 package org.mule.providers.email; 12 13 import java.util.Properties ; 14 15 import javax.mail.Address ; 16 import javax.mail.Authenticator ; 17 import javax.mail.PasswordAuthentication ; 18 import javax.mail.Session ; 19 import javax.mail.URLName ; 20 import javax.mail.internet.AddressException ; 21 import javax.mail.internet.InternetAddress ; 22 23 import org.apache.commons.lang.StringUtils; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.mule.config.i18n.Messages; 27 28 31 public class MailUtils 32 { 33 36 protected final static Log logger = LogFactory.getLog(MailUtils.class); 37 38 45 public static Session createMailSession(URLName url, MailConnector connector) 46 { 47 if (url == null) 48 { 49 throw new IllegalArgumentException ( 50 new org.mule.config.i18n.Message(Messages.X_IS_NULL, "URL").toString()); 51 } 52 String protocol = connector.getProtocol().toLowerCase(); 53 boolean secure = false; 54 if (protocol.equals("smtps")) 55 { 56 protocol = "smtp"; 57 secure = true; 58 } 59 else if (protocol.equals("pop3s")) 60 { 61 protocol = "pop3"; 62 secure = true; 63 } 64 else if (protocol.equals("imaps")) 65 { 66 protocol = "imap"; 67 secure = true; 68 } 69 70 Properties props = System.getProperties(); 71 Session session; 72 73 synchronized (props) 75 { 76 props.put("mail." + protocol + ".host", url.getHost()); 77 int port = url.getPort(); 78 if (port == -1) 79 { 80 port = connector.getDefaultPort(); 81 } 82 props.put("mail." + protocol + ".port", String.valueOf(port)); 83 84 if (secure) 85 { 86 System.setProperty("mail." + protocol + ".socketFactory.port", String.valueOf(port)); 87 if (protocol.equals("smtp")) 88 { 89 props = (Properties )props.clone(); 93 94 props.put("mail.smtp.ssl", "true"); 95 props.put("mail.smtp.socketFactory.class", ((SmtpsConnector)connector).getSocketFactory()); 96 props.put("mail.smtp.socketFactory.fallback", 97 ((SmtpsConnector)connector).getSocketFactoryFallback()); 98 99 if (((SmtpsConnector)connector).getTrustStore() != null) 100 { 101 System.setProperty("javax.net.ssl.trustStore", 102 ((SmtpsConnector)connector).getTrustStore()); 103 if (((SmtpsConnector)connector).getTrustStorePassword() != null) 104 { 105 System.setProperty("javax.net.ssl.trustStorePassword", 106 ((SmtpsConnector)connector).getTrustStorePassword()); 107 } 108 } 109 } 110 } 111 props.setProperty("mail." + protocol + ".rsetbeforequit", "true"); 112 113 if (StringUtils.isNotBlank(url.getPassword())) 114 { 115 props.put("mail." + protocol + ".auth", "true"); 116 Authenticator auth = connector.getAuthenticator(); 117 if (auth == null) 118 { 119 auth = new DefaultAuthenticator(url.getUsername(), url.getPassword()); 120 logger.debug("No Authenticator set on Connector: " + connector.getName() 121 + ". Using default."); 122 } 123 session = Session.getInstance(props, auth); 124 } 125 else 126 { 127 props.put("mail." + protocol + ".auth", "false"); 129 session = Session.getInstance(props, null); 130 } 131 } 132 133 return session; 134 } 135 136 public static String internetAddressesToString(InternetAddress [] addresses) 137 { 138 if (addresses == null || addresses.length == 0) 139 { 140 return StringUtils.EMPTY; 141 } 142 StringBuffer buf = new StringBuffer (); 143 for (int i = 0; i < addresses.length; i++) 144 { 145 InternetAddress address = addresses[i]; 146 buf.append(address.getAddress()); 147 if (i < addresses.length - 1) 149 { 150 buf.append(", "); 151 } 152 } 153 return buf.toString(); 154 } 155 156 public static String internetAddressesToString(InternetAddress address) 157 { 158 return internetAddressesToString(new InternetAddress []{address}); 159 } 160 161 public static String mailAddressesToString(Address [] addresses) 162 { 163 if (addresses == null || addresses.length == 0) 164 { 165 return StringUtils.EMPTY; 166 } 167 StringBuffer buf = new StringBuffer (); 168 for (int i = 0; i < addresses.length; i++) 169 { 170 Address address = addresses[i]; 171 buf.append(address.toString()); 172 if (i < addresses.length - 1) 174 { 175 buf.append(", "); 176 } 177 } 178 return buf.toString(); 179 } 180 181 public static String mailAddressesToString(Address address) 182 { 183 return mailAddressesToString(new Address []{address}); 184 } 185 186 public static InternetAddress [] stringToInternetAddresses(String address) throws AddressException 187 { 188 InternetAddress [] inetaddresses; 189 if (StringUtils.isNotBlank(address)) 190 { 191 inetaddresses = InternetAddress.parse(address, false); 192 } 193 else 194 { 195 throw new NullPointerException (new org.mule.config.i18n.Message(Messages.X_IS_NULL, 196 "Email address").toString()); 197 } 198 return inetaddresses; 199 } 200 201 205 private static class DefaultAuthenticator extends javax.mail.Authenticator 206 { 207 private String username = null; 208 private String password = null; 209 210 public DefaultAuthenticator(String user, String pwd) 211 { 212 username = user; 213 password = pwd; 214 } 215 216 public PasswordAuthentication getPasswordAuthentication() 217 { 218 return new PasswordAuthentication (username, password); 219 } 220 } 221 } 222 | Popular Tags |