• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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

武飞扬头像
遇见迷鹿
帮助3

使用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();
    }
}
学新通

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhgaee
系列文章
更多 icon
同类精品
更多 icon
继续加载