【发布时间】:2021-02-12 01:55:50
【问题描述】:
我无法通过 Postman 测试我的 Spring Boot 邮件发件人 API。 我想用spring boot创建一个api并发送邮件。为此,我在下面分享了应用程序属性、服务和控制器类。但是,当我通过邮递员提供 url 并将 json 正文添加到 post 方法时,我得到一个错误。我该如何解决这个问题?
邮递员回复
{
"timestamp": "2020-10-29T15:26:23.300+00:00",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/mail/send"
}
邮递员测试参数
url: http://localhost:8080/mail/send
method: post
JSON body:
{
"to" : "mail@gmail.com",
"textBody" : "TEST",
"topic" : "TEST"
}
应用属性
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.gmail.com
spring.mail.username=mail@gmail.com
spring.mail.password=password
spring.mail.port=587
spring.mail.protocol=smtp
spring.mail.test-connection=false
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
服务类
@Service
public class SendEmailService {
@Autowired
private JavaMailSender javaMailSender;
public void sendEmail(String to, String body, String topic){
try {
System.out.println("Mail sending is started");
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom("mutlueren01@gmail.com");
simpleMailMessage.setTo(to);
simpleMailMessage.setSubject(topic);
simpleMailMessage.setText(body);
javaMailSender.send(simpleMailMessage);
System.out.println("Mail sending is completed");
}catch (Exception e){
System.out.println("Error occured: "+e.getMessage());
}
}
}
控制器
@Controller
public class SendEmailController {
@Autowired
SendEmailService sendEmailService;
@RequestMapping(value = "/mail/send/", method = RequestMethod.POST)
public void sendMail(@RequestParam("to") String to, @RequestParam("textBody") String textBody, @RequestParam("topic") String topic) {
sendEmailService.sendEmail(to, textBody, topic);
}
}
【问题讨论】:
-
你应该提供你的
WebMvcConfigurer
标签: java spring spring-boot spring-mvc