Formato de dirección de correo electrónico de Internet ( RFC 822
)
(,)
secuencia de direcciones separada por comas
javax.mail - 1.4.7 parse( String[] )
no está permitido. Entonces tenemos que dar una secuencia de direcciones separada por comas en InternetAddress
objetos. Las direcciones deben seguir la sintaxis RFC822.
String toAddress = "mail@mail.com,mail2@mail.com";
InternetAddress.parse( toAddress );
(;)
secuencia de direcciones separada por punto y coma «Si el grupo de la lista de direcciones se proporciona con delimitador como"; " luego conviértalo a una matriz de cadenas usando el método de división para usar la siguiente función.
String[] addressList = { "mail@mail.com", "mail2@mail.com" };
String toGroup = "mail@mail.com;mail2@mail.com";
String[] addressList2 = toGroup.split(";");
setRecipients(message, addressList);
public static void setRecipients(Message message, Object addresslist) throws AddressException, MessagingException {
if ( addresslist instanceof String ) {
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse( (String) addresslist ));
} else if ( addresslist instanceof String[] ) {
String[] toAddressList = (String[]) addresslist;
InternetAddress[] mailAddress_TO = new InternetAddress[ toAddressList.length ];
for (int i = 0; i < toAddressList.length; i++) {
mailAddress_TO[i] = new InternetAddress( toAddressList[i] );
}
message.setRecipients(Message.RecipientType.TO, mailAddress_TO);
}
}
Ejemplo completo:
public static Properties getMailProperties( boolean addExteraProps ) {
Properties props = new Properties();
props.put("mail.transport.protocol", MAIL_TRNSPORT_PROTOCOL);
props.put("mail.smtp.host", MAIL_SERVER_NAME);
props.put("mail.smtp.port", MAIL_PORT);
props.put("mail.smtp.auth", true);
if( ENCRYPTION_METHOD.equals("STARTTLS") ) {
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.socketFactory.port", SMTP_STARTTLS_PORT);
} else {
props.put("mail.smtps.ssl.enable", true);
props.put("mail.smtp.socketFactory.port", SMTP_SSL_PORT);
}
props.put("mail.smtp.socketFactory", SOCKETFACTORY_CLASS);
return props;
}
public static boolean sendMail(String subject, String contentType, String msg, Object recipients) throws Exception {
Properties props = getMailProperties( false );
Session mailSession = Session.getInstance(props, null);
mailSession.setDebug(true);
Message message = new MimeMessage( mailSession );
message.setFrom( new InternetAddress( USER_NAME ) );
setRecipients(message, recipients);
message.setSubject( subject );
String htmlData = "<h1>This is actual message embedded in HTML tags</h1>";
message.setContent( htmlData, "text/html");
Transport transport = mailSession.getTransport( MAIL_TRNSPORT_PROTOCOL );
transport.connect(MAIL_SERVER_NAME, Integer.valueOf(MAIL_PORT), USER_NAME, PASSWORD);
message.saveChanges();
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
Usando Appache SimpleEmail
-commons-email-1.3.1
Ejemplo: email.addTo( addressList );
public static void sendSimpleMail() throws Exception {
Email email = new SimpleEmail();
email.setSmtpPort(587);
DefaultAuthenticator defaultAuthenticator = new DefaultAuthenticator( USER_NAME, PASSWORD );
email.setAuthenticator( defaultAuthenticator );
email.setDebug(false);
email.setHostName( MAIL_SERVER_NAME );
email.setFrom( USER_NAME );
email.setSubject("Hi");
email.setMsg("This is a test mail ... :-)");
String[] toAddressList = { "mail@mail.com", "mail2@mail.com" }
email.addTo( addressList );
email.setTLS(true);
email.setStartTLSEnabled( true );
email.send();
System.out.println("Mail sent!");
}