sending mail in spring is an easy task, and it's much easier to send it using velocity as a template for mail. First of all, or simply describe the configuration in sping, send mail needs a mail engin:<bean id= "Mailengine"class= "Com.aodragon.jdt.backend.service.impl.MailEngineImpl" > <property name= "MailSender" > <ref local= "Mail Sender "/> </property> <property name=" Velocityengine "> <ref local=" velocityengine "/> &L T;/property> </bean>The following classes are implemented: PackageCom.aodragon.jdt.backend.service.impl;ImportJava.util.Map;Importjavax.mail.MessagingException;ImportJavax.mail.internet.MimeMessage;Importorg.apache.commons.logging.*;ImportOrg.apache.velocity.app.VelocityEngine;Importorg.apache.velocity.exception.VelocityException;ImportOrg.springframework.core.io.ClassPathResource;ImportOrg.springframework.mail.*;ImportOrg.springframework.mail.javamail.*;Importorg.springframework.ui.velocity.VelocityEngineUtils;ImportCom.aodragon.jdt.backend.service.MailEngine; Public classMailengineimplImplementsMailengine {protected Static FinalLog log = Logfactory.getlog (Mailengineimpl.class); PrivateMailSender MailSender; PrivateVelocityengine Velocityengine; Public voidSetmailsender (MailSender mailsender) { This. MailSender =MailSender; } Public voidsetvelocityengine (Velocityengine velocityengine) { This. Velocityengine =Velocityengine; } Public voidsendMessage (simplemailmessage msg, string templatename, Map model) {string result =NULL; Try{result=velocityengineutils.mergetemplateintostring (Velocityengine, templatename, model); } Catch(velocityexception e) {e.printstacktrace (); } msg.settext (Result); Send (msg); } Public voidsendMessage (simplemailmessage msg, string templatename, string encoding, Map model) {string result=NULL; Try{result=velocityengineutils.mergetemplateintostring (Velocityengine, templatename, Encoding,model); } Catch(velocityexception e) {e.printstacktrace (); } msg.settext (Result); Send (msg); } Public voidSend (Simplemailmessage msg) {Try{mailsender.send (msg); } Catch(Mailexception ex) {//log it and go onLog.error (Ex.getmessage ()); } } Public voidsendMessage (string[] emailaddresses, classpathresource resource, String BodyText, String subject, String attachmentname)throwsmessagingexception {mimemessage message=((Javamailsenderimpl) mailsender). Createmimemessage (); //Use the true flag to indicate need a multipart messageMimemessagehelper Helper=NewMimemessagehelper (Message,true); Helper.setto (emailaddresses); Helper.settext (BodyText); Helper.setsubject (subject); Helper.addattachment (AttachmentName, Resource); ((Javamailsenderimpl) mailsender). Send (message); }} This engin implements 4 methods, which are called when needed, and two objects are required:PrivateMailSender MailSender; PrivateVelocityengine Velocityengine, so you need to be aware of it when configuring:<bean id= "MailSender"class= "Org.springframework.mail.javamail.JavaMailSenderImpl" > <property name= "host" > <value>${email.hos t}</value> </property> <property name= "username" > <value>${email.username}</value> ; </property> <property name= "password" > <value>${email.password}</value> </PROPERTY&G T </bean><bean id= "Velocityengine"class= "Org.springframework.ui.velocity.VelocityEngineFactoryBean" > <property name= "velocityproperties" > <p rops> <prop key= "Resource.loader" >class</prop> <prop key= "Class.resource.loader.class" > org.apache.velocity.runtime.resource.loader.classpathresourceloader</prop> <prop key= " Velocimacro.library "/> </props> </property> </bean>so the configuration is done, and here's an example of a call to send mail in action PublicActionforward Save (actionmapping actionmapping, Actionform actionform, HttpServletRequest HttpServletRequest, HttpServletResponse httpservletrresponse) throwsException {String toemail= "..."HashMap Model=NewHashMap (); This. Smm.setto (Toemail); This. Smm.setsubject ("Thank you for your attention"); Model.put ("Content", "......." "); Messageresources Resources=getresources (HttpServletRequest); This. Mailengin.sendmessage ( This. Smm,resources.getmessage (_locale, "velocity.email.bookproduct"), model); returnlist (actionmapping, Actionform, HttpServletRequest, httpservletrresponse); } //End SaveIt is necessary to note that the 2nd parameter in the call SendMessage is Resources.getmessage (_locale,"Velocity.email.bookproduct"), here is to accommodate the multilanguage version, call the corresponding VM file, Velocity.email.bookproduct is the path to the VM files that are saved in each version of Applicationresource.properties. where _locale =(Locale) request.getsession (). getattribute (Globals.locale_key). In this way, the simple velocity combines spring to send an email, you can add a task to manage the queue of messages, let the system periodically send messages in the queue, manage various parameters in the MailSender, and so on.
Using velocity to send email with spring