spring里发邮件到邮箱的实现

注意,使用的是qq邮箱,qq邮箱-设置-账户里的协议要开启!

application.properties:

# 邮件 smtp 设置
spring.mail.username=你的邮箱
# 如果用 QQ 邮箱, 就用下面的 host
spring.mail.host=smtp.qq.com
spring.mail.password=你的密码
spring.mail.port=465
spring.mail.protocol=smtps

 

异步、同步实现

package kybmig.ssm.controller;

import kybmig.ssm.Utility;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


@Component
class AsyncMail {
    private MailSender sender;
    private MailProperties mailProperties;

    public AsyncMail(MailSender sender, MailProperties mailProperties) {
        this.sender = sender;
        this.mailProperties = mailProperties;
    }

    @Async
    public void sendMail(String address, String title, String content) {
        Utility.log("异步发送邮件");
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom(mailProperties.getUsername());;
        mailMessage.setSubject(title);
        mailMessage.setTo(address);
        mailMessage.setText(content);

        sender.send(mailMessage);
    }
}


@Component
class SyncMail {
    private MailSender sender;
    private MailProperties mailProperties;

    public SyncMail(MailSender sender, MailProperties mailProperties) {
        this.sender = sender;
        this.mailProperties = mailProperties;
    }

    public void sendMail(String address, String title, String content) {
        Utility.log("同步发送邮件");
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom(mailProperties.getUsername());
        ;
        mailMessage.setSubject(title);
        mailMessage.setTo(address);
        mailMessage.setText(content);

        sender.send(mailMessage);
    }
}

@Controller
@RequestMapping("/mail")
public class MailController {
    private SyncMail syncMail;
    private AsyncMail asyncMail;

    public MailController(SyncMail syncMail, AsyncMail asyncMail) {
        this.syncMail = syncMail;
        this.asyncMail = asyncMail;
    }


    @GetMapping("/index")
    public ModelAndView index() {
        return new ModelAndView("mail/index");
    }

    @GetMapping("/send")
    @ResponseBody
    public String send(String address, String title, String content) {
        boolean valid = address !=null && title != null && content != null;
        if (valid) {
            syncMail.sendMail(address, title, content);
            Utility.log("同步发送结束");
            return "发送成功";
        } else {
            return "发送失败, 格式不对";
        }
    }

    @GetMapping("/asySend")
    @ResponseBody
    public String asySend(String address, String title, String content) {
        boolean valid = address !=null && title != null && content != null;
        if (valid) {
            asyncMail.sendMail(address, title, content);
            Utility.log("异步发送结束");
            return "发送成功";
        } else {
            return "发送失败, 格式不对";
        }
    }
}

加异步注解@EnableAsync

@SpringBootApplication
@EnableAsync
public class SsmApplication {

    public static void main(String[] args) {
        System.out.println(System.getProperty("file.encoding"));
        System.out.println("本地 ide 运行入口或者命令行 java -jar 的入口,生产级 tomcat 不运行这个 main 函数");
        SpringApplication.run(SsmApplication.class, args);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值