Spring Mail sends mail with the help of a QQ mailbox server _java

Source: Internet
Author: User
Tags email account

Spring Mail encapsulates the JavaMail mail service, making it easier to use, using the QQ mailbox server as an example to send mail with the Spring Mail Service

Configure QQ Mailbox, "Settings"-"Account", open SMTP service, generate authorization code

Generate authorization code need to verify the phone, then use QQ email account and Authorization code can send mail, do not need QQ password

Spring Mail Service in Spring-context-support, configure dependencies, and then can send mail with the help of the sending server from 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>

Plain Text Mail

The first test is plain 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 Javamailsenderimp L ();
  Mailsender.sethost ("smtp.qq.com");
  Mailsender.setport (587);
  Mailsender.setusername ("573215750@qq.com");
  Mailsender.setpassword ("Dsruklozelxcbdba");//Authorization Code
   
  simplemailmessage mail = new Simplemailmessage ();
  Mail.setto ("573215750@qq.com");
  Mail.setfrom ("573215750@qq.com");
  Mail.setsubject ("Test Mail");
  Mail.settext ("Test mail Content");
   
  Mailsender.send (mail);
  SYSTEM.OUT.PRINTLN ("Success");
 }

Run, you can send an email, note: Authorization code instead of password, port is not 25 but 587

Next, keep the mailsender unchanged, modify the mail type, and send content-rich messages

Simple HTML mail

Let the content of the message be displayed in HTML format, just modify the following

MimeMessage mail = Mailsender.createmimemessage ();
Mimemessagehelper helper = new Mimemessagehelper (mail, True);//true to open multipart mode, add picture or attachment
 
Helper.setto (" 573215750@qq.com ");
Helper.setfrom ("573215750@qq.com");
Helper.setsubject ("Test Mail");
Helper.settext (" 
 

Still use MailSender to send this mail

Mailsender.send (mail);

HTML messages with pictures

Inserts a picture display in the message's HTML content, modifies the text content to be possible

Helper.settext (" 
 

HTML Messages with Attachments

Add attachments to the message, text content unchanged, only need to modify the following

Helper.settext (" 
 

Freemarker Template Mail

HTML content is usually very rich, write directly in the SetText () method is too messy, so, you should be HTML as a file management, and then use the tool to convert its contents to strings, as SetText () parameters, the following Freemarker template engine as an example

In the project Src/main/resources directory to create a new templates directory, which contains a TEST.FTL file, as follows

 
 

Then, using the tools provided by Freemarker and spring to convert content to strings, of course, it depends on the new jar

<dependency>
 <groupId>org.freemarker</groupId>
 <artifactid>freemarker</ artifactid>
 <version>2.3.23</version>
</dependency>

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";
 }

Replaces the ${} content in the template with the value in the map, converting the template file to a string string

Note: the configuration and reading of template paths in the process is a hassle and is handled in this way for the time being

The code that sends the message requires only very small changes

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 the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.