使用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();
}
}
注:此文章仅为笔记,如有侵权等行为,可联系作者删除或修改文章。
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfhgaee
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13