Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
608 views
in Technique[技术] by (71.8m points)

spring - Java Send Email , Illegal semicolon, not in group in string

I am having an issue in sending email to a list of recipients.

This is the Exception.

javax.mail.internet.AddressException: Illegal semicolon, not in group in string ``[email protected];[email protected]'' 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)

This is my method to send email.

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;
}

I have also tried Comma separation but the Exception still remains. If i tried a single hard coded email address, it works fine.

Help will be appreciated.

question from:https://stackoverflow.com/questions/65844881/java-send-email-illegal-semicolon-not-in-group-in-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...