Javamail issue with email address characters

I am having problems with the parse method when using the character ñ:

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

I pass false for a strict parameter, but always get an 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)
+3
source share
6 answers

If you are still experiencing this issue, I advise you to add double quotes to your unusual email address like me. This worked for me because the checkAddress method for InternetAddress refuses to check the quoted address. This is an 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!)

Java Team InternetAddress class, "strict = false" , .

:

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

:

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

, , , Transport.send() - , , .

, , !

+5

, - n-.

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

- unicode escape ( .java )

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

, .

Edit: BTW ( "..." ) true , .

+3

JavaMail - : ASCII @ . RFC 5322 ABNF :

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

, , , .

+1

, UTF8, ASCII, nya . , UTF8. , .java, UTF8. , , .

0

(, ñ), UTF-8.

- RFC 5322. RFC 6532 ( ) .

Check out fooobar.com/questions/1741 / ... for more details.

0
source

Create a new class, extend it, and override the validate function. However, I do not understand why you need this.

public class invalidInternetAddress extends internetAddress {

  @Override
  private static void checkAddress() throws AddressException {
  }
}
-1
source

Source: https://habr.com/ru/post/1783712/


All Articles