如何解决Spring Boot-从应用程序发送电子邮件显示禁止错误
我想在我的Spring Boot应用程序中实现一个简单的反馈服务。用户必须提供三种不同类型的数据:发件人电子邮件,主题和消息。点击发送按钮后,它将把该消息发送到我们提到的电子邮件中(已发送到 application.yml 文件中)。但是,当我尝试使用swagger测试它时,它显示 Forbidden 错误并返回我的控制器路径。
编辑:我在控制器的send方法中放置了一个断点,但是Intellij无法识别该断点。我不确定,在邮件配置中可能仍然存在问题。
昂扬的I / p:
{
"body": "Hello","subject": "Hii","toemail": "The GMail I mentioned in application.yml"
}
点击发送后日志没有错误:
2020-08-25 20:09:54.692 INFO 73786 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path ''
2020-08-25 20:09:54.693 INFO 73786 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
2020-08-25 20:09:54.716 INFO 73786 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
2020-08-25 20:09:54.756 INFO 73786 --- [ main] s.d.s.w.s.ApiListingReferenceScanner : Scanning for api listing references
2020-08-25 20:09:55.033 INFO 73786 --- [ main] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [localhost:5672]
2020-08-25 20:09:55.087 INFO 73786 --- [ main] o.s.a.r.c.CachingConnectionFactory : Created new connection: rabbitConnectionFactory#6f9a3325:0/SimpleConnection@123e9094 [delegate=amqp://guest@127.0.0.1:5672/,localPort= 47922]
2020-08-25 20:09:55.541 INFO 73786 --- [ main] o.s.amqp.rabbit.core.RabbitAdmin : Auto-declaring a non-durable,auto-delete,or exclusive Queue (RabbitMqQueue) durable:false,auto-delete:false,exclusive:false. It will be redeclared if the broker stops and is restarted while the connection factory is alive,but all messages will be lost.
2020-08-25 20:09:55.613 INFO 73786 --- [ main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-08-25 20:09:55.884 INFO 73786 --- [ main] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-08-25 20:09:55.902 INFO 73786 --- [ main] c.a.ApplicationServiceApplication : Started ApplicationServiceApplication in 20.195 seconds (JVM running for 21.471)
2020-08-25 20:11:04.500 INFO 73786 --- [nio-8081-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-08-25 20:11:04.500 INFO 73786 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-08-25 20:11:04.514 INFO 73786 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 14 ms
Swagger UI错误: 响应正文
{
"timestamp": "2020-08-25T13:58:21.281+00:00","status": 403,"error": "Forbidden","message": "","path": "/feedback/send"
}
响应标题
cache-control: no-cache,no-store,max-age=0,must-revalidate
connection: keep-alive
content-type: application/json
date: Tue,25 Aug 2020 13:58:21 GMT
expires: 0
keep-alive: timeout=60
pragma: no-cache
transfer-encoding: chunked
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
我的MailConfig类:
@Component
public class MailConfig {
@Value("${spring.mail.host}")
String host;
@Value("${spring.mail.port}")
int port;
@Value("${spring.mail.username}")
String username;
@Value("${spring.mail.password}")
String password;
@Bean
public JavaMailSender getJavaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(host);
mailSender.setPort(port);
mailSender.setUsername(username);
mailSender.setPassword(password);
Properties props = mailSender.getJavaMailProperties();
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.auth","true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.debug","true");
return mailSender;
}
}
控制器类:
@RestController
@RequestMapping("/feedback")
public class FeedbackController {
@Autowired
EmailServiceImpl emailServiceImpl;
private MailConfig mailConfig;
@PostMapping("/send")
public String sendmail(@RequestBody FeedbackRequestDTO requestDTO) {
emailServiceImpl.sendSimpleMessage(requestDTO.getToemail(),requestDTO.getSubject(),requestDTO.getBody());
return "Thanks for your Feedback";
}
}
RequestDTO类:
@Data
public class FeedbackRequestDTO {
private String toemail;
private String subject;
private String body;
}
服务等级:
@Component
public class EmailServiceImpl implements FeedbackService{
@Autowired
private JavaMailSender emailSender;
public void sendSimpleMessage(
String to,String subject,String text) {
SimpleMailMessage mailMessage = new SimpleMailMessage();
if(checkIfNull(to)){
try{
mailMessage.setFrom("A Random GMail Address");
mailMessage.setTo(to);
mailMessage.setSubject("New Feedback: " + subject);
mailMessage.setText(text);
emailSender.send(mailMessage);
}catch (Exception e){
e.printStackTrace();
}
}
else{
System.out.println("Your Input is invalid,please fix");
}
}
private boolean checkIfNull(String email) {
if(isValid(email)){
return true;
}
return false;
}
public static boolean isValid(String email) {
String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\."+
"[a-zA-Z0-9_+&*-]+)*@" +
"(?:[a-zA-Z0-9-]+\\.)+[a-z" +
"A-Z]{2,7}$";
Pattern pat = Pattern.compile(emailRegex);
if (email == null)
return false;
return pat.matcher(email).matches();
}
}
Application.yml文件:
spring:
mail:
host: smtp.gmail.com
port: 587
username: ourGMailAccount@gmail.com
password: OurPasswordGoesHere
properties:
mail:
smtp:
auth: true
starttls:
enable: true
server:
port: 8081
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。