Problems with Spring sending messages with inline images

Source: Internet
Author: User
Tags mailmessage

Question 1:spring Send HTML-formatted messages with pictures?

Solution 1: Add the content directly inside the send

This method is the simplest way to send a message with a picture, but there is a problem: Some mailboxes will have a server address in the mail to the trash , resulting in the message can not receive, so continue the whole.

Solution 2: Use the spring band's mimemessagehelper and say it without a clear view of the code directly:

1  Public classMyClass {2 3      Public Static voidSendhtmlimgemail (string subject, String sendhtml, String Touser, String Ccuser, String bccuser,4 file[] Attachment) {5         Try {6             //"1"7sendhtml = sendhtml + ";8Javamailsenderimpl Senderimpl =NewJavamailsenderimpl ();9             //Setting up mail Server "2"Ten             intPort = 25; OneSenderimpl.sethost ("Mailbox Host"); A Senderimpl.setport (port); -  -             //Create a mail message to send the difference between a simple message and an HTML message theMimeMessage MailMessage =senderimpl.createmimemessage (); -             //Note that the boolean here is equal to the real time to nest the picture, when the Mimemessagehelper is constructed, the given value is true to enable multipart mode -             //The last parameter "Utf-8" avoids the message Chinese garbled question "3" -Mimemessagehelper Messagehelper =NewMimemessagehelper (MailMessage,true, "Utf-8"); +  -             //set up multiple cc addresses +             if(NULL! = Ccuser &&!Ccuser.isempty ()) { A@SuppressWarnings ("Static-access") atinternetaddress[] INTERNETADDRESSCC =Newinternetaddress (). Parse (ccuser); - MESSAGEHELPER.SETCC (INTERNETADDRESSCC); -             } -             //set multiple secret delivery addresses -             if(NULL! = Bccuser &&!Bccuser.isempty ()) { -@SuppressWarnings ("Static-access") ininternetaddress[] INTERNETADDRESSBCC =Newinternetaddress (). Parse (bccuser); - MESSAGEHELPER.SETBCC (INTERNETADDRESSBCC); to             } +             //Add the contents of an attachment -             if(NULL! = Attachment && Attachment.length! = 0) { the                  for(File file:attachment) { *DataSource Source =Newfiledatasource (file); $ messagehelper.addattachment (File.getname (), source);Panax Notoginseng                 } -             } the             if(NULL! = Touser &&!Touser.isempty ()) { +                 //The delimiter here can be customized to send to multiplayer "4" . Astring[] tousers = Touser.split (","); the Messagehelper.setto (tousers); +             } -             //set recipient, sender $Messagehelper.setfrom ("Sender name"); $ Messagehelper.setsubject (subject); -             //true to start HTML-formatted messages -Messagehelper.settext (sendhtml,true); the              -             //This can be done directly in the main method, but after deployment, the error cannot be found. Path "5"Wuyi             //String imagepath= "Src/main/resources/config/emailtag.png"; the              -             //This gets the full path of the project "6" WuString Abpath =NewMyClass (). GetClass (). getClassLoader (). GetResource ("/"). GetPath (); -  AboutString imagepath=abpath+ "Config/emailtag.png"; $              -Filesystemresource img =NewFilesystemresource (imagePath); -  -             //This is the ID of the image corresponding to the CID "7" in the image tag above AMessagehelper.addinline ("Imprtant", IMG); +Senderimpl.setusername ("Mailbox User name"); theSenderimpl.setpassword ("Your email password"); -Properties prop =NewProperties (); $             //This must be added when using Outlook send an error that,,, forgot "8" theProp.put ("Mail.smtp.starttls.enable", "true"); the senderimpl.setjavamailproperties (prop); the             //Send mail the senderimpl.send (mailmessage); -System.out.println ("Mail sent successfully! "); in}Catch(Exception e) { the e.printstacktrace (); the         }  About     } the}

Explain:

"1": note cid This parameter refers to the image of the logo, see "7", must be the same

"2": Set the mail server address and port number note that the Outlook port number is 587

"3": Send Chinese garbled, add encoding format utf-8

"4": Mail sent to multiple people

"5": This can not be found after the deployment of the path to know the warrior to enlighten

"6": This I saw the next project deployed on the download server path, and then think of this method to get to the absolute path in addition to their own picture in the path of success (this toss for a long time, has not found the path)

"7": corresponding to "1"

"8": This use Microsoft mailbox when error, com.sun.mail.smtp.smtpsendfailedexception:530 5.7.57 SMTP; Client is not authenticated to send anonymous mail during mail from

By adding this property, you can

Summarize:

This e-mail method, can take attachments, send more than one person, add inline images, HTML content, add attachments are not tested, the others are tested, but if you do not set "1" "7", the picture will automatically become an attachment sent.

Problems with Spring sending messages with inline images

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.