The following code encapsulates a class that sends a message using the Java Mail package
ImportJava.io.File;Importjava.util.ArrayList;Importjava.util.Date;Importjava.util.List;Importjava.util.Properties;ImportJavax.activation.DataHandler;ImportJavax.activation.FileDataSource;ImportJavax.mail.Message;Importjavax.mail.MessagingException;ImportJavax.mail.Multipart;Importjavax.mail.Session;ImportJavax.mail.Transport;Importjavax.mail.internet.AddressException;Importjavax.mail.internet.InternetAddress;ImportJavax.mail.internet.MimeBodyPart;ImportJavax.mail.internet.MimeMessage;ImportJavax.mail.internet.MimeMultipart; Public classSendMail {Private Static FinalString Mail_address_regex = "^[\\w\\.=-][email protected][\\w\\.-]+\\. [\\w] {2,3}$ "; PrivateString mailserver; PrivateString Sender; Privatestring[] receiver; PublicSendMail () {} Public voidsetmailbasicinfo (String mailserver,string sender,string receiver) { This. MailServer =mailserver; This. Sender =Sender; This. Receiver =receiver.split (","); } Public voidsetmailbasicinfo (String mailserver,string sender,string[] users) { This. MailServer =mailserver; This. Sender =Sender; This. Receiver =users; } Public voidSetmailbasicinfo (String mailserver,string sender,list<string>users) { This. MailServer =mailserver; This. Sender =Sender; This. Receiver =Newstring[users.size ()]; Users.toarray ( This. Receiver); } Public BooleanSend (string subject, string content, list<string>fileNames) {Subject= subject==NULL?"": Subject; Content= content==NULL?"": Content; Properties Props=NewProperties (); Props.put ("Mail.smtp.host", mailserver); Session Session= Session.getinstance (Props,NULL); Try{internetaddress[] receiver=getreceiverlist (); if(Receiver.length = = 0) {System.out.println ("Receiver is null"); return false; } mimemessage msg=NewMimeMessage (session); Msg.setfrom (Newinternetaddress (sender)); Msg.setrecipients (Message.RecipientType.TO, receiver); Msg.setsubject (subject); Msg.setsentdate (NewDate ()); Multipart Container=NewMimemultipart (); MimeBodyPart Textbodypart=NewMimeBodyPart (); Textbodypart.setcontent (Content.tostring (),"TEXT/HTML;CHARSET=GBK"); Container.addbodypart (Textbodypart); Doattachfile (Container,filenames); Msg.setcontent (container); System.out.println ("Begin Send Mail"); Transport.send (msg); System.out.println ("Send mail Success"); } Catch(messagingexception e) {System.out.println ("Send Mail Fail"); System.out.println (e); return false; } Catch(Exception e) {return false; } return true; } Public BooleanSend (string subject, string content) {returnSend (Subject,content,NULL); } Public BooleanSend (String subject) {returnSend (Subject,NULL); } Private voidDoattachfile (Multipart container, list<string> fileNames)throwsmessagingexception{if(filenames==NULL|| Filenames.size () ==0) return; for(String filename:filenames) {File F=NewFile (filename); if(!f.isfile ())Continue; System.out.println ("The Attach file is:" +filename); MimeBodyPart Filebodypart=NewMimeBodyPart (); Filedatasource FDS=NewFiledatasource (f);//address of the attachment to be sentFilebodypart.setdatahandler (NewDataHandler (FDS)); Filebodypart.setfilename (Fds.getname ());//set the name of the attachmentContainer.addbodypart (Filebodypart); } } PrivateInternetaddress[] Getreceiverlist ()throwsaddressexception {ArrayList<InternetAddress> toList =NewArraylist<internetaddress>(); for(inti = 0; i < receiver.length; ++i) {if(Receiver[i].matches (Mail_address_regex)) {Tolist.add (Newinternetaddress (Receiver[i])); } } return(internetaddress[]) Tolist.toarray (Newinternetaddress[tolist.size ()]); }}
Examples of Use
String host = "168.xx.xx.xx";//address of the mail serverString subject = "Subject of sending mail"; String Sender= "[Email protected]"; List<String> receivers =NewArraylist<string>(); Receivers.add ("[Email protected]"); Receivers.add ("[Email protected]"); String content= "Mail Subject"; SendMail SendMail=NewSendMail (); Sendmail.setmailbasicinfo (host, sender, receivers); Sendmail.send (subject, content,NULL);//No AttachmentsThe body can also be HTML content, such as String contents= ";
Java network programming: Using Java Mail packages to send e-mail