1.先配置自己的邮箱,获取校验码,将自己的邮箱配置成客户端

查看:

2.配置application.yml文件

spring:mail:#发送邮件服务器host: smtp.qq.com#邮箱用户名username: @qq.com#邮箱授权码password: ***********#字符编码default-encoding: utf-8

3.创建一个实体类,接受前台传参数据

package com.ds.entity;import java.io.Serializable;public class Mail implements Serializable {private String mail;//邮箱账号private String topic;//主题private String message;//内容或者html页面内容private String fileid;//fileidpublic Mail() {}public Mail(String mail, String topic, String message, String fileid) {this.mail = mail;this.topic = topic;this.message = message;this.fileid = fileid;}public String getMail() {return mail;}public void setMail(String mail) {this.mail = mail;}public String getTopic() {return topic;}public void setTopic(String topic) {this.topic = topic;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public String getFileid() {return fileid;}public void setFileid(String fileid) {this.fileid = fileid;}
}

4.创建service接口

package com.ds.service;public interface MailService {/*** 发送文本邮件* @param to 收件人* @param subject 主题* @param content 内容*/void sendSimpleMail(String to, String subject, String content);/*** 发送HTML邮件* @param to 收件人* @param subject 主题* @param content 内容*/void sendHtmlMail(String to, String subject, String content);/*** 发送带附件的邮件* @param to 收件人* @param subject 主题* @param content 内容* @param filePath 附件*/public void sendAttachmentsMail(String to, String subject, String content, String filePath);
}

5.创建serviceimpl实现类

package com.ds.service.impl;import com.ds.service.MailService;
import lombok.extern.slf4j.Slf4j;
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
@Slf4j
public class MailServiceImpl implements MailService {@Autowiredprivate JavaMailSender javaMailSender;//注入配置文件中配置的信息——>from@Value("${spring.mail.username}")private String from;/*** 发送文本邮件* @param to 收件人* @param subject 主题* @param content 内容*/@Overridepublic void sendSimpleMail(String to, String subject, String content) {log.info("发送文本邮件---->");log.info("发送邮件至:"+to);log.info("发送邮件主题:"+subject);log.info("发送文本内容:"+content);SimpleMailMessage message = new SimpleMailMessage();//发件人message.setFrom(from);//收件人message.setTo(to);//邮件主题message.setSubject(subject);//邮件内容message.setText(content);//发送邮件javaMailSender.send(message);}/*** 发送HTML邮件* @param to 收件人* @param subject 主题* @param content 内容*/@Overridepublic void sendHtmlMail(String to, String subject, String content) {MimeMessage message = javaMailSender.createMimeMessage();MimeMessageHelper messageHelper;try {log.info("发送HTML邮件---->");log.info("发送邮件至:"+to);log.info("发送邮件主题:"+subject);log.info("发送文本内容:"+content);messageHelper = new MimeMessageHelper(message,true);messageHelper.setFrom(from);messageHelper.setTo(to);message.setSubject(subject);messageHelper.setText(content,true);javaMailSender.send(message);log.info("邮件已经发送!");} catch (MessagingException e) {log.error("发送邮件时发生异常:"+e);}}/*** 发送带附件的邮件* @param to 收件人* @param subject 主题* @param content 内容* @param filePath 附件*/@Overridepublic void sendAttachmentsMail(String to, String subject, String content, String filePath) {MimeMessage message = javaMailSender.createMimeMessage();MimeMessageHelper messageHelper;try {log.info("发送带附件邮件---->");log.info("发送邮件至:"+to);log.info("发送邮件主题:"+subject);log.info("发送文本内容:"+content);messageHelper = new MimeMessageHelper(message,true);messageHelper.setFrom(from);messageHelper.setTo(to);messageHelper.setSubject(subject);messageHelper.setText(content,true);//携带附件FileSystemResource file = new FileSystemResource(filePath);if(file.exists()){//如果文件存在String fileName = filePath.substring(filePath.lastIndexOf(File.separator));messageHelper.addAttachment(fileName,file);}javaMailSender.send(message);log.info("邮件加附件发送成功!");} catch (MessagingException e) {log.error("发送失败:"+e);}}
}

6.创建接口调用

package com.ds.controller;import com.ds.common.FilePath;
import com.ds.entity.File;
import com.ds.entity.Mail;
import com.ds.entity.RespBean;
import com.ds.service.FileService;
import com.ds.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class MailController {@Autowiredprivate JavaMailSender javaMailSender;@AutowiredMailService mailService;@AutowiredFileService fileService;@RequestMapping("/send")public String sendMail(){//测试用例SimpleMailMessage message = new SimpleMailMessage();message.setFrom("@qq.com");//发送人邮箱message.setTo("@qq.com");//收件人邮箱message.setSubject("11111111");//发送标题message.setText("笑笑笑");//发送内容try {javaMailSender.send(message);return "成功";}catch (Exception e){e.printStackTrace();return "失败";}}@RequestMapping("/sendSimpleMail")public RespBean sendSimpleMail(Mail mail){try {mailService.sendSimpleMail(mail.getMail(),mail.getTopic(),mail.getMessage());return RespBean.sucess("发送成功!");}catch (Exception e){e.printStackTrace();return RespBean.error("发送失败!");}}@RequestMapping("/sendHtmlMail")public RespBean sendHtmlMail(Mail mail){try {mailService.sendHtmlMail(mail.getMail(),mail.getTopic(),mail.getMessage());return RespBean.sucess("发送成功!");}catch (Exception e){e.printStackTrace();return RespBean.error("发送失败!");}}@RequestMapping("/sendFileMail")public RespBean sendFileMail(Mail mail){try {File file = fileService.getFileByid(mail.getFileid());if(file!=null){mailService.sendAttachmentsMail(mail.getMail(),mail.getTopic(),mail.getMessage(), FilePath.PATH+file.getPath());}else{mailService.sendAttachmentsMail(mail.getMail(),mail.getTopic(),mail.getMessage(),null);}return RespBean.sucess("发送成功!");}catch (Exception e){e.printStackTrace();return RespBean.error("发送失败!");}}}

其中:RespBean是我自定义的返回实体类。

package com.ds.entity;/*
* 公共返回对象
* */
public class RespBean {private long code;private String message;private Object obj;public RespBean() {}public RespBean(long code, String message, Object obj) {this.code = code;this.message = message;this.obj = obj;}public long getCode() {return code;}public void setCode(long code) {this.code = code;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public Object getObj() {return obj;}public void setObj(Object obj) {this.obj = obj;}@Overridepublic String toString() {return "RespBean{" +"code=" + code +", message='" + message + '\'' +", obj=" + obj +'}';}/** 成功返回结果* */public static RespBean sucess(String message){return new RespBean(200,message,null);}/** 成功返回结果* */public static RespBean sucess(String message,Object obj){return new RespBean(200,message,obj);}/** 失败返回结果* */public static RespBean error(String message){return new RespBean(500,message,null);}/** 失败返回结果* */public static RespBean error(String message,Object obj){return new RespBean(500,message,obj);}
}

邮件发送(qq邮箱)相关推荐

  1. Java邮件发送QQ邮箱带附件

    添加依赖 <!-- https://mvnrepository.com/artifact/javax.mail/mail --><dependency><groupId& ...

  2. 发送qq邮箱激活邮件工具类

    qq邮箱授权码在设置找 package com.Util;import com.sun.mail.util.MailSSLSocketFactory;import javax.mail.*; impo ...

  3. java 发送激活邮件 以qq邮箱为例

    java发送激活邮件 以qq邮箱为例 1.首先发送者邮箱需要开启服务 登录qq邮箱,选择设置--账号,开启以上两个服务.会获得相应的两个授权码 2.以下代码为发送邮件代码 package com.em ...

  4. php使用qq发邮件怎么发,php 发送QQ邮箱邮件

    这是我的源码比较简陋 https://www.lanzous.com/i2l7h8f 感谢 https://www.cnblogs.com/woider/p/6980456.html 下载phpmai ...

  5. SpringBoot整合定时任务和邮件发送(邮箱 信息轰炸 整蛊)

    SpringBoot整合定时任务和邮件发送(邮箱 信息轰炸 整蛊) 目录 SpringBoot整合定时任务和邮件发送(邮箱 信息轰炸 整蛊) 1.概述 2.最佳实践 2.1创建项目引入依赖(mail) ...

  6. C# WinForm 使用SMTP协议发送QQ邮箱验证码

    文章目录 前言 功能实现步骤 一.获取QQ邮箱授权码 二.功能界面 三.创建发送邮箱验证码的类 四.在From1中调用类中的函数,实现功能 前言   在程序设计中,发送验证码是常见的一个功能,用户在注 ...

  7. SpringBoot实现发送QQ邮箱验证码

    SpringBoot实现发送QQ邮箱验证码 打开qq邮箱官网 点击设置 找到开启服务:POP3/SMTP 然后复制给的密授权码(记住) 导入maven依赖坐标 <!--qq邮箱--> &l ...

  8. python 发送QQ邮箱

    文章目录 开启QQ邮箱的STMP服务 导包 MIMEText MIMEImage MIMEMultipart STMP_SSL 设置收发邮箱账号 创建SMTP_SSL实例 邮箱内容 纯文本 HTML格 ...

  9. linux如何写邮件到QQ邮箱

    linux如何写邮件到QQ邮箱 作为一名运维人,肯定不会,也不想7*24h待在电脑旁,观察服务器的情况. 要做一名高级运维,必会脚本. [注:]模拟场景:当剩余可用内存不足时,linux服务器会自动发 ...

  10. Python3 发送QQ邮箱(含附件)

    Python3 发送QQ邮箱(含附件) 准备工作: 1.用到的python包:smtplib和email 2.获取qq邮箱授权码:在qq邮箱的设置里面的账户下面,开启POP3/SMTP服务和IMAP/ ...

最新文章

  1. android控制wifi状态
  2. 蒙特卡洛树搜索的主要流程有哪些_海运拼箱操作流程主要有哪些
  3. Objective-C知识总结(5)
  4. Undefined Reference to Typeinfo
  5. 土是独体字结构吗_205砂浆、混凝土强度等级与定额不同时,你会调整换算吗?...
  6. centos 7 php目录权限设置密码,Linux系统中(CentOS 7)的用户和权限管理
  7. Dr.Cleaner pro for mac(系统清理和优化软件)
  8. PHPExcel导出Excel方法总结——ThinkPHP5
  9. C#温故而知新学习系列之面向对象编程—自动属性(十一)
  10. MATLAB三维绘图命令plot3入门
  11. html转换成chm乱码,【全部解决】用Docbook生成htmlhelp + 【完全解决】生成的chm中标题和左边的索引目录是乱码的问题...
  12. Stellarium(虚拟天文馆)
  13. Meebo 和 GMail + Talk 等 WebIM 的实现方式
  14. 百度地图LV1.5实践项目开发工具类bmap.util.jsV1.3
  15. Elasticsearch7.12常用操作总结
  16. 首创Domino前后端彻底分离,结合vue、react优美例子
  17. 71. 从Lotus Notes表单到XPage——兼谈程序里的二进制文件和文本文件
  18. 《人类简史》九、科学革命——承认自己无知的革命
  19. linux系统nc命令使用
  20. java unlock_java – 古怪的StampedLock.unlock(长)行为?

热门文章

  1. [ARCGIS]带黑边的IMG格式影像如何消除黑边?
  2. myftpadmin+proftpd+mysql架设ftp服务器_proftpd – 碎言碎语
  3. Android6.0以上系统搜索不到 蓝牙BLE 设备问题
  4. 【总结】1026- 一文读懂 base64
  5. 3D游戏建模:游戏建模都要用到哪些软件?
  6. pyaudio录制音频和播放音频
  7. 论游戏中Buff的实现
  8. 增加内容曝光、获得更多粉丝 - 「评论发红包」功能
  9. 多线程批量获取腾讯云磁盘分区状态
  10. 没有网就不能和女朋友开视频了?有Python在!没网照样开视频!