Spring mail encapsulates javaMail's mail service, making it easier to use mail service. Take the QQ mailbox server as an example, and use the spring mail service to send emails.
Configure the QQ mailbox, "Settings" - "Account", open the SMTP service, and generate an authorization code
Generating an authorization code requires verification of the mobile phone. Next, you can use the QQ email account and authorization code to send emails, and there is no need for a QQ password.
The spring mail service is configured in spring-context-support, and then you can send emails with the sending server provided by the qq mailbox.
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version></dependency><dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.2.17.RELEASE</version></dependency>
Normal text mail
The first test is normal text mail
package com.xmyself.mail; import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSenderImpl; public class Main { public static void main(String[] args) { JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setHost("smtp.qq.com"); mailSender.setPort(587); mailSender.setUsername("[email protected]"); mailSender.setPassword("dsruklozelxcbdba");//Authorization code SimpleMailMessage mail = new SimpleMailMessage(); mail.setTo("[email protected]"); mail.setFrom("[email protected]"); mail.setSubject("test mail"); mail.setText("test mail content"); mailSender.send(mail); System.out.println("success"); }}Run it and send an email. Note: the authorization code is not the password, the port is not 25 but 587
Next, keep the mailSender unchanged, modify the mail type, and send rich mail messages
Simple html mail
Let the email content be displayed in html format, just modify it as follows
MimeMessage mail = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(mail, true);//true is used to turn on multipart mode and add an image or attachment helper.setTo("[email protected]");helper.setFrom("[email protected]");helper.setSubject("test mail");helper.setText("<html><head></head><body>" + "<h1>hello!!spring html Mail</h1>" + "</body></html>" , true);Still send this mail using mailSender
mailSender.send(mail);
HTML mail with pictures
Insert image display in the html content of the email, modify the text content
helper.setText("<html><head></head><body>" + "<h1>hello!!spring html Mail</h1>" + "<img src=/"cid:image/" />" + "</body></html>" , true);FileSystemResource image = new FileSystemResource(new File("d:/test.jpg"));helper.addInline("image", image); HTML mail with attachments
Add attachments to email, the text content remains unchanged, just modify it as follows
helper.setText("<html><head></head><body>" + "<h1>hello!!spring html Mail</h1>" + "</body></html>" , true);FileSystemResource image = new FileSystemResource(new File("d:/test.jpg"));helper.addAttachment("test.jpg", image); freemarker template email
HTML content is usually very rich, and it is too messy to write directly in the setText() method. Therefore, html should be managed separately as a file, and then use tools to convert its content into a string as a parameter of setText(). The following is a freemarker template engine as an example
Create a new templates directory in the project src/main/resources directory, and place a test.ftl file, the content is as follows
<html> <head></head> <body> <p>test freemarker template, welcome ${username}</p> <img src="cid:image" /> </body></html>Then, use the tools provided by freemarker and spring to convert the content into a string, which of course requires relying on the new jar
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version></dependency>
Create a new FreemarkerParser.java
package com.xmyself.mail; import java.util.Map;import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;import freemarker.template.Configuration;import freemarker.template.Template; public class FreemarkerParser { public String toHtmlString(String name, Map<String, String> data) { @SuppressWarnings("deprecation") Configuration config = new Configuration(); config.setClassForTemplateLoading(this.getClass(), "/templates/"); try { Template template = config.getTemplate(name); return FreeMarkerTemplateUtils.processTemplateIntoString(template, data); } catch (Exception e) { e.printStackTrace(); } return "fail"; }}Replace the ${} content in the template with the value in the map, convert the template file to a String string
Note: The configuration and reading of template paths during the process are troublesome, and it is temporarily handled in this way.
The code to send an email requires only a very small change
Map<String, String> data = new HashMap<String, String>();data.put("username", "chengyi");String text = new FreemarkerParser().toHtmlString("test.ftl", data); helper.setText(text, true);FileSystemResource image = new FileSystemResource(new File("d:/test.jpg"));helper.addInline("image", image);The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.