@ -56,6 +56,8 @@ public class MailUtils {
public static final String mailSender = getString ( Constants . MAIL_SENDER ) ;
public static final String mailSender = getString ( Constants . MAIL_SENDER ) ;
public static final String mailUser = getString ( Constants . MAIL_USER ) ;
public static final String mailPasswd = getString ( Constants . MAIL_PASSWD ) ;
public static final String mailPasswd = getString ( Constants . MAIL_PASSWD ) ;
public static final Boolean mailUseStartTLS = getBoolean ( Constants . MAIL_SMTP_STARTTLS_ENABLE ) ;
public static final Boolean mailUseStartTLS = getBoolean ( Constants . MAIL_SMTP_STARTTLS_ENABLE ) ;
@ -68,6 +70,8 @@ public class MailUtils {
public static final String sslEnable = getString ( Constants . MAIL_SMTP_SSL_ENABLE ) ;
public static final String sslEnable = getString ( Constants . MAIL_SMTP_SSL_ENABLE ) ;
public static final String sslTrust = getString ( Constants . MAIL_SMTP_SSL_TRUST ) ;
private static Template MAIL_TEMPLATE ;
private static Template MAIL_TEMPLATE ;
static {
static {
@ -126,16 +130,10 @@ public class MailUtils {
HtmlEmail email = new HtmlEmail ( ) ;
HtmlEmail email = new HtmlEmail ( ) ;
try {
try {
// set the SMTP sending server, 163 as follows: "smtp.163.com"
Session session = getSession ( ) ;
email . setHostName ( mailServerHost ) ;
email . setMailSession ( session ) ;
email . setSmtpPort ( mailServerPort ) ;
email . setFrom ( mailSender ) ;
//set charset
email . setCharset ( Constants . UTF_8 ) ;
email . setCharset ( Constants . UTF_8 ) ;
// TLS verification
email . setTLS ( Boolean . valueOf ( starttlsEnable ) ) ;
// SSL verification
email . setSSL ( Boolean . valueOf ( sslEnable ) ) ;
if ( CollectionUtils . isNotEmpty ( receivers ) ) {
if ( CollectionUtils . isNotEmpty ( receivers ) ) {
// receivers mail
// receivers mail
for ( String receiver : receivers ) {
for ( String receiver : receivers ) {
@ -286,23 +284,11 @@ public class MailUtils {
// Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
// Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
// final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
// final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = new Properties ( ) ;
props . setProperty ( Constants . MAIL_HOST , mailServerHost ) ;
props . setProperty ( Constants . MAIL_SMTP_AUTH , Constants . STRING_TRUE ) ;
props . setProperty ( Constants . MAIL_TRANSPORT_PROTOCOL , mailProtocol ) ;
props . setProperty ( Constants . MAIL_SMTP_STARTTLS_ENABLE , starttlsEnable ) ;
props . setProperty ( "mail.smtp.ssl.enable" , sslEnable ) ;
Authenticator auth = new Authenticator ( ) {
@Override
protected PasswordAuthentication getPasswordAuthentication ( ) {
// mail username and password
return new PasswordAuthentication ( mailSender , mailPasswd ) ;
}
} ;
// 1. The first step in creating mail: creating session
// 1. The first step in creating mail: creating session
Session session = Session . getInstance ( props , auth ) ;
Session session = getSession ( ) ;
// Setting debug mode, can be turned off
// Setting debug mode, can be turned off
session . setDebug ( false ) ;
session . setDebug ( false ) ;
// 2. creating mail: Creating a MimeMessage
// 2. creating mail: Creating a MimeMessage
MimeMessage msg = new MimeMessage ( session ) ;
MimeMessage msg = new MimeMessage ( session ) ;
// 3. set sender
// 3. set sender
@ -314,6 +300,32 @@ public class MailUtils {
return msg ;
return msg ;
}
}
/ * *
* get session
* @return
* /
private static Session getSession ( ) {
Properties props = new Properties ( ) ;
props . setProperty ( Constants . MAIL_HOST , mailServerHost ) ;
props . setProperty ( Constants . MAIL_PORT , String . valueOf ( mailServerPort ) ) ;
props . setProperty ( Constants . MAIL_SMTP_AUTH , Constants . STRING_TRUE ) ;
props . setProperty ( Constants . MAIL_TRANSPORT_PROTOCOL , mailProtocol ) ;
props . setProperty ( Constants . MAIL_SMTP_STARTTLS_ENABLE , starttlsEnable ) ;
props . setProperty ( Constants . MAIL_SMTP_SSL_ENABLE , sslEnable ) ;
props . setProperty ( Constants . MAIL_SMTP_SSL_TRUST , sslTrust ) ;
Authenticator auth = new Authenticator ( ) {
@Override
protected PasswordAuthentication getPasswordAuthentication ( ) {
// mail username and password
return new PasswordAuthentication ( mailUser , mailPasswd ) ;
}
} ;
Session session = Session . getInstance ( props , auth ) ;
return session ;
}
/ * *
/ * *
*
*
* @param receiversCc
* @param receiversCc
@ -370,13 +382,6 @@ public class MailUtils {
* @throws EmailException
* @throws EmailException
* /
* /
private static Map < String , Object > getStringObjectMap ( String title , String content , ShowType showType , Map < String , Object > retMap , HtmlEmail email ) throws EmailException {
private static Map < String , Object > getStringObjectMap ( String title , String content , ShowType showType , Map < String , Object > retMap , HtmlEmail email ) throws EmailException {
// sender's mailbox
email . setFrom ( mailSender , mailSender ) ;
/ * *
* if you need authentication information , set authentication : username - password .
* The registered name and password of the sender on the mail server respectively
* /
email . setAuthentication ( mailSender , mailPasswd ) ;
/ * *
/ * *
* the subject of the message to be sent
* the subject of the message to be sent