Javamail problem with ñ characters in mail addresses

后端 未结 6 625
花落未央
花落未央 2021-01-19 01:16

I\'m having problems with parse method when usising ñ character:

essage.setRecipients(Message.RecipientType.TO, internetAddress.parse(\"somedir.         


        
相关标签:
6条回答
  • 2021-01-19 01:22

    Create a new class, extend it and override the validate functie. However I don't see why you would want that.

    public class invalidInternetAddress extends internetAddress {
    
      @Override
      private static void checkAddress() throws AddressException {
      }
    }
    
    0 讨论(0)
  • 2021-01-19 01:27

    If you are still with this problem, I advise you to add double quotes to your unusual e-mail address like I did. It worked for me, because checkAddress method of InternetAddress give up to validate quoted address. That's the easy and quick solution.

    Example:

    unusual_email_without_at_sign_or_with_accentuátion (won't work)
    "unusual_email_without_at_sign_or_with_accentuátion" (it works!)
    

    The perfect and correct solution would be Java Team to fix InternetAddress class' bug that even receiving "strict=false" at constructor is checking the e-mail syntax, when it shouldn't.

    Example of the bug:

    InternetAddress i = new InternetAddress("unusual_email_without_at_sign_or_with_accentuátion ", false)
    

    Example of the workaround solution:

    InternetAddress i = new InternetAddress("\"unusual_email_without_at_sign_or_with_accentuátion\"", false)
    

    The approach of creating a new class that extends Address won't work, since Transport.send() for some reason seems to validate again the address and the exception is also launched.

    Please, let us know if it helped!

    0 讨论(0)
  • 2021-01-19 01:27

    JavaMail's address parser is telling you something very important here: You may only use normal printable ASCII characters on the left side of the @ sign in an email address. Check out RFC 5322 for the ABNF for valid email addresses:

    addr-spec       =   local-part "@" domain
    
    local-part      =   dot-atom / quoted-string / obs-local-part
    
    dot-atom        =   [CFWS] dot-atom-text [CFWS]
    
    dot-atom-text   =   1*atext *("." 1*atext)
    
    atext           =   ALPHA / DIGIT /    ; Printable US-ASCII
                        "!" / "#" /        ;  characters not including
                        "$" / "%" /        ;  specials.  Used for atoms.
                        "&" / "'" /
                        "*" / "+" /
                        "-" / "/" /
                        "=" / "?" /
                        "^" / "_" /
                        "`" / "{" /
                        "|" / "}" /
                        "~"
    
    quoted-string   =   [CFWS]
                        DQUOTE *([FWS] qcontent) [FWS] DQUOTE
                        [CFWS]
    
    qcontent        =   qtext / quoted-pair
    
    quoted-pair     =   ("\" (VCHAR / WSP)) / obs-qp
    
    qtext           =   %d33 /             ; Printable US-ASCII
                        %d35-91 /          ;  characters not including
                        %d93-126 /         ;  "\" or the quote character
                        obs-qtext
    

    Make sure that the address is correct before you go any further.

    0 讨论(0)
  • 2021-01-19 01:31

    Maybe your text editor isn't saving your sourcefile as UTF8 but as ASCII so your nya is getting messed up. Make sure you're saving it as UTF8. There may also be a compile option necessary to compile a .java file when its saved as UTF8. I haven't done it in a while but I remember it being that way.

    0 讨论(0)
  • 2021-01-19 01:33

    Verify that your ñ is actually an n-tilde. The following works perfectly for me

    InternetAddress[] list = InternetAddress.parse("fred.joñes@example.com", false);
    for (InternetAddress a : list) {
        System.out.println("[" + a + "]");
    }
    

    It also works when replacing the ñ with a unicode escape (in the source .java file)

    InternetAddress[] list = InternetAddress.parse("fred.jo\u00F1es@example.com", false);
    

    You could try replacing it like this just as a test.

    Edit: BTW I had also used parse("...") with the address and no second parameter, and passed true for strict, both of which also worked.

    0 讨论(0)
  • 2021-01-19 01:48

    To send emails to addresses with non printable characters (like ñ), you must encode the email address using UTF-8.

    Accented characters were not allowed by original RFC 5322 Internet Message Format. But since, RFC 6532 (Internationalized Email Headers) offered a way to use international characters in email addresses.

    Check this StackOverflow question for more details.

    0 讨论(0)
提交回复
热议问题