Spring send a summary of the message (attached source)

Source: Internet
Author: User

Do the project with automatic e-mail function, online access to a lot of no special detailed instructions, need to explore, do a lot of detours.

Let's share your code here:

360 Network disk:Http://yunpan.cn/cJzDQ3gVUHBxY access password 8221



When using, modify the user name, password, mailbox server, and port in the spring configuration file if it is not a yeah mailbox.


If you are using a MAVEN project:

P Om.xml

What to add

<!--Spring3--><dependency><groupid>org.springframework</groupid><artifactid> Spring-core</artifactid><version>3.2.4.release</version></dependency><dependency ><groupId>org.springframework</groupId><artifactId>spring-context</artifactId>< Version>3.2.4.release</version></dependency><dependency><groupid> org.springframework</groupid><artifactid>spring-jdbc</artifactid><version>3.2.4. Release</version></dependency><dependency><groupid>org.springframework</groupid> <artifactId>spring-web</artifactId><version>3.2.4.RELEASE</version></dependency> <dependency><groupid>org.springframework</groupid><artifactid>spring-aop</ artifactid><version>3.2.4.release</version></dependency><!--Spring Email-->< Dependency> <groupid>org.springframeworK</groupid> <artifactId>spring-context-support</artifactId> <version>3.2.4.release</ version></dependency><!--Javamail API-<dependency> <groupid>javax.mail</groupid > <artifactId>mail</artifactId> <version>1.4.4</version></dependency>


If you import directly jar mode, you need to introduce the following jar package


Emailutil

Package Com.bookmarksclouds.util;import Java.io.file;import Javax.annotation.resource;import Javax.mail.messagingexception;import Javax.mail.internet.mimemessage;import Org.springframework.core.io.classpathresource;import Org.springframework.core.io.filesystemresource;import Org.springframework.mail.simplemailmessage;import Org.springframework.mail.javamail.javamailsender;import Org.springframework.mail.javamail.mimemessagehelper;import org.springframework.stereotype.Component; @Component (  "Simplemail") public class Emailutil {private Javamailsender javamailsender;      Private Simplemailmessage simplemailmessage;        /** * @ Method Name: SendMail * @ parameter name: @param subject Message subject * @ parameter name: @param content Message Subject Contents * @ Parameter name: @param to     Recipient email Address * @param ishtml is HTML format (UTF-8 encoding when sending HTML) * @ Description: Send Message * @throws messagingexception Send an exception               */public void SendMail (string subject, String Content,boolean ishtml, String to) throws Messagingexception {             MimeMessage mimemessage = Javamailsender.createmimemessage ();            Mimemessagehelper Messagehelper =null;            if (ishtml) {messagehelper = new Mimemessagehelper (mimemessage,true, "UTF-8");            } else {messagehelper = new Mimemessagehelper (mimemessage,true); } messagehelper.setfrom (Simplemailmessage.getfrom ()); Set Sender email Messagehelper.setsubject (subject);   Set the message subject if (ishtml) {messagehelper.settext (content,true);   Set Message subject content (HTML format)} else {Messagehelper.settext (content);          Set message subject content} Messagehelper.setto (to);      Set Recipient email javamailsender.send (mimemessage);    /** * Send mail (with attachments) * @param subject Subject * @param content * @param fileattachment attachment file *@param ishtml Whether the content is in HTML format * @param to the email address sent by * @throws messagingexception Send Message exception (FAILED) */public void SendMail (                            String subject, String Content,boolean ishtml, File fileattachment,string to) throws Messagingexception {                      MimeMessage mimemessage = Javamailsender.createmimemessage ();                        Mimemessagehelper messagehelper = new Mimemessagehelper (mimemessage,true, "UTF-8"); Messagehelper.setfrom (Simplemailmessage.getfrom ()); Set Sender email Messagehelper.setsubject (subject);   Set the message subject if (ishtml) {messagehelper.settext (content,true);   Set Message subject content (HTML format)} else {Messagehelper.settext (content);          Set message subject content} Messagehelper.setto (to);                       Set Recipient Email Filesystemresource file = new Filesystemresource (fileattachment); Messagehelper.addattachment (File.getfilename (), file); Add toAnnex Javamailsender.send (MimeMessage); /** * Send mail (with attachments) * @param subject Subject * @param content Contents * @param classpathresource Attachment file (attached to the item Internal time) * @param ishtml content is HTML format * @param to email address * @throws messagingexception */public void SENDMA Il (string subject, String Content,boolean ishtml, Classpathresource classpathresource,string to) throws Messagingexcept          Ion {MimeMessage mimemessage = Javamailsender.createmimemessage (); /** * New Mimemessagehelper (Mimemessage,true) True Personal Insights: * For true parameters, the official document explains this: * Use T He true flag to indicate you need a multipart message * translated is: Use True to indicate that you need multiple messages * and then go over the MIMEMESSAG Ehelper API, find such a sentence: * Supporting alternative texts, inline elements and attachments * that is, if you want to support internal Inline elements and attachments must be given a second argument of true * otherwise thrown java.lang.IllegalStateException exception */MImemessagehelper messagehelper = new Mimemessagehelper (mimemessage,true, "UTF-8"); Messagehelper.setfrom (Simplemailmessage.getfrom ()); Set Sender email Messagehelper.setsubject (subject);   Set the message subject if (ishtml) {messagehelper.settext (content,true);   Set Message subject content (HTML format)} else {Messagehelper.settext (content);          Set message subject content} Messagehelper.setto (to);                       Set Recipient Email/** filesystemresource file = new Filesystemresource (fileattachment); * Classpathresource: Obviously is the classpath resource, I here is the attachment is in the project, so need to use Classpathresource * If the system file resources can not use Classpathresource, but to use Filesys Temresource, Example: * classpathresource file = new Classpathresource ("Attachment/readme.txt"              ); *//** * Mimemessagehelper AddAttachment Method: * AddAttachment (String attachmentFileName, in Putstreamsource InputstreaMsource) * Inputstreamsource is an interface, Classpathresource and Filesystemresource all implement this interface             Send Attachment message */classpathresource file = Classpathresource; Messagehelper.addattachment (File.getfilename (), file);              Add Attachment Javamailsender.send (mimemessage);        }//spring Dependency Injection @Resource public void Setsimplemailmessage (Simplemailmessage simplemailmessage) {    This.simplemailmessage = Simplemailmessage; }//spring Dependency Injection @Resource public void Setjavamailsender (Javamailsender javamailsender) {This.javamailse    NDEr = Javamailsender; }}

Demo

Package Com.bookmarksclouds.test;import Javax.mail.messagingexception;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.bookmarksClouds.util.EmailUtil; public class emailutiltest{Private ApplicationContext factory = new Classpathxmlapplicationcontext (New Strin G[] {"Spring/spring-smtp-mail-attachment.xml"}), @Testpublic void Test () {Emailutil mail = (emailutil) Factory.getbean ( "Simplemail"); String html= "

&NBSP; spring configuration file

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:       Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi:schemalocation= "Http://www.springframework.org/schema /beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org /schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.spring                      FRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.2.xsd "> <context:annotation-config/><context:component-scan base-package= "Com.bookmarksclouds"/> <bean id = "MailSender" class = "Org.springframework.mail.javamail.JavaMailSenderImpl" > <!--server--& Lt;property name = "Host" value = "smtp.163.com "/> <!--port number--<property name =" Port "value ="/> "<!           --User name--<property name = "username" value = "Email address"/> <!--password-- <property name = "Password" value = "Mailbox Password"/> <!--SMTP Server Authentication-<property name =  "Javamailproperties" > <props > <!--authentication--<prop  Key = "Mail.smtp.auth" > True </prop > </props > </property > </bean >        <bean id= "Simplemailmessage" class= "Org.springframework.mail.SimpleMailMessage" > <!--sender Email-- <property name= "from" value= "send email address"/> <!--recipient email <property name= "to" value= "[em        ail protected] "/> Email theme (title) <property name=" Subject "value=" subject "/> Email subject content <property name= "Text "> <value>ContentText</value> </property>-</bean> </b Eans>




Spring send a summary of the message (attached source)

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.