Send Spring mail and send spring mail

Source: Internet
Author: User

Send Spring mail and send spring mail
Preface:In the past, emails were directly sent using Java's built-in email tools. Now Spring has helped us encapsulate them, javaMailSender, a simpler email sending tool, is provided. We will not talk about setting the email server here. Simply go to QQ mail to set it. The following describes the main steps: step 1. Add Maven dependencies for sending emails

<! -- Spring mail --> <dependency> <groupId> org. springframework </groupId> <artifactId> spring-context-support </artifactId> <version> 4.3.2.RELEASE </version> </dependency> <groupId> javax. mail </groupId> <artifactId> mail </artifactId> <version> 1.4.7 </version> </dependency>

 

Step 2, add the relevant Spring configuration, create a spring-mail.xml (if it is Spring-Boot please ignore), the content is as follows:

<? 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" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-co Ntext. xsd "> <! -- Spring JavaMailSender --> <bean class = "org. springframework. mail. javamail. JavaMailSenderImpl"> <property name = "host" value = "smtp.qq.com"> </property> <! -- Maybe you can use port 465, the default value of 25 is not suitable --> <property name = "port" value = "587"/> <property name = "protocol" value = "smtp"/> <property name =" username "value =" ****** @ qq.com "> </property> <! -- Here, after you send the SMTP activation text message in the QQ mailbox, authorization code obtained --> <property name = "password" value = "******"> </property> <property name = "defaultEncoding" value = "UTF-8 "/> <property name = "javaMailProperties"> <props> <prop key = "mail. smtp. auth "> true </prop> <prop key =" mail. smtp. timeout "> 25000 </prop> </props> </property> </bean> </beans>

 

Step 3: Create a mail sending Tool

/*** Spring-based JavaMailSender tool for sending emails * @ author simon **/@ Componentpublic class EmailSender {@ Autowired private JavaMailSender javaMailSender; private static EmailSender emailSender; @ PostConstruct public void init () {emailSender = this; emailSender. javaMailSender = this. javaMailSender;}/*** send a simple text email * @ param sendTo recipient group * @ param subject topic * @ param content text content */public static void sendSimpleMessage (String sendFrom, string [] sendTo, String subject, String textcontent) {SimpleMailMessage mail = new SimpleMailMessage (); mail. setFrom (sendFrom); mail. setTo (sendTo); mail. setSubject (subject); mail. setText (textcontent); // send emailSender. javaMailSender. send (mail );} /*** send an email in HTML format * @ param sendFrom * @ param sendTo recipient group * @ param subject topic * @ param htmlContent HTML content * @ throws Exception */public static void sendHtmlMessage (String sendFrom, string [] sendTo, String subject, String htmlContent) throws Exception {MimeMessage mimeMessage = emailSender. javaMailSender. createMimeMessage (); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper (mimeMessage); mimeMessageHelper. setFrom (sendFrom); mimeMessageHelper. setTo (sendTo); mimeMessageHelper. setSubject (subject); // true indicates starting an HTML-format email mimeMessageHelper. setText (htmlContent, true); // send the email emailSender. javaMailSender. send (mimeMessage );}}

 

Step 4: create a unit test and send an email

/*** Mail sending test class ** @ author simon **/@ RunWith (SpringJUnit4ClassRunner. class) @ ContextConfiguration ("classpath: applicationContext. xml ") public class EmailSenderTest {@ Test public void testSend () throws Exception {String sendFrom =" 1317492210@qq.com "; String [] sendTo = {" zhaosheng@hitencent.com "}; string subject = "HTML mail sent by JavaMailSender in Spring"; StringBuilder htmlContent = new StringBuilder (). app End ("

 

OK, all the steps here are over. Isn't it easier and easier than Java Mail? Please try it on your own!

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.