This case presentation is sent in HTML format and can be sent with attachments. Examples of sending plain text messages can refer to the previous post JavaMail simple case.
Emailhelper, e-mail help class, to help class to provide the SMTP server domain name, user name, password, sender mailbox, recipient mailbox, message subject, HTML-formatted content (optional), attachments (optional), you can send a copy of the message.
Sendemaildemo, the demo sends the message.
Importjava.util.Properties;ImportJavax.activation.DataHandler;ImportJavax.activation.DataSource;ImportJavax.activation.FileDataSource;ImportJavax.mail.BodyPart;ImportJavax.mail.Message;Importjavax.mail.MessagingException;ImportJavax.mail.Multipart;Importjavax.mail.PasswordAuthentication;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 classEmailhelper {PrivateString host; PrivateString username; PrivateString password; PrivateString from; PrivateString to; PrivateString subject; PrivateString htmlcontent; PrivateString Attachedfilename; PublicEmailhelper (string host, string username, string password, string from)throwsaddressexception, messagingexception{ This. Host =host; This. Username =username; This. Password =password; This. from =From ; } Public voidSend ()throwsexception{Properties Props=NewProperties (); Props.put ("Mail.smtp.auth", "true"); Props.put ("Mail.smtp.host", host); FinalString username1 =username; FinalString Password1 =password; Session Session=session.getinstance (Props,NewJavax.mail.Authenticator () {protectedpasswordauthentication getpasswordauthentication () {return Newpasswordauthentication (username1, Password1); } }); Message Message=NewMimeMessage (session); Message.setfrom (Newinternetaddress (from)); Message.setrecipients (Message.RecipientType.TO, Internetaddress.parse (to)); Message.setsubject (subject); Multipart Multipart=NewMimemultipart (); if(Htmlcontent! =NULL) {System.out.println ("Has HTML"); BodyPart Htmlpart=NewMimeBodyPart (); Htmlpart.setcontent (Htmlcontent,"Text/html"); Multipart.addbodypart (Htmlpart); } if(Attachedfilename! =NULL) {System.out.println ("has attached file"); BodyPart Attchmentpart=NewMimeBodyPart (); DataSource Source=NewFiledatasource (attachedfilename); Attchmentpart.setdatahandler (NewDataHandler (source)); Attchmentpart.setfilename (Attachedfilename); Multipart.addbodypart (Attchmentpart); } message.setcontent (multipart); Transport.send (message); System.out.println ("Sent"); } Public voidSetto (String to) { This. to =to ; } Public voidSetsubject (String subject) { This. Subject =subject; } Public voidsethtmlcontent (String htmlcontent) { This. htmlcontent =htmlcontent; } Public voidsetattachedfilename (String attachedfilename) { This. Attachedfilename =Attachedfilename; }}
Demo Code,
Public classSendemaildemo { Public Static voidMain () {String host= "Smtp.163.com"; Use your SMTP server hostFinalString username = "[email protected]";//Use your usernameFinalString password = "Password"; Use your password String from= "[Email protected]"; Use your sender e-mail address String to= "[Email protected]"; Use your reciever email addressTry{emailhelper Emailhelper=NewEmailhelper (host, username, password, from); Emailhelper.setto (to); Emailhelper.setsubject ("Subject TTT Test"); Emailhelper.sethtmlcontent ("); Emailhelper.setattachedfilename ("/users/grs/documents/java/mavenemail/test/src/main/resource/attachment3.txt"); Emailhelper.send (); } Catch(Exception e) {e.printstacktrace (); } }}
Resources
Java-sending Email
[Java] JavaMail sending HTML-formatted messages with attachments