Java implementation of 163 mailbox send mail to QQ mailbox success stories _java

Source: Internet
Author: User
Tags stringbuffer

Download and upload attachments, send text messages and send emails, are commonly used in the program functions, before recording the file upload and download and send text messages, due to the recent busy, the function of the mail to send no time to get, and now finally successfully 163 email to send mail to QQ mailbox, the following is the relevant code, A specific explanation can be referenced in the code:  

Package test; 
Import java.util.ArrayList; 
Import Java.util.Date; 
Import java.util.List; 
Import java.util.Properties; 
Import Java.util.regex.Matcher; 
Import Java.util.regex.Pattern; 
Import javax.mail.Address; 
Import Javax.mail.Authenticator; 
Import Javax.mail.BodyPart; 
Import Javax.mail.Message; 
Import javax.mail.PasswordAuthentication; 
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 Com.sun.mail.util.MailSSLSocketFactory;  * *//*/* @author tuzongxun123//* @Description Mail Send test class////public class Sendmailtest {public static void 
  Main (string[] args) throws Exception {//configuration information Properties Pro = new properties (); 
  Pro.put ("Mail.smtp.host", "smtp.163.com"); 
  Pro.put ("Mail.smtp.auth", "true"); 
  SSL encryption mailsslsocketfactory SF = NULL; SF = new MAILSSLSOCKETFActory (); 
  Set Trust all host sf.settrustallhosts (TRUE); 
  Pro.put ("Mail.smtp.ssl.enable", "true"); 
  Pro.put ("Mail.smtp.ssl.socketFactory", SF); 
  Based on the message's session properties to construct a message to send the sessions, where you need to note that the user name there can not be a suffix, otherwise it is not the user name//Also note that the password here is not the normal use of the mailbox login password, but the client generated another special authorization code 
  Mailauthenticator authenticator = new Mailauthenticator ("tuzongxun123", "Client Authorization Code"); 
  Session session = Session.getinstance (pro, authenticator); 
  Build mail message messages = new MimeMessage (session) based on session; 
  Create mail Sender address from = new InternetAddress ("tuzongxun123@163.com"); 
  Set the sender of the mail message Message.setfrom (from); 
  Verify the recipient's mailbox address list<string> toaddresslist = new arraylist<> (); 
  Toaddresslist.add ("1160569243@qq.com"); 
  StringBuffer buffer = new StringBuffer (); if (!toaddresslist.isempty ()) {String regEx = "^" ([a-z0-9a-z]+[-|\\.]?) +[a-z0-9a-z]@ ([a-z0-9a-z]+ (-[a-z0-9a-z]+)/\.) 
   +[a-za-z]{2,}$ "; 
   Pattern p = pattern.compile (regEx); for (int i = 0; i < toaddresslist.size ();i++) {Matcher match = P.matcher (Toaddresslist.get (i)); 
     if (Match.matches ()) {Buffer.append (Toaddresslist.get (i)); 
     if (I < toaddresslist.size ()-1) {Buffer.append (","); 
  }}} String toaddress = Buffer.tostring (); 
   if (!toaddress.isempty ()) {//Create the recipient address of the message address[] to = Internetaddress.parse (toaddress); 
   Set Mail Recipient address message.setrecipients (Message.RecipientType.TO, to); 
   Mail Subject//message.setsubject ("Java Mail Test"); 
   Message.setsubject ("why Wrong"); 
   Mail container Mimemultipart Mimemultipart = new Mimemultipart (); 
   Set HTML BodyPart bodypart = new MimeBodyPart (); 
   Mail content//String htmltext = "java Mail test 111"; 
   String HTMLText = "why Wrong"; 
   Bodypart.setcontent (HTMLText, "text/html;charset=utf-8"); 
   Mimemultipart.addbodypart (BodyPart); 
   Add Attachment list<string> fileaddresslist = new arraylist<string> (); Fileaddresslist. Add ("c:\\users\\tuzongxun123\\desktop\\ new MicroSoft Office Word document. docx "); 
    if (fileaddresslist!= null) {bodypart attchpart = null; for (int i = 0; i < fileaddresslist.size (); i++) {if (!fileaddresslist.get (i). IsEmpty ()) {Attchpart = NE 
      W MimeBodyPart (); 
      Attachment data sources DataSource Source = new Filedatasource (Fileaddresslist.get (i)); 
      Add the attachment data source to the message body Attchpart.setdatahandler (new DataHandler source); 
      Set the attachment name to the original filename attchpart.setfilename (mimeutility.encodetext (source. GetName ())); 
     Mimemultipart.addbodypart (Attchpart); 
   }} message.setcontent (Mimemultipart); 
   Message.setsentdate (New Date ()); 
   Save Mail Message.savechanges (); 
  Send mail transport.send (message); 
 Class Mailauthenticator extends Authenticator {/** * username/private String username; 
 
 /** * Password/private String password; 
  /** * Create a new instance of Mailauthenticator. 
 * * @param username * @param password * *Public Mailauthenticator (string Username, string password) {this.username = username; 
 This.password = password; 
 Public String GetPassword () {return password; } @Override protected Passwordauthentication getpasswordauthentication () {return new passwordauthentication (user 
 name, password); 
 Public String GetUserName () {return username; 
 } public void SetPassword (String password) {this.password = password; 
 } public void Setusername (String username) {this.username = username;  } 
 
}

Note: I have a colleague to use this code to replace his account and client authorization code, a run on the error, and then reset the mailbox client authorization code, the error disappeared.

The above is the entire content of this article, I hope to learn Java program to help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.