邮件发件人例外(使用 mailtrap.io)

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        : 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]

我的代码:

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()); // 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);
    }
}

这些是我的 类,我在这段代码中留下了 2 条评论:

第 1 步: 我想在用户完成注册后发送验证邮件。我正在接收他输入的电子邮件,并将该地址传递给 'sendVerificationEmail' 方法。

第 2 步: 到目前为止,一切似乎都还不错,但是在我的控制台上出现此调用错误之后,我不明白为什么。

注意: 除了 mailtrap.io 我也尝试使用 mailspons 但是我遇到了同样的错误。

你知道我为什么会收到这个错误吗?

我设法修复了错误。

问题出在“sendVerificationEmail”方法中。

而不是使用:

message.setReplyTo(emailAddress);

我需要使用:

message.setTo(emailAddress);