java发送电子邮件,非法分号,不在组中的字符串

ni65a41a  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(754)

我在向收件人列表发送电子邮件时遇到问题。
这是个例外。

javax.mail.internet.AddressException: Illegal semicolon, not in group in string ``abc@gmail.com;xyz@gmail.com'' at position 23
at javax.mail.internet.InternetAddress.parse(InternetAddress.java:1021)
at javax.mail.internet.InternetAddress.parse(InternetAddress.java:728)
at javax.mail.internet.InternetAddress.parse(InternetAddress.java:705)
at org.springframework.mail.javamail.MimeMessageHelper.parseAddress(MimeMessageHelper.java:735)
at org.springframework.mail.javamail.MimeMessageHelper.setTo(MimeMessageHelper.java:614)
at com.vyzor.emailmanager.service.impl.EmailServiceImpl.sendEmail(EmailServiceImpl.java:166)
at com.vyzor.emailmanager.restapi.EmailController.sendEmail(EmailController.java:56)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

这是我发电子邮件的方法。

public Email sendEmail(Email email){

    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

    List<EmailConfig> emailConfigs = emailConfigRepository.findAllByClientWebId(email.getClient().getWebId());

    EmailConfig emailConfig;

    if (emailConfigs.iterator().hasNext()){
        emailConfig = emailConfigs.iterator().next();
    }else{
        throw new ResourceNotFoundException(HttpStatus.NOT_FOUND.getReasonPhrase());
    }

    javaMailSender.setHost(emailConfig.getHostName());
    javaMailSender.setPort(emailConfig.getPortNo());
    javaMailSender.setUsername(emailConfig.getEmail());
    javaMailSender.setPassword(emailConfig.getPassword());

    Properties javaMailProperties = new Properties();
    javaMailProperties.put("mail.smtp.starttls.enable", "true");
    javaMailProperties.put("mail.smtp.auth", "true");
    javaMailProperties.put("mail.transport.protocol", "smtp");
    javaMailProperties.put("mail.debug", "true");

    try{

        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message,true);
        helper.setFrom(email.getFromEmail());

        String recipients = email.getEmailRecipient().stream()
            .map(EmailRecipient::getEmailAddress)
            .collect( Collectors.joining( ";" ) );

        helper.setTo(recipients);               // I am having issue here
        helper.setSubject(email.getSubject());
        helper.setText(email.getBody());

        javaMailSender.setJavaMailProperties(javaMailProperties);
        javaMailSender.send(message);
    }catch (Exception exception){
        exception.printStackTrace();
        email.setStatus(EmailStatus.FAILED);
        return null;
    }

    return email;
}

我也尝试过逗号分隔,但例外仍然存在。如果我尝试一个硬编码的电子邮件地址,它的工作很好。
我们将不胜感激。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题