PackageCom.syj; ImportJava.io.ByteArrayOutputStream; ImportJava.io.FileInputStream; Importjava.io.IOException; Importjava.util.Arrays; Importjava.util.Date; Importjava.util.Properties; ImportJavax.activation.DataHandler; ImportJavax.activation.FileDataSource; ImportJavax.mail.Authenticator; ImportJavax.mail.Message; Importjavax.mail.PasswordAuthentication; Importjavax.mail.Session; ImportJavax.mail.Transport; Importjavax.mail.internet.InternetAddress; ImportJavax.mail.internet.MimeMessage; ImportJavax.mail.BodyPart; ImportJavax.mail.Multipart; ImportJavax.mail.internet.MimeBodyPart; ImportJavax.mail.internet.MimeMultipart; ImportCom.sun.istack.internal.ByteArrayDataSource; /*** <P> * Title: Example of sending mail in Java * </P> * * <P> * Description: Send picture attachment and use the picture in HTML * </p& Gt * * <P> * Copyright:copyright (c) * </P> * *@authorSun Yu * @main [email protected] * @date June, 12:35:26 AM*/ Public classSendMail {Private StaticString username = "XXXX"; Private StaticString password = "xxxx"; Private StaticString smtpserver = "smtp.163.com"; Private StaticString frommailaddress = "[Email protected]"; Private StaticString tomailaddress = "[Email protected]"; Public Static voidMain (string[] args)throwsException {Properties props=NewProperties (); Props.put ("Mail.smtp.auth", "true"); Props.put ("Mail.smtp.host", SmtpServer); //get mail Session objectSession session =session.getdefaultinstance (Props,Newsmtpauthenticator (username, password)); /** *************************************************** */ //To create a MIME message objectMimeMessage MimeMessage =NewMimeMessage (session); Mimemessage.setfrom (NewInternetAddress (frommailaddress));//SenderMimemessage.setrecipient (Message.RecipientType.TO,Newinternetaddress (tomailaddress));//RecipientMimemessage.setsubject ("Subject"); Mimemessage.setsentdate (NewDate ());//Send DateMultipart MP =NewMimemultipart ("related");//related means that you can send HTML-formatted messages /** *************************************************** */bodypart BodyPart=NewMimeBodyPart ();//BodyBodypart.setdatahandler (NewDataHandler ("Test Test", "TEXT/HTML;CHARSET=GBK"));//Web page Format /** *************************************************** */bodypart Attachbodypart=NewMimeBodyPart ();//General AccessoriesFiledatasource FDS =NewFiledatasource ("C:/boot.ini"); Attachbodypart.setdatahandler (NewDataHandler (FDS)); Attachbodypart.setfilename ("=? GBK? B? " +NewSun.misc.BASE64Encoder (). Encode (Fds.getname (). GetBytes ())+ "?=");//address the attachment name in Chinese garbledMp.addbodypart (Attachbodypart); /** *************************************************** */MimeBodyPart Imgbodypart=NewMimeBodyPart ();//attachment icon byte[] bytes = ReadFile ("C:/button.gif"); Bytearraydatasource fileds=Newbytearraydatasource (Bytes,"Application/octet-stream"); Imgbodypart.setdatahandler (NewDataHandler (fileds)); Imgbodypart.setfilename ("Button.gif"); Imgbodypart.setheader ("Content-id", "</IMG1>");//Use this picture method in HTML src= "CID:IMG1"Mp.addbodypart (Imgbodypart); /** *************************************************** */Mp.addbodypart (bodypart); Mimemessage.setcontent (MP);//set up message content objectsTransport.send (MimeMessage);//Send mail } /*** Read File * *@paramfile * Path *@returnreturns a binary array*/ Public Static byte[] readFile (String file) {FileInputStream fis=NULL; Bytearrayoutputstream Bos=NULL; Try{FIS=Newfileinputstream (file); Bos=NewBytearrayoutputstream (); intBytesread; byteBuffer[] =New byte[1024 * 1024]; while((bytesread = fis.read (buffer))! =-1) {bos.write (buffer,0, Bytesread); Arrays.fill (buffer, (byte) 0); } } Catch(IOException E1) {e1.printstacktrace (); } finally { Try { if(Bos! =NULL) Bos.close (); } Catch(IOException e) {e.printstacktrace (); } } returnBos.tobytearray (); } } /*** SMTP Authentication*/ classSmtpauthenticatorextendsAuthenticator {String username=NULL; String Password=NULL; //SMTP Authentication PublicSmtpauthenticator (string Username, string password) { This. Username =username; This. Password =password; } Publicpasswordauthentication getpasswordauthentication () {return NewPasswordauthentication ( This. Username, This. Password); } }
Pop3_ using Java to send an illustrated HTML message