添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

最新推荐文章于 2023-09-10 14:42:21 发布
最新推荐文章于 2023-09-10 14:42:21 发布 阅读量4k

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

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

我在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 = "[email protected]";
    private String to = "[email protected]";
    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();

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

使用java mail 发送邮件报错处理com.sun.mail.smtp.SMTPSendFailedException: 501 Mail from address must be same as 使用java mail发送邮件报错处理
意思是:com.sun.mail.smtp.SMTPSendFailedException:来自地址的501邮件必须与授权用户相同 在这个项目里,邮件服务是腾讯企业邮提供的,不支持高权限的用户使用不用的用户作为发件人。所以使用统一的系统账号进行作为发件人。 转载于:https://www.cnblogs.com/jiml/p/9357048.html...
使用者创建的账号,系统会自动分配一个密码,不可直接使用这个密码,否则会出现上述异常,我们要用分配的账号, 通过http://exmail.qq.com/login 这个地址登录,此时会提示更换密码,按照提示更换密码,然后使用新密码就可以正常发送邮件 使用java mail 发送邮件报错处理com.sun.mail.smtp.SMTPSendFailedException: 501 Mail from address must be same as
使用java mail 发送邮件报错处理com.sun.mail.smtp.SMTPSendFailedException: 501 Mail from address must be same as youhetest: 必须加上message.setFrom() 要不然就会报这个501错误 Nacos安装指南 CSDN-Ada助手: CS入门 技能树或许可以帮到你:https://edu.csdn.net/skill/gml?utm_source=AI_act_gml