技术标签: spring boot javamail SpringBoot知识
pom.xml
文件下引入邮件依赖包<!--发邮件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
邮件处理工具类
package com.online_post.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@Service
public class MailUtils {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Value("${spring.mail.username}")
private String from;
@Autowired
private JavaMailSender mailSender;
/**
* 简单文本邮件
* @param to 接收者邮件
* @param subject 邮件主题
* @param contnet 邮件内容
*/
public void sendSimpleMail(String to, String subject, String contnet){
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(contnet);
message.setFrom(from);
mailSender.send(message);
}
/**
* HTML 文本邮件
* @param to 接收者邮件
* @param subject 邮件主题
* @param contnet HTML内容
* @throws MessagingException
*/
public void sendHtmlMail(String to, String subject, String contnet) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(contnet, true);
helper.setFrom(from);
mailSender.send(message);
}
/**
* 附件邮件
* @param to 接收者邮件
* @param subject 邮件主题
* @param contnet HTML内容
* @param filePath 附件路径
* @throws MessagingException
*/
public void sendAttachmentsMail(String to, String subject, String contnet,
String filePath) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(contnet, true);
helper.setFrom(from);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = file.getFilename();
helper.addAttachment(fileName, file);
mailSender.send(message);
}
/**
* 图片邮件
* @param to 接收者邮件
* @param subject 邮件主题
* @param contnet HTML内容
* @param rscPath 图片路径
* @param rscId 图片ID
* @throws MessagingException
*/
public void sendInlinkResourceMail(String to, String subject, String contnet,
String rscPath, String rscId) {
logger.info("发送静态邮件开始: {},{},{},{},{}", to, subject, contnet, rscPath, rscId);
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(contnet, true);
helper.setFrom(from);
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);
mailSender.send(message);
logger.info("发送静态邮件成功!");
} catch (MessagingException e) {
logger.info("发送静态邮件失败: ", e);
}
}
}
application.properties
中修改配置文件#邮箱服务器地址,各大运营商不同(此处发送方邮箱以QQ为例)
spring.mail.host=smtp.qq.com
#用户名
spring.mail.username=xxx@qq.com #该qq邮箱是发送方的邮箱
#密码,如果是qq的,要申请临时授权码
spring.mail.password=xxx
spring.mail.default-encoding=UTF-8
#以谁来发送邮件
mail.fromMail.addr=xxx1@qq.com
#加下面这两个配置才能通过qq给qq及其他邮箱(如163等)发邮件
spring.mail.protocol=smtp
spring.mail.properties.mail.smtp.ssl.enable=true
QQ临时授权码
按照操作获取授权码之后将该授权码粘贴到application.properties中的
#密码,如果是qq的,要申请临时授权码
spring.mail.password=你的授权码
此步骤省略,可随意在任何测试类中测试
QQ: 2805005861
PHP-FPM模式相对于CLI比较复杂,因为PHP-FPM为常驻进程不断处理请求,因此二者只是在执行流程略有不同.PHP-FPM现在PHP广泛被使用的一种模式,用于解析FastCGI协议,执行相应的请求.PHP-FPM 三种模式staticFPM启动根据pm.max_children固定的woker进程数.dynamicFPM是启动的woker进程是变化:根据参数pm.start_servers~...
本文详细讲解了 jQuery 中的 DOM 操作。走过路过不要错过丫
SpringBoot是近年来比较火的一门技术,越来越多的公司已经在使用,很多公司招聘也会要求求职者掌握,作为一个与时俱进的程序员,为了提高我们的姿势水平,也得赶紧学起来啊,本篇博客简要介绍SpringBoot及其特性,还有怎么样快速开始一个SpringBoot项目。
springboot-用logback将日志文件按等级保存到不同文件案例:例如项目基本包名为com.xxx,将该包下的所有日志按debug、info、warn、error等级分别保存到D:/log文件夹下的logback_debug、logback_info、logback_warn、logback_error文件中。每日凌晨0点将日志文件归档保存为zip压缩包,每个压缩包最大50M,...
域名备案需要购买服务器吗 内容精选换一换可以。如果您有业务部署在华为云服务器(含弹性公网IP),即可通过华为云备案,与域名注册服务商没有关系。示例:如果您的主体和域名均为第一次备案,即在工信部无任何备案信息。请参见首次备案。如果您的主体和域名已在其他接入商备案过,现需将顶级域名或其子域名解析在华为云,应申请新增接入备案。请参见新增接入。可以用于华为云备案的服务器,请参见准备可备备案是中国大陆的一项...
Seata Server搭建和配置搭建Nacos可参考我的博客:Nacos服务治理中心和配置中心第一步:下载安装服务端,并解压到指定位置unzip seata-server-1.1.0.zip seata的目录结构:bin:存放各系统的启动脚本conf:存放seata server启动时所需要配置信息、数据库模式下所需要的建表语句lib:运行seata server所需要的的依赖包第二步:配置seataseata的配置文件(conf目录下)file.conf: 该文件用于配置存
查找所在目录的方式如下:System.out.println(System.getProperty(“java.io.tmpdir”));System.getProperty(),还可以获取更多其他的系统字符,具体可以查看文档在我电脑上打印的log日志是:C:\Users\wxl19\AppData\Local\Temp\在不同的操作系统上java.io.tmpdir指向...
import java.util.*;import java.util.*;import java.text.*;public class Default { public static void main(String[] args) { Locale aLocale = Locale.JAPAN; Locale.setDefault(aLocale); System.out.println(
# coding:utf-8 '''@author: [email protected]: [email protected]: Implementation: SVDBias Datatset: Movielen-1m Evaluation: hitradio,ndcg Squared loss function wit...
看报错信息,问题出现在了代码段的第二十行,这行只写了一个for循环,再看下面还报了一个空指针异常的错误,初步判断问题出现在了list上,jsp没能从Java后台中获取到list的数据。找到对应发送请求的位置package controller;import service.UserService;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servl..
<br />TBXML常用API2010年10月28日 星期四 11:27==============================================================================<br />文件<br /><br />tbxml“框架”中包含的文件:<br />TBXML.h - tbxml声明<br />TBXML.m - tbxml实现<br />NSDataAdditions.h - NSData类别等的声明<br />NSDataAdditions
1.spring配置文件test-spring-service.xml<?xml version="1.0" encoding="utf-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-...