Spring Boot Learning (10) springboot Configuration Send Email

Source: Internet
Author: User
Tags starttls

Springboot Configure send email to introduce dependencies

To introduce the mail configuration in the Pom.xml file:

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId> Spring-boot-starter-mail</artifactid></dependency>

Configuration file

# Javamailsender Mail sent configuration spring.mail.host=smtp.163.comspring.mail.username= user 163 mailbox spring.mail.password= Email password spring.mail.properties.mail.smtp.auth=truespring.mail.properties.mail.smtp.starttls.enable= Truespring.mail.properties.mail.smtp.starttls.required=true

Note: If you use QQ mailbox to send mail, you need to change to spring.mail.host=smtp.qq.com , at the same time spring.mail.password change to QQ mailbox authorization code.
QQ Mailbox--Settings---account->POP3/SMTP service: QQ Authorization code will be obtained after opening service


However, when the program is actually running, it will still explode 535 authentication failure.

Solution : Because of the jre\lib\security replacement of two jar packages in JDK1.8. The downloaded local_policy.jar and US_export_policy.jar replaced jre\lib\security folders can be JDK1.8.
Address: http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html

Send a simple text message

Paste Java code:

@RunWith (Springrunner.class) @SpringBootTest @activeprofiles ("163") public class Emailtest {    @Autowired    Private Javamailsender MailSender; Automatically injected bean    @Value ("${spring.mail.username}")    private String Sender;//Read parameters in the configuration file    @Test    Public void Sendsimplemail () throws Exception {        Simplemailmessage message = new Simplemailmessage ();        Message.setfrom (Sender);        Message.setto (Sender); Send yourself an email        message.setsubject ("Subject: Simple Mail");        Message.settext ("test message content");        Mailsender.send (message);}    }

Send an HTML message

Java code:

   @Test public    void Sendhtmlmail () {        mimemessage message = NULL;        try {            message = Mailsender.createmimemessage ();            Mimemessagehelper helper = new Mimemessagehelper (message, true);            Helper.setfrom (Sender);            Helper.setto (Sender);            Helper.setsubject ("title: Sending HTML Content");            StringBuffer sb = new StringBuffer ();            Sb.append ("

Send a message with an attachment

Java code:

  @Test public    void Sendattachmentsmail () {        mimemessage message = NULL;        try {            message = Mailsender.createmimemessage ();            Mimemessagehelper helper = new Mimemessagehelper (message, true);            Helper.setfrom (Sender);            Helper.setto (Sender);            Helper.setsubject ("Subject: Mail with Attachments");            Helper.settext ("Mail content with attachments");            Note The project path problem, the auto-complement project path            filesystemresource file = new Filesystemresource ("Src/main/resources/static/image /picture.jpg "));            Add Mail            helper.addattachment ("picture. jpg", file),        } catch (Exception e) {            e.printstacktrace ();        }        Mailsender.send (message);    }

Send a message with a static resource

Java code:

    @Test public    void Sendinlinemail () {        mimemessage message = NULL;        try {            message = Mailsender.createmimemessage ();            Mimemessagehelper helper = new Mimemessagehelper (message, true);            Helper.setfrom (Sender);            Helper.setto (Sender);            Helper.setsubject ("Subject: Mail with static resources");            The second parameter specifies that the HTML format is sent, while the CID: is a fixed notation            helper.settext ("

Send Template message

The template engine can also be used in spring boot to implement templated mail delivery. With regard to template mail, springboot originally supported velocity, and abandoned velocity after the 1.4 release, temporarily supporting only freemaker.
Introducing Freemaker Dependencies:

      <dependency>          <groupId>org.springframework.boot</groupId>          <artifactId> Spring-boot-starter-freemarker</artifactid>      </dependency>

Java code:

   @Autowired private Freemarkerconfigurer Freemarkerconfigurer;        Automatic injection @Test public void Sendtemplatemail () {mimemessage message = NULL;            try {message = Mailsender.createmimemessage ();            Mimemessagehelper helper = new Mimemessagehelper (message, true);            Helper.setfrom (Sender);            Helper.setto (Sender);            Helper.setsubject ("Subject: Template Mail");            map<string, object> model = new Hashedmap ();            Model.put ("username", "zggdczfr");            Modify the read path in the Application.properties file//Freemarkerconfigurer Configurer = new Freemarkerconfigurer ();//            Configurer.settemplateloaderpath ("Classpath:templates");            Read HTML template templates template = Freemarkerconfigurer.getconfiguration (). GetTemplate ("mail.html");            String html = freemarkertemplateutils.processtemplateintostring (template, model);        Helper.settext (HTML, true);          } catch (Exception e) {  E.printstacktrace ();    } mailsender.send (message); }

Effect

Spring Boot Learning (10) springboot Configuration Send Email

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.