菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
196
109

浅析SpringBoot如何使用JavaMailSender发送邮件及遇到问题501 mail from address must be same as authorization user

原创
05/13 14:22
阅读数 16659

  Spring提供了非常好用的JavaMailSender接口实现邮件发送。在Spring Boot的Starter模块中也为此提供了自动化配置。下面通过实例看看如何在Spring Boot中使用JavaMailSender发送邮件。

一、简单邮件发送

1、导入依赖

  在Spring Boot的工程中的pom.xml中引入spring-boot-starter-mail依赖

<!-- mail -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2、填写邮件配置

  如其他自动化配置模块一样,在完成了依赖引入之后,只需要在application.properties中配置相应的属性内容。

spring: 
  mail:
    host: smtp.exmail.qq.com   // 邮件服务
    username: ***   // 使用哪个邮箱发送
    password: ***   // 邮箱密码
    port: 465
    protocol: smtp
    properties:
      mail:
        smtp:
          auth: true
          ssl:
            enable: true
          starttls:
            enable: true

3、编写邮件发送代码

package com.opengauss.exam.common.service;

import com.opengauss.exam.common.ExamMail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class EmailService {
    @Autowired
    private JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String emailFrom;   // 从配置文件取发件方

    public void sendSimpleMail(ExamMail examMail) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(emailFrom);  // 发件方
        message.setTo(examMail.getRecipient());  // 收件方
        message.setSubject(examMail.getSubject()); // 邮件主题
        message.setText(examMail.getContent());  // 邮件内容
        javaMailSender.send(message);
    }
}
package com.opengauss.exam.common;

import lombok.Data;

@Data
public class ExamMail {
    private String subject;
    private String recipient;
    private String content;
}

  这样就可以实现简单邮件发送了。

二、发送附件

  在上面单元测试中加入如下测试用例(通过MimeMessageHelper来发送一封带有附件的邮件)

@Test
public void sendAttachmentsMail() throws Exception {
    MimeMessage mimeMessage = mailSender.createMimeMessage();

    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
    helper.setFrom("***");
    helper.setTo("***");
    helper.setSubject("主题:***");
    helper.setText("***");

    FileSystemResource file = new FileSystemResource(new File("weixin.jpg"));
    helper.addAttachment("附件-1.jpg", file);
    helper.addAttachment("附件-2.jpg", file);

    mailSender.send(mimeMessage);
}

三、遇到问题501 mail from address must be same as authorization user

  这里在测试的时候,遇到这个报错。原因其实很简单,就是emailFrom我随便写的一个邮箱,而不是我配置的那个邮箱,所以就报错:501 mail from address must be same as authorization user,意思也很好理解。

  解决方法就是:将 emailFrom 改成配置文件里的那个邮箱即可。

 

  其他比如通过模板发送一份好看的邮件,以后有时间再写。

发表评论

0/200

热门评论

大雾散尽

22222

1 5
大雾散尽

阿西阿西

点赞 回复
大雾散尽

阿西阿西

点赞 回复
大雾散尽

呜呜呜呜

点赞 回复
查看更多...
大雾散尽

1111213232132232

0 12
我没有名字

sdfsdfs

点赞 回复
我没有名字

asdasda

点赞 回复
我没有名字

asdasda

点赞 回复
查看更多...
大雾散尽

1111213232132232

1 7
我没有名字

asdasda

点赞 回复
我没有名字

asfasfw

点赞 回复
我没有名字

asdasda

点赞 回复
查看更多...
大雾散尽

1111213232132232

1 4
我没有名字

afasdfa

点赞 回复
我没有名字

实打实大

点赞 回复
我没有名字

实打实大

点赞 回复
查看更多...
大雾散尽

34342432

0 1
我没有名字

阿斯达四大

点赞 回复
大雾散尽

34342432

0 2
我没有名字

阿斯达四大

点赞 回复
我没有名字

大叔大婶多

点赞 回复
大雾散尽

34342432

0 4
我没有名字

阿斯达四大

点赞 回复
我没有名字

阿斯达四大

点赞 回复
我没有名字

沙发第三代

点赞 回复
查看更多...
大雾散尽

34342432

0 1
我没有名字

且无人违反吧

点赞 回复
大雾散尽

34342432

1 0
大雾散尽

2322322323

1 0
大雾散尽

2322

0 0
大雾散尽

我是小樱

0 0
大雾散尽

erect飞 多次出出 vvv

0 0
大雾散尽

2222

1 0
大雾散尽

22222

1 0
大雾散尽

22

0 0
大雾散尽

呜呜呜

0 0
大雾散尽

232

0 0
大雾散尽

22232332433335345345345435345345

0 0
大雾散尽

1131313

0 0
大雾散尽

内容太狠了 优秀

1 0
大雾散尽

1123

1 0
大雾散尽

34454544444

0 1
大雾散尽

rgrgf

点赞 回复
大雾散尽

23434

0 1
大雾散尽

ewdfwef

点赞 回复
大雾散尽

点还上课不好闹参数的你发

0 5
大雾散尽

没有毛病

点赞 回复
大雾散尽

好像是都可以的

点赞 回复
大雾散尽

好像回复有点距离太小了

点赞 回复
查看更多...
大雾散尽

阿西爱心爱心矮矮小

0 5
大雾散尽

OMG

点赞 回复
大雾散尽

威斯安徽

点赞 回复
大雾散尽

这是奇偶i

点赞 回复
查看更多...
大雾散尽

大号那分就开始DNF冀东水泥三点就

0 0
大雾散尽

111111111111111111

0 0
大雾散尽

11111111111111111111

0 0
大雾散尽

隐藏着黑暗力量的钥匙啊

0 0
大雾散尽

在我面前显示你真正的力量

0 0
大雾散尽

我以小樱的身份命令你

0 4
大雾散尽

阿西

点赞 回复
大雾散尽

我我我哦我

点赞 回复
大雾散尽

回复的回复刷新没了

点赞 回复
查看更多...
大雾散尽

封印解除

0 3
大雾散尽

好像不行

点赞 回复
大雾散尽

好像不行

点赞 回复
大雾散尽

啊呸

点赞 回复
查看更多...
Z.

说的都对

1 0
Z.

1111111111111111

0 1
大雾散尽

2222

点赞 回复
Z.

我觉得也是,说的很好

0 0
大雾散尽

2的单杀四次

0 0
大雾散尽

哒哒哒哒哒哒多多多多多多多多多多多多多多多多多多多多多多多多多多多多多多多多多多

1 0
还没有起名字

奥术大师大所大所

0 0
还没有起名字

奥术大师大二

0 0
还没有起名字

阿打算打

0 0
还没有起名字

阿打算打

0 0
还没有起名字

Asdasd

0 0
还没有起名字

奥术大师

0 0
还没有起名字

阿斯达四大

0 0
还没有起名字

阿斯达四大

0 0
还没有起名字

实打实大

0 0
还没有起名字

阿斯达四大

0 0
还没有起名字

你是大牛

0 0
196 点赞
109 评论
收藏
为你推荐 换一批