Spring Boot series (13) Spring Boot send mail

Source: Internet
Author: User
Tags starttls
Spring Boot series (13) Spring Boot send mail

This article describes the spring Boot send mail needs springboot combat full video tutorial, click here.


The spring framework uses the Javamailsender interface to provide a simple abstraction for sending messages, and spring boot provides it with an automatic configuration and a starter module.


If Spring.mail.host and the associated libraries (defined by Spring-boot-starter-mail) are present, a default javamailsender will be created. The sender can be further customized through the configuration items under the Spring.mail namespace, and the following Motobumi home blog details how spring boot implements the sending of messages.


Introducing Spring-boot-starter-mail dependencies, add the following in the Pom.xml configuration file (based on the Pom.xml file in the previous section, "Spring Boot Build Framework"):

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

App Send Message case

In the Application.properties configuration file, add the following configuration (note to replace your user name and password):

Spring.mail.host=smtp.qq.com
Spring.mail.username= user name//sender's mailbox
spring.mail.password= Password//For QQ mailbox password refers to the sender's authorization code
Spring.mail.properties.mail.smtp.auth=true
Spring.mail.properties.mail.smtp.starttls.enable=true
Spring.mail.properties.mail.smtp.starttls.required=true

The Mail Service code, specifically as follows:

@Service
public class Mailservice {
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
* @param to
* @param subject
* @param content
*/
public void Sendhtmlmail (string to, string subject, string content) {
MimeMessage message = Sender.createmimemessage ();

try {
True indicates that a multipart message needs to be created
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
* @param to
* @param subject
* @param content
* @param FilePath
*/
public void Sendattachmentsmail (string to, string subject, string content, String FilePath) {
MimeMessage message = Sender.createmimemessage ();

try {
True indicates that a multipart message needs to be created
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);

Sender.send (message);
Logger.info ("The message with the attachment has been sent. ");
} catch (Messagingexception e) {
Logger.error ("An exception occurred while sending messages with attachments.") ", e);
}
}

/**
* Send messages embedded in static resources (typically pictures)
* @param to
* @param subject
* @param content message, 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 indicates that a multipart message needs to be created
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 while sending messages embedding static resources.") ", e);
}
}
}

The simple test code is as follows:

public class Mailtests extends basicutclass{
@Autowired
Private Mailservice Mailservice;

Private String to = "xujijun@mail.cn";

@Test
public void Sendsimplemail () {
Mailservice.sendsimplemail (To, "Subject: Simple Mail", "test message content");
}

}


Source: Motobumi Blog

Links: https://blog.yoodb.com/yoodb/article/detail/1412

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.