Javamail problem with ñ characters in mail addresses

大憨熊 提交于 2019-12-19 19:43:15

问题


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

essage.setRecipients(Message.RecipientType.TO, internetAddress.parse("somedir.withñchar@m ailserver.com",false));

I'm passing false for the strict parameter, but always i get the error:

javax.mail.internet.AddressException: Local address contains control or whitespace in string ``somedir.with±har@mailserver.com''
at Mailer.main(Mailer.java:386)
Caused by: javax.mail.internet.AddressException: Local address contains control
or whitespace in string ``somedir.with±har@mailserver.com''
        at javax.mail.internet.InternetAddress.checkAddress(InternetAddress.java
:1155)
        at javax.mail.internet.InternetAddress.parse(InternetAddress.java:1044)
        at javax.mail.internet.InternetAddress.parse(InternetAddress.java:575)
        at Mailer.main(Mailer.java:377)

回答1:


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!




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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


来源:https://stackoverflow.com/questions/4608554/javamail-problem-with-%c3%b1-characters-in-mail-addresses

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!