Home > front end >  parsing email in java returns no entry
parsing email in java returns no entry

Time:12-01

currently I'm creating an email service for my hobby project for newly signed up users. This is the relevant part of the code, which causes me some headache:

private Message createEmail(String firstName, String password, String email) throws MessagingException {
        Message message = new MimeMessage(getEmailSession());
        message.setFrom(new InternetAddress(FROM_EMAIL));
        message.setRecipient(Message.RecipientType.TO, InternetAddress.parse(email, false)[0]);
        message.setRecipient(Message.RecipientType.CC, InternetAddress.parse(CC_EMAIL, false)[0]);
        message.setSubject(EMAIL_SUBJECT);
        message.setText("Hello "   firstName   ", \n \n Your new account password is: "   password   "\n \n "  
                "The support team");
        message.setSentDate(new Date());
        message.saveChanges();

        return message;
    }

I have two problems with this line message.setRecipient(Message.RecipientType.TO, InternetAddress.parse(email, false)[0]); (and of course the same problem with the next line below it):

  1. On the internet, if I google after it, everywhere it is used like this:

message.setRecipient(Message.RecipientType.TO, InternetAddress.parse(email, false);

so, without the indexing. But if I remove the indexing, I get an IDE error, which says, that the function requires a type of Address, but it has got InternetAddress[], an array. That's why I put the indexing.

  1. But if I leave the indexing and run the app and register a new user, I get the error in the console: Index 0 out of bounds for length 0. Obviously, the InternetAddress[] array is empty. But why?

What exactly is going on here?

CodePudding user response:

Looking at the docs, it should be new InternetAddress(String, boolean), which

Parse[s] the given string and create[s] an InternetAddress.

instead of InternetAddress.parse(String, boolean), which

Parse[s] the given sequence of addresses into InternetAddress objects.

  •  Tags:  
  • java
  • Related