Send emails using the spring template and spring send emails.

Source: Internet
Author: User
Tags starttls

Send emails using the spring template and spring send emails.

1. jar package

Velocity-1.7.jar
Velocity-tools-2.0.jar
Mail. jar
Spring-context-support-4.0.6.RELEASE.jar

 

Ii. Configuration

<? 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: p = "http://www.springframework.org/schema/p" xmlns: context = "http://www.springframework.org/schema/context" xmlns: mvc = "http://www.springframework.org/schema/mvc" xsi: schemaLocation = "http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context /Spring-context-3.0.xsd "> <bean id =" velocityEngine "class =" org. springframework. ui. velocity. velocityEngineFactoryBean "> <property name =" velocityProperties "> <props> <prop key =" resource. loader "> file </prop> <prop key =" file. resource. loader. class "> org. apache. velocity. runtime. resource. loader. classpathResourceLoader </prop> <prop key = "file. resource. loader. cache "> false </prop> <prop key =" file. resource. load Er. modificationCheckInterval "> 3 </prop> <prop key =" input. UTF-8 </prop> <prop key = "output. encoding "> UTF-8 </prop> </props> </property> </bean> <! -- Mail implementation class --> <bean id = "javaMailSender" class = "org. springframework. mail. javamail. javaMailSenderImpl "> <property name =" host "value =" $ {mail. smtp. host} "/> <property name =" port "value =" $ {mail. smtp. port} "/> <property name =" username "value =" $ {mail. smtp. username} "/> <property name =" password "value =" $ {mail. smtp. password} "/> <property name =" javaMailProperties "> <props> <prop key =" mail. smtp. timeout ">$ {mail. smtp. timeout} </prop> <prop key = "mail. smtp. auth ">$ {mail. smtp. auth} </prop> <prop key = "mail. smtp. starttls. enable ">$ {mail. smtp. starttls. enable} </prop> <prop key = "mail. smtp. socketFactory. port ">$ {mail. smtp. port} </prop> <prop key = "mail. smtp. socketFactory. class "> javax.net. ssl. SSLSocketFactory </prop> <prop key = "mail. smtp. socketFactory. fallback "> false </prop> </props> </property> </bean> </beans>

Code

Package com. ljx. model; import java. util. hashMap; import java. util. map; import javax. mail. messagingException; import javax. mail. internet. mimeMessage; import org. apache. velocity. app. velocityEngine; import org. springframework. beans. factory. annotation. autowired; import org. springframework. mail. javamail. javaMailSenderImpl; import org. springframework. mail. javamail. mimeMessageHelper; import org. springframework. stereotype. component; import org. springframework. ui. velocity. dependencies; @ Componentpublic class SendTemplateMail {@ Autowired private JavaMailSenderImpl mailSender; @ Autowired private VelocityEngine velocityEngine; public void sendEmail () throws MessagingException {MimeMessage message = mailSender. createMimeMessage (); MimeMessageHelper helper = new MimeMessageHelper (message, true, "GBK"); helper. setFrom ("111111111@qq.com"); helper. setTo ("222222222@qq.com"); helper. setSubject ("test HTM topic"); Map <String, Object> model = new HashMap <String, Object> (); model. put ("userName", "xxx"); String mailText = VelocityEngineUtils. mergetemplate1_string (velocityEngine, "mail. vm "," GBK ", model); System. out. println (mailText); helper. setText (mailText, true); mailSender. send (message );}}

Template mail. vm

<! DOCTYPE html> 

Controllor

@RequestMapping(value = "/mail.action")    public @ResponseBody String mail() {        try {            System.out.println(1);            mail.sendEmail();        } catch (MessagingException e) {            e.printStackTrace();            return "false";        }        return "ok";    }

 

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.