package com.dailywork.util;import java.io.unsupportedencodingexception;import java.util.properties;import javax.activation.datahandler;import javax.activation.filedatasource; import javax.mail.bodypart;import javax.mail.message;import javax.mail.messagingexception; 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 emailmanager { private properties props; //System Properties private session session; //mail Session Object private MimeMessage Mimemsg; //mime Mail Object private multipart mp; //multipart object, message content, title, Add content such as attachments to the MimeMessage object /** * Constructor * @param smtp&nBSP; Mail sending server */public emailmanager () {props = system.getproperties ();p rops.put (" Mail.smtp.auth "," false "); session = session.getdefaultinstance (Props, null); Session.setdebug (True); Mimemsg = new mimemessage (session); Mp = new mimemultipart (); } /** * constructor * @param smtp Mail sending server */public Emailmanager (String smtp, string username, string password) { props = System.getproperties ();p rops.put ("Mail.smtp.auth", "true") props.put ("Mail.smtp.host", &NBSP;SMTP); Props.put ("username", username);p rops.put ("password", password);session = Session.getdefaultinstance (Props, null); Session.setdebug (true); Mimemsg = new mimemessage ( session); Mp = new mimemultipart (); } /** * Send mail */ public boolean sendmail (String from, string[] to, string[] copyto, string subject, string content, string filename) {try {//Set Sender Mimemsg.setfrom (New internetaddress (from)); //set receiver for (int i = 0; i < to.length; i++) {mimemsg.setrecipients ( Message.recipienttype.to, internetaddress.parse (To[i]) }//set cc person for (int i = 0; i < copyto.length; i++) {mimemsg.setrecipients (message.recipienttype.cc, Internetaddress.parse (Copyto[i]); }//set theme Mimemsg.setsubject (subject);//Set Body bodypart bp = New mimebodypart (); bp.setcontent (content, "Text/html;charset=utf-8"); Mp.addBodyPart (BP);// Set the attachment Bp = new mimebodypart (); Filedatasource fileds = new filedatasource (filename); bp.setdatahandler (new DataHandler (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 void main (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 = "Theme"; string content = "Mail Content"; string filename = "D:\\xxx.txt"; EmailManager email = new emailmanager (Smtp, username, password); email.sendMail (from, to, copyto, subject, content, filename);}}
Java Send mail with cc and attachments