The message body and attachments sent by JavaMail are independent of each other, but the built-in picture needs to position the picture in the body, so the built-in picture and the message body are interdependent.
Send a message with an attachment to refer to JavaMail sending an HTML-formatted message with an attachment.
Send plain text messages with reference to the JavaMail simple case.
Specific examples
Emailhelper, email help class, to help the class to provide the SMTP server domain name, user name, password, sender mailbox, recipient mailbox, message subject, HTML format content, picture path, you can send a built-in picture of the message. When creating Mimemultipart, you need to pass in the parameter related and declare the position of the picture in the body.
Sendemaildemo, the demo sends the message.
Emailhelper.java
1 PackageMail;2 3 Importjava.util.Properties;4 5 ImportJavax.activation.DataHandler;6 ImportJavax.activation.DataSource;7 ImportJavax.activation.FileDataSource;8 ImportJavax.mail.BodyPart;9 ImportJavax.mail.Message;Ten Importjavax.mail.MessagingException; One ImportJavax.mail.Multipart; A Importjavax.mail.PasswordAuthentication; - Importjavax.mail.Session; - ImportJavax.mail.Transport; the Importjavax.mail.internet.AddressException; - Importjavax.mail.internet.InternetAddress; - ImportJavax.mail.internet.MimeBodyPart; - ImportJavax.mail.internet.MimeMessage; + ImportJavax.mail.internet.MimeMultipart; - + Public classEmailhelper { A at PrivateString host; - PrivateString username; - PrivateString password; - PrivateString from; - - PrivateString to; in PrivateString subject; - PrivateString htmlcontent; to PrivateString ImagePath; + - PublicEmailhelper (string host, string username, string password, string from)throwsaddressexception, messagingexception{ the This. Host =host; * This. Username =username; $ This. Password =password;Panax Notoginseng This. from =from ; - } the + Public voidSendwithimage ()throwsException { A theProperties props =NewProperties (); +Props.put ("Mail.smtp.auth", "true"); -Props.put ("Mail.smtp.host", host); $ $ FinalString username1 =username; - FinalString Password1 =password; - theSession session = Session.getinstance (props,NewJavax.mail.Authenticator () { - protectedpasswordauthentication getpasswordauthentication () {Wuyi return Newpasswordauthentication (username1, password1); the } - }); Wu -Message message =NewMimeMessage (session); About $Message.setfrom (Newinternetaddress (from)); - - message.setrecipients (Message.RecipientType.TO, Internetaddress.parse (To)); - A Message.setsubject (subject); + theMultipart Multipart =NewMimemultipart ("related"); - $SYSTEM.OUT.PRINTLN ("HTML"); theBodyPart Htmlpart =NewMimeBodyPart (); theHtmlcontent = "htmlcontent; theHtmlpart.setcontent (htmlcontent, "text/html"); the Multipart.addbodypart (htmlpart); - inSystem.out.println ("image"); theSYSTEM.OUT.PRINTLN ("Image path:" +ImagePath); theBodyPart Imgpart =NewMimeBodyPart (); AboutDataSource FDS =NewFiledatasource ( This. ImagePath); the theImgpart.setdatahandler (NewDataHandler (FDS)); theImgpart.setheader ("Content-id", "<image>"); + - Multipart.addbodypart (imgpart); the message.setcontent (multipart);Bayi transport.send (message); the theSystem.out.println ("Sent-|"); - } - the Public voidSetto (String to) { the This. to =to ; the } the - Public voidSetsubject (String subject) { the This. Subject =subject; the } the 94 Public voidsethtmlcontent (String htmlcontent) { the This. htmlcontent =htmlcontent; the } the 98 PublicString Getimagepath () { About returnImagePath; - }101 102 Public voidsetImagePath (String imagePath) {103 This. ImagePath =ImagePath;104 } the}
Sendemaildemo.java
1 Public classSendemaildemo {2 3 Public Static voidMain () {4 5String host = "smtp.163.com";//Use your SMTP server host6 7 FinalString username = "[email protected]";//Use your username8 FinalString password = "password";//Use your password9 TenString from = "[email protected]";//Use your sender email address One AString to = "[email protected]";//Use your reciever email address - Try { -Emailhelper Emailhelper =NewEmailhelper (host, username, password, from); the Emailhelper.setto (to); -Emailhelper.setsubject ("Subject TTT test"); -Emailhelper.sethtmlcontent ("); -Emailhelper.setimagepath ("/users/grs/documents/java/mavenemail/test/src/main/resource/promises.png"); + - emailhelper.send (); + A}Catch(Exception e) { at e.printstacktrace (); - } - } -}
Resources
JavaMail api-sending Email with Inline imagess
[Java] JavaMail sending HTML-formatted messages with pictures