Internet 电子邮件地址格式 ( RFC 822
)
(,)
逗号分隔的地址序列
javax.mail - 1.4.7parse( String[] )
是不允许的。所以我们必须将逗号分隔的地址序列赋予InternetAddress
对象。地址必须遵循 RFC822 语法。
String toAddress = "mail@mail.com,mail2@mail.com";
InternetAddress.parse( toAddress );
(;)
分号分隔的地址序列 « 如果地址列表组带有分隔符,则为“;” 然后使用 split 方法转换为 String 数组以使用以下函数。
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 ) { // CharSequence
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse( (String) addresslist ));
} else if ( addresslist instanceof String[] ) { // String[] « Array with collection of Strings/
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);
}
}
完整示例:
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);
// Sending Email to the GMail SMTP server requires authentication and SSL.
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); // 587
} else {
props.put("mail.smtps.ssl.enable", true);
props.put("mail.smtp.socketFactory.port", SMTP_SSL_PORT); // 465
}
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(); // don't forget this
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
使用 Appache SimpleEmail
-commons-email-1.3.1
例子: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 ... :-)");
//email.addTo( "mail@mail.com", "Yash" );
String[] toAddressList = { "mail@mail.com", "mail2@mail.com" }
email.addTo( addressList );
email.setTLS(true);
email.setStartTLSEnabled( true );
email.send();
System.out.println("Mail sent!");
}