英文:
Mail Sender Exception (Using mailtrap.io)
问题
我正在尝试实现邮件发送功能(目前我正在使用一些虚假的SMTP),但我不断收到这个错误:
2020-09-19 00:20:38.209 DEBUG 880 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : 未能完成请求:org.springframework.mail.MailSendException: 发送失败的消息:javax.mail.SendFailedException: 没有收件人地址;消息异常(1)为:
失败消息1:javax.mail.SendFailedException: 没有收件人地址
2020-09-19 00:20:38.217 ERROR 880 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : 在路径为 [] 的上下文中为 servlet [dispatcherServlet] 提供服务()抛出异常[请求处理失败;嵌套异常是 org.springframework.mail.MailSendException: 发送失败的消息:javax.mail.SendFailedException: 没有收件人地址;消息异常(1)为:
失败消息1:javax.mail.SendFailedException: 没有收件人地址],根本原因如下:
org.springframework.mail.MailSendException: 发送失败的消息:javax.mail.SendFailedException: 没有收件人地址
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:491) ~[spring-context-support-5.2.2.RELEASE.jar:5.2.2.RELEASE]
...
我的代码:
application.properties:
mail.enable=true
mail.smtp.host=smtp.mailtrap.io
mail.smtp.port=2525
mail.smtp.user=2be3af3d5134be
mail.smtp.pass=2c9ac24fa0808e
MailConfig.java:
@Configuration
public class MailConfig {
@Value("${mail.smtp.host}")
private String host;
@Value("${mail.smtp.port}")
private Integer port;
@Value("${mail.smtp.user}")
private String username;
@Value("${mail.smtp.pass}")
private String password;
@Bean
public JavaMailSender mailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(host);
mailSender.setPort(port);
mailSender.setUsername(username);
mailSender.setPassword(password);
return mailSender;
}
}
一切都从这里开始:
@RequestMapping(value = "/register", method=RequestMethod.POST)
ModelAndView register(ModelAndView modelAndView, @ModelAttribute(value="user") @Valid SiteUser user, BindingResult result) {
modelAndView.setViewName("app.register");
if(!result.hasErrors()) {
userService.register(user);
emailService.sendVerificationEmail(user.getEmail()); // 步骤 1
modelAndView.setViewName("redirect:/");
}
return modelAndView;
}
EmailService.java:
@Service
public class EmailService {
@Autowired
private JavaMailSender mailSender;
@Value("${mail.enable}")
private Boolean isMailEnabled;
private void sendEmail(MimeMessagePreparator preparator) {
if(isMailEnabled) {
mailSender.send(preparator); // 步骤 2
}
}
public void sendVerificationEmail(String emailAddress) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
@Override
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setReplyTo(emailAddress);
message.setFrom(new InternetAddress("socialnetworkmailsender@yandex.com"));
message.setSubject("请验证您的电子邮件地址");
message.setSentDate(new Date());
message.setText("你好!");
}
};
sendEmail(preparator);
}
}
这些是我的类,我在这段代码中留下了 2 个评论:
步骤 1: 我想在用户完成注册后发送验证电子邮件。我正在获取他输入的电子邮件地址,并将该地址传递给 'sendVerificationEmail' 方法。
步骤 2: 在此点之前,一切似乎都没问题,但在这个调用之后,我的控制台上出现错误,我不明白为什么。
注意: 除了 mailtrap.io,我还尝试过使用 mailspons,但我得到了相同的错误。
你知道我为什么会得到这个错误吗?
英文:
I'm trying to implement Mail Sending functionality (For now I'm using some fake smtp) but I keep getting this error:
2020-09-19 00:20:38.209 DEBUG 880 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : Failed to complete request: org.springframework.mail.MailSendException: Failed messages: javax.mail.SendFailedException: No recipient addresses; message exceptions (1) are:
Failed message 1: javax.mail.SendFailedException: No recipient addresses
2020-09-19 00:20:38.217 ERROR 880 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.mail.MailSendException: Failed messages: javax.mail.SendFailedException: No recipient addresses; message exceptions (1) are:
Failed message 1: javax.mail.SendFailedException: No recipient addresses] with root cause
org.springframework.mail.MailSendException: Failed messages: javax.mail.SendFailedException: No recipient addresses
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:491) ~[spring-context-support-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:361) ~[spring-context-support-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:378) ~[spring-context-support-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:366) ~[spring-context-support-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at com.socialnetwork.service.EmailService.sendEmail(EmailService.java:26) ~[classes/:na]
at com.socialnetwork.service.EmailService.sendVerificationEmail(EmailService.java:49) ~[classes/:na]
at com.socialnetwork.controllers.AuthController.register(AuthController.java:50) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
My code:
application.properties:
mail.enable=true
mail.smtp.host=smtp.mailtrap.io
mail.smtp.port=2525
mail.smtp.user=2be3af3d5134be
mail.smtp.pass=2c9ac24fa0808e
MailConfig.java
@Configuration
public class MailConfig {
@Value("${mail.smtp.host}")
private String host;
@Value("${mail.smtp.port}")
private Integer port;
@Value("${mail.smtp.user}")
private String username;
@Value("${mail.smtp.pass}")
private String password;
@Bean
public JavaMailSender mailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(host);
mailSender.setPort(port);
mailSender.setUsername(username);
mailSender.setPassword(password);
return mailSender;
}
}
And everything starts here:
@RequestMapping(value = "/register", method=RequestMethod.POST)
ModelAndView register(ModelAndView modelAndView, @ModelAttribute(value="user") @Valid SiteUser user, BindingResult result) {
modelAndView.setViewName("app.register");
if(!result.hasErrors()) {
userService.register(user);
emailService.sendVerificationEmail(user.getEmail()); // STEP 1
modelAndView.setViewName("redirect:/");
}
return modelAndView;
}
EmailService.java
@Service
public class EmailService {
@Autowired
private JavaMailSender mailSender;
@Value("${mail.enable}")
private Boolean isMailEnabled;
private void sendEmail(MimeMessagePreparator preparator) {
if(isMailEnabled) {
mailSender.send(preparator); // STEP 2
}
}
public void sendVerificationEmail(String emailAddress) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
@Override
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setReplyTo(emailAddress);
message.setFrom(new InternetAddress("socialnetworkmailsender@yandex.com"));
message.setSubject("Please Verify Your Email Address");
message.setSentDate(new Date());
message.setText("Hello there!");
}
};
sendEmail(preparator);
}
}
These are my classes, I left 2 comments in this code:
STEP 1: I want to send verification email after user finish registration. I'm taking email he entered and I'm passing that address to 'sendVerificationEmail' method.
STEP 2: Up to this point everything seems okay but after this call error appears on my console and I don't understand why.
NOTE: Besides mailtrap.io I tried using mailspons as well but I'm getting same error.
Do you know why I'm getting this error?
答案1
得分: 0
我成功修复了这个错误。
问题出在 'sendVerificationEmail' 方法中。
不应该使用:
message.setReplyTo(emailAddress);
我需要使用:
message.setTo(emailAddress);
英文:
I managed to fix the error.
Problem was within 'sendVerificationEmail' method.
Instead of using:
message.setReplyTo(emailAddress);
I needed to use:
message.setTo(emailAddress);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论