Prior to the use of a program in the mail-sending function, also wrote a related note, at that time using 163 mailboxes, after testing more than 163 accounts are feasible. But recently replaced the account with China Mobilechinamobilesz.com This end of time, but has been not connected to the server, verification does not pass, so had to re-made some changes, this period also refer to another colleague before the wording, successful implementation.
This implementation is similar to the previous one, just after testing, this way not only 163 of the mailbox is not a problem,China Mobile @chinamobilesz.com This is also possible, so it should be better than the one written in front,The specific code is as follows:
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.activation.datahandler;import Javax.activation.datasource;import Javax.activation.filedatasource;import Javax.mail.address;import Javax.mail.bodypart;import Javax.mail.message;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 SendMailTest1 {public static void main (string[] args) {SendMailTest1 send = new SendMailTest1 (); Send.sendemail (); }/** * * @Title: Sendtextmail * @Description: TODO * @param mailinfo * @return * *//Mailm Odel Mailinfo Public boolean sendEmail () {//Read configuration information from the configuration file properties Pro = new properties (); Pro.put ("Mail.smtp.host", "mail.chinamobilesz.com"); Pro.put ("Mail.smtp.auth", "true"); Properties Pro = Mailconfig.getproperties (); Constructs a message session Mailauthenticator Authenticator = new Mailauthenticator ("Account", "password") according to the message's reply property; Session session = Session.getinstance (pro, authenticator); Monitor Mail command try {//build mail message from session message message = new MimeMessage (session); Create mail Sender address from = new InternetAddress ("Xtyw"); Set the sender Message.setfrom (from) of the mail message; Verify email address list<string> auth = new arraylist<string> (); Auth.add ("[email protected]"); String toaddress = Validateemail (auth); if (!toaddress.isempty ()) {//Create message recipient address address[] to = Internetaddress.parse (toaddress); Set the Mail Recipient address message.setrecipients (message.reCipienttype.to, to); Message.setsubject ("12345"); Mail container Mimemultipart Mimemultipart = new Mimemultipart (); Set HTML BodyPart bodypart = new MimeBodyPart (); String htmltext = "123456"; Bodypart.setcontent (HTMLText, "text/html;charset=utf-8"); Mimemultipart.addbodypart (BodyPart); Add Attachment list<string> fileList = new arraylist<string> (); Filelist.add ("c:\\users\\tuzongxun123\\desktop\\ Independent Service Api.docx"); if (fileList! = null) {BodyPart attchpart = null; for (int i = 0; i < filelist.size (); i++) {if (!filelist.get (i). IsEmpty ()) { Attchpart = new MimeBodyPart (); Accessory data source DataSource Source = new Filedatasource (filelist.ge T (i)); Add an attachment data source to the message body Attchpart.setdatahandler (new DataHandler); Set the attachment name to the original file name Attchpart.setfilename (Mimeutility.encodetext (source . GetName ())); Mimemultipart.addbodypart (Attchpart); }}} message.setcontent (Mimemultipart); Message.setsentdate (New Date ()); Save Message Message.savechanges (); Send mail transport.send (message); return true; }} catch (Exception e) {e.printstacktrace (); SYSTEM.OUT.PRINTLN ("message sent Failed"); } return false; }/** * @title validateemail * @Description Verify Mailbox Format * @param emaillist * @return String * * public String Validateemail (list<string> emaillist) { StringBuffer buffer = new StringBuffer (); if (!emaillist.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 < emaillist.size (); i++) {Matcher match = P.matcher (Emaillist.get (i)); if (Match.matches ()) {Buffer.append (Emaillist.get (i)); if (I < emaillist.size ()-1) {Buffer.append (","); }}}} return Buffer.tostring (); }}
Examples and explanations of Java code sending messages (II.)