In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to use springboot to send mailboxes". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The first step is to add dependencies for mail
Org.springframework.boot spring-boot-starter-mail
The second step is to write the mailbox
Yml profile
Spring: # mailbox configuration mail: host: smtp.qq.com username: 2631245486@qq.com # authorization code of QQ Mail password: authorization code default-encoding: UTF-8 properties: mail: smtp: auth: true starttls: enable: true required: true
Configuration file for properties
# QQ Mail configuration # JavaMailSender email configuration spring.mail.host=smtp.qq.comspring.mail.username= user QQ Mail # QQ Mail's authorization code spring.mail.password= authorization code spring.mail.properties.mail.smtp.auth=truespring.mail.properties.mail.smtp.starttls.enable=truespring.mail.properties.mail.smtp.starttls.required=truespring.mail.default-encoding=UTF-8#163 mailbox configuration spring.mail.host=smtp.163.comspring.mail.username= user 163mailbox spring.mail. Password= mailbox password spring.mail.properties.mail.smtp.auth=truespring.mail.properties.mail.smtp.starttls.enable=truespring.mail.properties.mail.smtp.starttls.required=truespring.mail.default-encoding=UTF-8
Write two interfaces to send mail
Package www.it.com.server;import java.io.File / * * @ author wangjie:* @ version creation time: 10 am on August 27th, 2019. Description: * / public interface MailServer {/ * @ param sendUser email recipient * @ param title email title * @ param text email content * / void sendMailServer (String sendUser,String title,String text) / * * send * @ param sendUser * @ param title * @ param text * @ param file * / void sendFileMail (String sendUser,String title,String text,File file) with attached mailbox;}
Implementation of interface
Package www.it.com.server.impl;import java.io.File;import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;import org.apache.logging.log4j.message.SimpleMessage;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.core.io.FileSystemResource;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.MimeMessageHelper;import org.springframework.stereotype.Service Import www.it.com.server.MailServer;/*** @ author wangjie:* @ version created on August 27th, 2019. Description of the class: * / @ Servicepublic class MailServerImpl implements MailServer {@ Value ("${spring.mail.username}") private String fromUser; @ Autowired private JavaMailSender javaMailSender; public String getFromUser () {return fromUser } public void setFromUser (String fromUser) {this.fromUser = fromUser;} @ Override public void sendMailServer (String sendUser, String title, String text) {/ / the entity that creates the mail is used to encapsulate the information needed to send the message SimpleMailMessage simpleMailMessage=new SimpleMailMessage () / / sender simpleMailMessage.setFrom (fromUser); / recipient simpleMailMessage.setTo (sendUser); / / title of email simpleMailMessage.setSubject (title); / / content of email simpleMailMessage.setText (text) / / send javaMailSender.send (simpleMailMessage);} @ Override public void sendFileMail (String sendUser, String title, String text, File file) {MimeMessage mimeMessage = null; try {mimeMessage = javaMailSender.createMimeMessage () / / create a mimeMessageHelper object to process mail messages with attachments: MimeMessageHelper mimeMessageHelper=new MimeMessageHelper (mimeMessage,true); mimeMessageHelper.setFrom (fromUser); mimeMessageHelper.setTo (sendUser); mimeMessageHelper.setSubject (title); mimeMessageHelper.setText (text) FileSystemResource r = new FileSystemResource (file); / / add attachment mimeMessageHelper.addAttachment ("attachment", r); javaMailSender.send (mimeMessage) } catch (MessagingException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}}
Controller coding
Package www.it.com.controller;import java.io.File;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import www.it.com.server.MailServer / * * @ author wangjie: * @ version creation time: 9:52:30 * @ Description description: controller * / @ RestController () @ RequestMapping ("/ mail") public class MailController {@ Autowired private MailServer mailServer sent by email / * * send simple email * @ return * / @ RequestMapping ("/ send") public String sendMail () {/ / 2694433816 mailServer.sendMailServer ("2631245486@qq.com", "Hello", "come to your house tomorrow"); return "success" } / * send email with attachments * / @ RequestMapping ("/ sendFile") public String sendFileMail () {File file=new File ("C://Users//DELL//Desktop// Learning Materials .txt") MailServer.sendFileMail ("2631245486@qq.com", "Hello dsf", "this is the second email with attachment", file); return "success";}}
Steps to generate authorization codes
Login mailbox selection settings
Select account
Slide below to open the corresponding service selection to generate authorization codes
This is the end of the content of "how to use springboot to send mailboxes". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.