使用java mail 发送邮件报错处理com.sun.mail.smtp.SMTPSendFailedException: 501 Mail from address must be same as

使用java mail发送邮件报错处理

我的解决思路:

这个问题是和邮箱账号相关的问题,检查和账号有关的设置和代码,让其保持一致,即可解决此问题。

我的出错位置及解决方案:

我在yml配置文件中并没有添加“此位置出错”部分,但是在service层实现接口时进行了添加,导致spring.mail.username与setFrom不一致,从而报错。
去掉“此位置出错”或者在spring.mail.useranme中添加“此位置出错”都可解决此问题。

public void sendMail() {
        SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom(from+"此位置出错");
        message.setTo(to);
        message.setSubject(subject);
        message.setText(context);
        javaMailSender.send(message);
    }

以下为报错代码

报错内容:

2022-10-18 11:19:33.609 ERROR[http-nlo-8081-exec-2]o.a.c.c.C.[Tomcat].[localhost].[/].[dispatcherServlet].log:175 -Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 501 Mail from address must be same as authorization user.
;
  nested exception is:
	com.sun.mail.smtp.SMTPSenderFailedException: 501 Mail from address must be same as authorization user.
; message exceptions (1) are:
Failed message 1: com.sun.mail.smtp.SMTPSendFailedException: 501 Mail from address must be same as authorization user.
;
  nested exception is:
	com.sun.mail.smtp.SMTPSenderFailedException: 501 Mail from address must be same as authorization user.
] with root cause
org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 501 Mail from address must be same as authorization user.
;
  nested exception is:
	com.sun.mail.smtp.SMTPSenderFailedException: 501 Mail from address must be same as authorization user.

目录结构

在这里插入图片描述

pom.xml

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

application.yml

spring:
  mail:
    host: smtp.qq.com
	#	邮箱的账号 
    username: 111111@qq.com
    #	邮箱对应的POP3/SMTP服务的授权码
    password: yldoxcxiihmdjhai

service.sendMailService

package com.cn.service;

/**
 * @Author: yjml
 * @Date: 2022/8/15$ 15:08$
 */
public interface SendMailService {
    /**
     * 发邮件
     */
    void sendMail();
}

service.impl.sendMailServiceImpl

package com.cn.service.impl;

import com.cn.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * @Author: yjml
 * @Date: 2022/8/15$ 15:11$
 */
@Service
public class SendMailServiceImpl implements SendMailService {

	//此处也可能报错,这是另一个问题,最可能出现的情况是报错也不影响程序运行
    @Autowired
    private JavaMailSender javaMailSender;
    
    private String from = "111111@qq.com";
    private String to = "222222@qq.com";
    private String subject = "测试标题";
    private String context = "测试正文";
    @Override
    public void sendMail() {
        SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom(from+"此位置出错");
        message.setTo(to);
        message.setSubject(subject);
        message.setText(context);
        javaMailSender.send(message);
    }
}

springbootMailApplicationTests测试

package com.cn;

import com.cn.service.SendMailService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Springboot23MailApplicationTests {

    @Autowired
    private SendMailService sendMailService;
    @Test
    void contextLoads() {
        sendMailService.sendMail();
    }
}

注:此文章仅为笔记,如有侵权等行为,可联系作者删除或修改文章。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值