Spring Boot: Mail Service

Source: Internet
Author: User

Introducing Dependencies

Add Spring-boot-starter-mail Package Reference

<dependency>      <groupId>org.springframework.boot</groupId>      <artifactId> Spring-boot-starter-mail</artifactid></dependency>
Configuration file
Spring:  Mail:    host:smtp.163.com    Username: User name    password: password    properties:      Mail:        SMTP:          auth:true          starttls:            enable:true            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

Send Plain text simple messages
    Private final Logger Logger = Loggerfactory.getlogger (This.getclass ());    @Autowired    private Javamailsender sender;    @Value ("${spring.mail.username}")    private  String from;    /**     * Send plain text Simple Mail     * @param to     * @param subject     * @param content */public    void Sendsimplemail (String to, string subject, string content) {        Simplemailmessage message = new Simplemailmessage ();        Message.setfrom (from);        Message.setto (to);        Message.setsubject (subject);        Message.settext (content);        try {            sender.send (message);            Logger.info ("Simple Mail has been sent. ");        } catch (Exception e) {            logger.error ("an exception occurred while sending a simple message! ", E);        }    }
Send HTML-formatted messages
     /**     * Send HTML-formatted messages     * @param to     * @param subject     * @param content     *    /public void Sendhtmlmail ( String to, string subject, string content) {        MimeMessage message = Sender.createmimemessage ();        try {            //true means that you need to create a multipart message            mimemessagehelper helper = new Mimemessagehelper (message, true);            Helper.setfrom (from);            Helper.setto (to);            Helper.setsubject (subject);            Helper.settext (content, true);            Sender.send (message);            Logger.info ("HTML Mail has been sent. ");        } catch (Messagingexception e) {            logger.error ("an exception occurred while sending an HTML message! ", E);        }    }
Send a message with an attachment
     /** * Send mail with attachments * @param to * @param subject * @param content * @param filePath */Public V OID Sendattachmentsmail (string to, string subject, string content, String filePath) {MimeMessage message = SENDER.C        Reatemimemessage (); Try {//true represents the need to create a multipart message mimemessagehelper helper = new Mimemessagehelper (message, True            );            Helper.setfrom (from);            Helper.setto (to);            Helper.setsubject (subject);            Helper.settext (content, true);            Filesystemresource file = new Filesystemresource (new file (FilePath));            String fileName = filepath.substring (Filepath.lastindexof (file.separator));            Helper.addattachment (fileName, file);   Helper.addattachment (fileName2, file2);            Add multiple attachment sender.send (message); Logger.info ("The message with the attachment has been sent.        "); } catch (Messagingexception e) {logger.error ("an exception occurred when sending messages with attachments!        ", e); }    }
Send messages embedded in static resources
/** * Send messages embedded in static resources (typically pictures) * @param to * @param subject * @param     Content message contents, need to include the ID of a static resource, for example:  * @param rscpath Static resource path and file name * @param rscid static resource ID        */public void Sendinlineresourcemail (string to, string subject, string content, String Rscpath, String rscid) {        MimeMessage message = Sender.createmimemessage (); Try {//true represents the need to create a multipart message mimemessagehelper helper = new Mimemessagehelper (message, True            );            Helper.setfrom (from);            Helper.setto (to);            Helper.setsubject (subject);            Helper.settext (content, true);            Filesystemresource res = new Filesystemresource (new File (Rscpath));            Helper.addinline (Rscid, RES);            Sender.send (message); Logger.info ("a message embedded in a static resource has been sent.        "); } catch (Messagingexception e) {logger.error ("an exception occurred when sending messages embedded in a static resource!        ", e); }    }
Writing Tests
@RunWith (springrunner.class) @SpringBootTestpublic class MailTest {@Autowired private mailservice mailservice;    Private String to = "[email protected]";    @Test public void Sendsimplemail () {Mailservice.sendsimplemail (To, "Subject: Simple Mail", "test message content");                } @Test public void Sendhtmlmail () {String content= "Effect

Project Reference Address

Https://github.com/ithobart/Spring-Boot-Examples

Spring Boot: Mail Service

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.