[Java] JavaMail sending HTML-formatted messages with attachments

Source: Internet
Author: User

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

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.