package com.fr.function; import java.io.IOException; import java.security.Security; import java.util.Date; import java.util.Properties; import javax.mail.Address; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import com.sun.mail.util.MailSSLSocketFactory; public class MailTest { public static void main(String[] args) throws Exception { MailUtil.sendEmil465 ( "***** @ qq.com", " National Energy Group E-mail Test 4"); } / ** * Use encryption method, using the 465 port to transmit the message, open ssl * @param to as the recipient's mailbox * @param message sent message * / public static void sendEmil465 (to String, String message) { the try { Final smtpHost String = "mail.chnenergy.com.cn"; Final SMTPPORT String = "465"; Final username = String "****@chnenergy.com.cn"; Final String password = "****"; Final Subject String = "national energy Group mailing test 5"; Security.addProvider (new new com.sun. net.ssl.internal.ssl.Provider ()); // set the mail session parameters the Properties props = new new the Properties (); sending mail // server address props.setProperty ( "the mail.smtp.host", smtpHost); props.setProperty ( "mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); The props. setProperty ( "mail.smtp.socketFactory.fallback", "false"); // 465 port requires a certificate, use MailSSLSocketFactory avoids the need to add a certificate, a lot of people unsuccessful because of missing wrote the following code MailSSLSocketFactory sf = new MailSSLSocketFactory ( ); sf.setTrustAllHosts (to true); props.put ( "mail.smtp.ssl.socketFactory", SF); props.setProperty ( "mail.smtp.ssl.enable", "to true"); // send mail server port, where the port is set to 465 props.setProperty ( "mail.smtp.port", smtpPort); props.setProperty ( "mail.smtp.socketFactory.port", SMTPPORT); props.put ( "mail.smtp.auth", "to true"); // acquired mailbox session by way anonymous inner classes, and transmits by mail to the authorized user name and password JVM the Session = Session.getDefaultInstance the session (The props, the Authenticator new new () { protected PasswordAuthentication the getPasswordAuthentication () { return new new PasswordAuthentication (username, password); } }); session.setDebug (to true); // through the session, to give a message, for transmitting the message MSG = the MimeMessage new new (the session); // set sender msg.setFrom (new InternetAddress (username)) ; // Set the recipient, to a recipient, cc to cc, bcc of BCC msg.setRecipients (Message.RecipientType.TO, InternetAddress.parse (to, to false)); //msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse (to, to false)); //msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse (to, to false)); msg.setSubject (Subject) ; // set mail message msg.setText (message); // set the date transmitted msg.setSentDate (new new a date ()); // call to send to a method of sending mail Transport the Transport.send (MSG); } the catch (Exception E) { e.printStackTrace (); } } }
465 Port reasons for failure, comments
In earlier releases it was necessary to explicitly set a socket factory property to enable use of SSL. In almost all cases, this is no longer necessary. SSL support is built in. However, there is one case where a special socket factory may be needed. JavaMail now includes a special SSL socket factory that can simplify dealing with servers with self-signed certificates. While the recommended approach is to include the certificate in your keystore as described above, the following approach may be simpler in some cases. The class com.sun.mail.util.MailSSLSocketFactory can be used as a simple socket factory that allows trusting all hosts or a specific set of hosts. For example: SF = new new MailSSLSocketFactory MailSSLSocketFactory (); sf.setTrustAllHosts (to true); // or // sf.setTrustedHosts (new new String [] { "My-Server"}); props.put ( "mail.smtp.ssl.enable" , "to true"); // Also use following for Additional Safety //props.put("mail.smtp.ssl.checkserveridentity "," to true "); props.put (" mail.smtp.ssl.socketFactory ", SF ); the use of MailSSLSocketFactory avoids at the certificate at the need to the Add to your AS the keystore Described above, or the configure your own TrustManager AS Described below (using MailSSLSocketFactory avoids the need to add a certificate, your key library as described above, or configure your own. a TrustManager. follows.
Reprinted from: https://blog.csdn.net/allen_zs/article/details/50753311