Introduction to a concept
Secure Socket Layer, developed for Netscape to secure data transmission over the Internet, uses the encryption (encryption) technology to ensure that data is not intercepted and tapped during transmission over the network. The general General specification is the safety standard of the A-bit, while the United States has introduced a higher safety standard of-bit, but restricted its exit. As long as the 3.0 version above the i.e. or Netscape browser can support SSL.
The current version is 3.0. It has been widely used for authentication and encrypted data transfer between Web browsers and servers.
(PS: to Baidu Encyclopedia)
ii encrypt mail sending using SSL in JavaMail
In fact, most of the operations are the same as normal mail delivery, only two places have changed. (1) Transfer port changed from 25 to 465; (2) Replace default Socketfactory
Below I use 163 mailbox to implement the SSL transmission example, is the 163 mailbox official gives the relevant configuration information:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/77/6F/wKioL1Zn1viwAgyiAAAwjTscizQ247.png "title=" 163 Mailbox " alt= "Wkiol1zn1viwagyiaaawjtscizq247.png"/>
package javamail.zifangsky.com;import java.io.unsupportedencodingexception;import java.util.arraylist;import java.util.date;import java.util.list;import java.util.properties; import javax.activation.datahandler;import javax.activation.filedatasource;import javax.mail.address;import javax.mail.bodypart;import javax.mail.multipart;import Javax.mail.session;import javax.mail.transport;import javax.mail.internet.internetaddress;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.mimemultipart;import javax.mail.internet.mimeutility;public class sendmailbyssl {private final string ssl_factory = " Javax.net.ssl.SSLSocketFactory ";p RIVATE&NBSP;STRING&NBSP;SMTPSERVER;&NBSP;//&NBSP;SMTP server address private string port; // Port private string username; // user name to log on to the SMTP server PRIVATE&NBSP;STRING&NBsp;password; // password to log on to the SMTP server private list<string> recipients = new Arraylist<string> (); // recipient address Collection private string subject; // message subject private String content; // message body private list<string> attachmentnames = New arraylist<string> (); // attachment path information collection Public sendmailbyssl () {}public Sendmailbyssl (String smtpserver, string port, string username,string password, list<string> recipients, string subject,string content, list<string > attachmentnames) {this.smtpServer = smtpServer;this.port = port; this.username = username;this.password = password;this.recipients = recipients; this.subject = subject;this.content = content;this.attachmentnames = Attachmentnames;} Public void setsmtpserver (String smtpserver) {this.smtpserver = smtpserver;} Public void setport (String port) {this.port = port;} Public void setusername (String username) {this.username = username;} Public void setpassword (String password) {this.password = password;} Public void setrecipients (list<string> recipients) {this.recipients = Recipients;} Public void setsubject (String subject) {this.subject = subject;} Public void setcontent (string content) {this.content = content;} Public void setattachmentnames (list<string> attachmentnames) {this.attachmentNames = attachmentnames;} /** * for base64 encryption to prevent Chinese garbled * */public string changeencode (STRING&NBSP;STR) { Try {str = mimeutility.encodetext (New string (str.getbytes (), "UTF-8"), "UTF-8 ", " B "); // " B "stands for base64} catch (unsupportedencodingexception e) { E.printstacktrace ();} Return str;} /** * official Email * */public boolean sendmail () {Properties properties = new properties ();p roperties.put ("Mail.smtp.host", smtpserver);p roperties.put ("Mail.smtp.auth" , "true");p roperties.put ("Mail.smtp.socketFactory.class", ssl_factory); //use Jsse SSL socketfactory to replace the default socketfactoryproperties.put ("Mail.smtp.socketFactory.fallback", "false"); // only handles SSL connections and does not handle non-SSL connections properties.put ("Mail.smtp.port", port);p roperties.put (" Mail.smtp.socketFactory.port ", port); Session session = session.getinstance (properties); Session.setdebug (true); Mimemessage message = new mimemessage (session);try {// Sender address address = new internetaddress (username); Message.setfrom (address);// recipient for (string recipient : recipients) {system.out.println ("Recipient:" + recipient); Address toaddress = new internetaddress (recipient); Message.setrecipient ( mimemessage.recipienttype.to, toaddress); // set the recipient and set its receive type to To/** * to: Represents the primary recipient of the health. &NBSP;CC: Represents a CC recipient with health. &NBSP;BCC: Represents the Dark send recipient of the message. * */}// Theme Message.setsubject (Changeencode (subject));// Time Message.setsentdate (new Date ()); Multipart multipart = new mimemultipart ();// Add text bodypart text = new mimebodypart (); Text.settext (content), Multipart.addbodypart (text);// add Attachment for (string Filename : attachmentnames) {bodypart adjunct = new mimebodypart (); Filedatasource filedatasource = new filedatasource (FileName); Adjunct.setdatahandler (new datahandler (Filedatasource)); Adjunct.setfilename (Changeencode (Filedatasource.getname ())); Multipart.addbodypart (adjUNCT);} Empties the recipient collection, Attachment collection recipients.clear (); Attachmentnames.clear (); message.setcontent (multipart); Message.savechanges ();} catch (exception e) {e.printstacktrace (); return false;} Try {transport transport = session.gettransport ("SMTP"); Transport.connect (SmtpServer, username, password); Transport.sendmessage (Message, message.getallrecipients ()); transport.close ();} catch (exception e) {e.printstacktrace (); return false;} Return true;} Public static void main (String[] args) {list<string> recipients = new ArrayList<String> ();//recipients.add ("[email protected]"); Recipients.add ("[Email protected] "); string subject = "This message is to test SSL encrypted transmission of SMTP"; string content = "This is the text of this email"; List<string> attachmentnames = new arraylist<string> (); AttachmentNames.add ("C ://users//administrator//desktop//kali. txt "); Sendmailbyssl sendmailbyssl = new sendmailbyssl ("smtp.163.com", "465", "[email Protected] ", " Youpassword ", recipients, subject, content,attachmentnames); Sendmailbyssl.sendmail ();}}
The three test results are as follows
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/77/6F/wKioL1Zn10DgdSwiAACP2IcGRms800.png "style=" float: none; "title=" 20151205204257_75954.png "alt=" Wkiol1zn10dgdswiaacp2icgrms800.png "/>
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/77/6F/wKioL1Zn10Gh2TNkAACae-ExFUs423.png "style=" float: none; "Title=" QQ Mailbox "alt=" Wkiol1zn10gh2tnkaacae-exfus423.png "/>
Attached: Other articles related to:
JavaMail implementing e-mail (i) Related concepts overview
JavaMail Implement e-mail (ii) Send mail
JavaMail implement send and receive mail (iii) receiving mail
JavaMail implementation of e-mail (iv) Major major mail server address
This article is from "Zifangsky's personal blog" blog, make sure to keep this source http://983836259.blog.51cto.com/7311475/1721164
JavaMail implement send and receive mail (v) encrypted transmission using SSL