Java Send mail

Source: Internet
Author: User

 PackageCom.dailywork.util;Importjava.io.UnsupportedEncodingException;Importjava.util.Properties;ImportJavax.activation.DataHandler;ImportJavax.activation.FileDataSource;ImportJavax.mail.BodyPart;ImportJavax.mail.Message;Importjavax.mail.MessagingException;ImportJavax.mail.Multipart;Importjavax.mail.Session;ImportJavax.mail.Transport;Importjavax.mail.internet.InternetAddress;ImportJavax.mail.internet.MimeBodyPart;ImportJavax.mail.internet.MimeMessage;ImportJavax.mail.internet.MimeMultipart;Importjavax.mail.internet.MimeUtility;  Public classEmailmanager {PrivateProperties props;//System Properties    PrivateSession session;//Mail Session Object    PrivateMimeMessage mimemsg;//MIME Message Object    PrivateMultipart MP;//multipart objects, message contents, headers, attachments, and so on are added to the MimeMessage object          /*** Constructor *@paramSMTP mail sending server*/     PublicEmailmanager () {Props=system.getproperties (); Props.put ("Mail.smtp.auth", "false"); Session= Session.getdefaultinstance (Props,NULL); Session.setdebug (true); Mimemsg=NewMimeMessage (session); MP=NewMimemultipart (); }      /*** Constructor *@paramSMTP mail sending server*/     PublicEmailmanager (string smtp, string username, string password) {props=system.getproperties (); Props.put ("Mail.smtp.auth", "true"); Props.put ("Mail.smtp.host", SMTP); Props.put ("Username", username); Props.put ("Password", password); Session= Session.getdefaultinstance (Props,NULL); Session.setdebug (true); Mimemsg=NewMimeMessage (session); MP=NewMimemultipart (); }      /*** Send mail*/      Public BooleanSendMail (string from, string["to, string[] CopyTo, string subject, string content, string filename) {Try {            //Set SenderMimemsg.setfrom (Newinternetaddress (from)); //set the recipient.             for(inti = 0; i < to.length; i++) {mimemsg.setrecipients (Message.RecipientType.TO, Internetaddress.parse (To[i])); }            //Set cc people             for(inti = 0; i < copyto.length; i++) {mimemsg.setrecipients (Message.RecipientType.CC, Internetaddress.parse (Copyto[i])); }            //Set ThemeMimemsg.setsubject (subject); //Set BodyBodyPart BP =NewMimeBodyPart (); Bp.setcontent (Content,"Text/html;charset=utf-8");            Mp.addbodypart (BP); //Setting Attachmentsbp =NewMimeBodyPart (); Filedatasource fileds=Newfiledatasource (filename); Bp.setdatahandler (NewDataHandler (fileds)); Bp.setfilename (Mimeutility.encodetext (Fileds.getname (),"UTF-8", "B"));             Mp.addbodypart (BP);             Mimemsg.setcontent (MP);             Mimemsg.savechanges (); //Send mail            if(Props.get ("Mail.smtp.auth"). Equals ("true") {Transport Transport= Session.gettransport ("SMTP"); Transport.connect (String) props.get ("Mail.smtp.host"), (String) props.get ("username"), (String) props.get ("Password"));                 Transport.sendmessage (Mimemsg, Mimemsg.getrecipients (Message.RecipientType.TO));                 Transport.sendmessage (Mimemsg, Mimemsg.getrecipients (Message.RecipientType.CC));             Transport.close (); }Else{transport.send (mimemsg); } System.out.println ("Mail sent successfully"); } Catch(messagingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(unsupportedencodingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        return true; }          Public Static voidMain (string[] args) {String smtp= "Smtp.163.com"; String username= "[Email protected]"; String Password= "XXX"; String from= "[Email protected]"; String[] to= {"[Email protected]"}; String[] CopyTo= {"[Email protected]"}; String subject= "Subject"; String content= "Message Content"; String filename= "D:\\xxx.txt"; Emailmanager Email=NewEmailmanager (SMTP, username, password);    Email.sendmail (from, to, CopyTo, subject, content, filename); }     }

Java Send mail

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.