博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
邮件发送
阅读量:5278 次
发布时间:2019-06-14

本文共 6981 字,大约阅读时间需要 23 分钟。

1:整合springboot的发送参见:

https://blog.csdn.net/zyw_java/article/details/81635375

2:常见的邮箱服务器地址:

https://blog.csdn.net/chuanyu/article/details/46740287

3:如何查询公司的outlook邮箱服务器地址:

https://zhidao.baidu.com/question/39774692.html

 

关键点:

密码为三方授权码

待解决问题:

如何查询企业邮箱的服务地址?

 

demo

org.springframework.boot
spring-boot-starter-mail

配置

spring:  application:    name: test-config-server  profiles:  mail:    host: smtp.163.com #发送邮件服务器    username: ppyun7c@163.com #163邮箱    password: wdsqm01 #客户端授权码    protocol: smtp #发送邮件协议    properties.mail.smtp.auth: true    properties.mail.smtp.port: 465 #端口号465或994    properties.mail.display.sendmail: XIEDONGXU #可以任意    properties.mail.display.sendname: Spring Boot Guide Email #可以任意    properties.mail.smtp.starttls.enable: true    properties.mail.smtp.starttls.required: true    properties.mail.smtp.ssl.enable: true    default-encoding: utf-8    from: ppyun7c@163.com #与上面的username保持一致

 

代码

package com.test.domi.service;import javax.mail.MessagingException;public interface IMailService {    /**     * 发送文本邮件     * @param to     * @param subject     * @param content     */    public void sendSimpleMail(String to, String subject, String content, String... cc);    /**     * 发送HTML邮件     * @param to     * @param subject     * @param content     * @throws MessagingException     */    public void sendHtmlMail(String to, String subject, String content, String... cc)throws MessagingException;    /**     * 发送带附件的邮件     * @param to     * @param subject     * @param content     * @param filePath     * @throws MessagingException     */    public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc)throws MessagingException;    /**     * 发送正文中有静态资源的邮件     * @param to     * @param subject     * @param content     * @param rscPath     * @param rscId     * @throws MessagingException     */    public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc)throws MessagingException;}

 实现类

package com.test.domi.service.impl;import com.test.domi.service.IMailService;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.Component;import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;import java.io.File;@Componentpublic class IMailServiceImpl implements IMailService {    @Autowired    private JavaMailSender mailSender;    @Value("${spring.mail.from}")    private String from;    /**     * 发送文本邮件     * @param to     * @param subject     * @param content     */    @Override    public void sendSimpleMail(String to, String subject, String content, String... cc) {        SimpleMailMessage message = new SimpleMailMessage();        message.setFrom(from);        message.setTo(to);        message.setCc(cc);        message.setSubject(subject);        message.setText(content);        mailSender.send(message);    }    /**     * 发送HTML邮件     * @param to     * @param subject     * @param content     */    @Override    public void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException{        MimeMessage message = mailSender.createMimeMessage();        MimeMessageHelper helper = new MimeMessageHelper(message, true);        helper.setFrom(from);        helper.setTo(to);        helper.setCc(cc);        helper.setSubject(subject);        helper.setText(content, true);        mailSender.send(message);    }    /**     * 发送带附件的邮件     * @param to     * @param subject     * @param content     * @param filePath     * @throws MessagingException     */    @Override    public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc)throws MessagingException {        MimeMessage message = mailSender.createMimeMessage();        MimeMessageHelper helper = new MimeMessageHelper(message, true);        helper.setFrom(from);        helper.setTo(to);        helper.setCc(cc);        helper.setSubject(subject);        helper.setText(content, true);        FileSystemResource file = new FileSystemResource(new File(filePath));        String fileName = filePath.substring(filePath.lastIndexOf(File.separator));        helper.addAttachment(fileName, file);        mailSender.send(message);    }    /**     * 发送正文中有静态资源的邮件     * @param to     * @param subject     * @param content     * @param rscPath     * @param rscId     */    @Override    public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException {        MimeMessage message = mailSender.createMimeMessage();        MimeMessageHelper helper = new MimeMessageHelper(message, true);        helper.setFrom(from);        helper.setTo(to);        helper.setCc(cc);        helper.setSubject(subject);        helper.setText(content, true);        FileSystemResource res = new FileSystemResource(new File(rscPath));        helper.addInline(rscId, res);        mailSender.send(message);    }}

 

 

controller

package com.test.domi.controller;import com.test.domi.common.utils.ResultInfo;import com.test.domi.common.utils.ResultUtil;import com.test.domi.service.impl.IMailServiceImpl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/email")public class EmailController {    @Autowired    private IMailServiceImpl mailService;//注入发送邮件的各种实现方法    @GetMapping("/normal")    public ResultInfo index(){        try {            mailService.sendSimpleMail("491431567@qq.com","SpringBoot Email","这是一封普通的SpringBoot测试邮件");            String[] cc = {"491431567@qq.com","491431567@qq.com"};            mailService.sendAttachmentsMail("491431567@qq.com","附件发送测试","这是附件","C:\\Users\\Domi\\Desktop\\ls.txt",cc);            mailService.sendHtmlMail("491431567@qq.com","html测试","\n"                    + "    
\n" +"

欢迎使用IJPay -By Javen

\n" +"
https://github.com/Javen205/IJPay" + "
IJPay 让支付触手可及,欢迎Start支持项目发展:)
\n" + "
如果对你有帮助,请任意打赏\n" + "
\n" + " \n" + "",cc); }catch (Exception ex){ ex.printStackTrace(); return ResultUtil.getFailResult("999999","失败",null); } return ResultUtil.getSuccessResult(null); }}

 

转载于:https://www.cnblogs.com/domi22/p/9749024.html

你可能感兴趣的文章
octave基本操作
查看>>
axure学习点
查看>>
WPF文本框只允许输入数字[转]
查看>>
dom4j 通用解析器,解析成List<Map<String,Object>>
查看>>
第一个项目--用bootstrap实现美工设计的首页
查看>>
使用XML传递数据
查看>>
TYVJ.1864.[Poetize I]守卫者的挑战(概率DP)
查看>>
0925 韩顺平java视频
查看>>
iOS-程序启动原理和UIApplication
查看>>
mysql 8.0 zip包安装
查看>>
awk 统计
查看>>
模板设计模式的应用
查看>>
实训第五天
查看>>
平台维护流程
查看>>
2012暑期川西旅游之总结
查看>>
12010 解密QQ号(队列)
查看>>
2014年辛星完全解读Javascript第一节
查看>>
装配SpringBean(一)--依赖注入
查看>>
java选择文件时提供图像缩略图[转]
查看>>
方维分享系统二次开发, 给评论、主题、回复、活动 加审核的功能
查看>>