Java SPRINGMVC Mail sending configuration and code

Source: Internet
Author: User

The first is the configuration in spring: (I used the freemarker template engine)

<!--mail delivery implementation class-->    <bean id= "Javamailsender"  class= " Org.springframework.mail.javamail.JavaMailSenderImpl ">        < Property name= "Host"  value= "smtp.qq.com"/>        < Property name= "Port"  value= "/>        <property"  name= "username"  value= "Your Mailbox"/>        <property  Name= "Password"  value= "Your mailbox password"/>        <property name = "Javamailproperties" >            <props  >                < prop key= "Mail.smtp.auth" >true</prop>             </props>        </property>    </bean>     <bean id= "Simplemailmessage"  class= "Org.springframework.mail.SimpleMailMessage" >         <property name= "from"  value= "sender"  />         <property name= "Subject"  value= "Mail Subject"  />         <!--        <property  name= "text"  value= "Mail Content"  />        -->     </bean> <!--thread pool asynchronously sends mail-->    <bean id= " Taskexecutor " class=" Org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor ">         <property name= "Corepoolsize"  value= "5"  />         <p roperty name= "Maxpoolsize"  value= "ten"  />         <property name= "queuecapacity"  value= " />    </bean> "    <!--freemarker Template-->    <bean id= "Freemarkerconfigurer"  class= "Org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" >         <property name= "Templateloaderpath"  value= "Classpath:template"  />         <!--<property name= "Templateloaderpath"  value= "/template"  />-->        <property name= " Freemarkersettings ">            <props>                 <prop  key= "Locale" >zh_cn</prop>                <prop  key= "Default_encoding" >UTF-8</prop>             </props>        </property>     </bean>

Tool classes for sending messages: (using annotations as a class for managing mail delivery)

public class mailsender {     @Inject     private  javamailsender javamailsender;     @Inject     private  simplemailmessage simplemailmessage;     @Inject     private  freemarkerconfigurer freemarkerconfigurer;     @Inject     private  TaskExecutor taskExecutor;    /**     *  Build mail content, Send the message.      *  @param  user   users      *  @param  url    links      */    public void  Send (User user, string url,string email)  {         string nickname = user.getnickname ();         string to = email;        string text =  "";         map<string, string> map = new hashmap<string,  String> (1);         map.put ("url",  url);         try {//              generate message content from Freemarker templates              Template template = freemarkerconfigurer.getconfiguration (). GetTemplate ("REGISTER_MAIL.FTL");//              template with ${xxx}, the value of key XXX in map replaces placeholder content.             text =  Freemarkertemplateutils.processtemplateintostring (Template, map);         } catch  (ioexception e) &nbsP {            e.printstacktrace ();         } catch  (templateexception e)  {             e.printstacktrace ();         }        this.taskexecutor.execute (New SendMailThread (To,null,text));     }    //     internal threading class that asynchronously sends messages using the thread pool.     private class SendMailThread implements Runnable {         private String to;         private String subject;        private  String content;        private sendmailthread (String to , string subject, string content)  {            super ();             this.to = to;             this.subject = subject;             this.content = content;         }         @Override          public void run ()  {             sendmail (to, subject, content);         }    }    /**     *  Send mail      *  @param  to         recipient mailbox      *  @param subject    Mail subject      *  @param  content     e-mail content      */    public void sendmail (String  To, string subject, string content)  {         Try {            mimemessage message  = javamailsender.createmimemessage ();             mimemessagehelper messagehelper = new mimemessagehelper (Message, true,   "UTF-8");             messagehelper.setfrom ( Simplemailmessage.getfrom ());            if  ( Subject != null)  {                 messageHelper.setsubject (subject);             } else  {                 Messagehelper.setsubject (Simplemailmessage.getsubject ());             }             Messagehelper.setto (To);             Messagehelper.settext (content, true);            Javamailsender.send (message);        } catch  ( Messagingexception e)  {             E.printstacktrace ();         }    }}

Service Layer Invocation:

1. To inject the Send mail tool class

@Inject private MailSender MailSender;

2. Calling code

User user = (user) request.getsession (). getattribute ("user"); String URL = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () + Request.getcont        Extpath (); Mailsender.send (User,url,email);


Java SPRINGMVC Mail sending configuration and code

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.