2

What is the best way to check the cc field in an email for correct addresses?

For example if I have cc: mail@mail.com;me@gmail.com;asdfj

How can I detect the asdfj as an invalid address?

I'm using java. The best way I can think of is using a stringtokenizer but I'm not sure how to implement this.

The code I had earlier checks the cc field but only for one email address using this:

if( (!Cc.equals("")) && ccat != Cc.lastIndexOf('@')) {
        System.out.println("CC: address is invalid 2");
        return false;
    }

Thanks.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
user541597
  • 4,247
  • 11
  • 59
  • 87

2 Answers2

3

I'd use JavaMail's InternetAddress class. It has many methods to play with and you won't need a regular expression.

Mosty Mostacho
  • 42,742
  • 16
  • 96
  • 123
  • I like it, but does it handle multiple, concatenated addresses? – Paul Sasik Oct 07 '11 at 05:09
  • @PaulSasik I don't understand the question? The answer gives a link to the API where you can check out the various methods yourself. Thats what I did when I saw this answer because I have never used this class either. I then upvoted the suggestion because I liked what I saw in the API. – camickr Oct 07 '11 at 13:41
  • @camickr: I liked the class as well and looked at the methods. I did not see anything there that would lead me to believe that the class can handle more than a single address at a time. The OP needs to parse out N address and validate each one. Though I agree that the InternetAddress class would be very helpful with the individual validations, i'm concerned that the OP still has to parse, or tokenize the initial input to allow the class to function on individual addresses. – Paul Sasik Oct 07 '11 at 13:50
  • 1
    The parse() method returns an Array of InternetAddresses. Or even if you first split the addresses yourself you should still use the InternetAddress to do the validation. There should be no need to use 3rd party Regex's. – camickr Oct 07 '11 at 14:03
1

You should split the CC field on the semicolon using the String split function and then use a regex email validation pattern on each address.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189