java Javamail 问题?邮件地址中的字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4608554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javamail problem with ? characters in mail addresses
提问by Guille
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:
我为严格参数传递了 false,但总是出现错误:
javax.mail.internet.AddressException: Local address contains control or whitespace in string ``somedir.with±[email protected]''
at Mailer.main(Mailer.java:386)
Caused by: javax.mail.internet.AddressException: Local address contains control
or whitespace in string ``somedir.with±[email protected]''
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)
回答by André
If you are still with this problem, I advise you to add double quotes to your unusual e-mail addresslike I did. It worked for me, because checkAddress method of InternetAddress give up to validate quoted address. That's the easy and quick solution.
如果您仍然遇到这个问题,我建议您像我一样在您不寻常的电子邮件地址中添加双引号。它对我有用,因为 InternetAddress 的 checkAddress 方法放弃验证引用的地址。这就是简单快捷的解决方案。
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' bugthat even receiving "strict=false"at constructor is checking the e-mail syntax, when it shouldn't.
完美和正确的解决方案是 Java 团队修复InternetAddress 类的错误,即使在构造函数中收到“strict=false”也在检查电子邮件语法,而它不应该这样做。
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.
创建扩展 Address 的新类的方法将不起作用,因为 Transport.send() 出于某种原因似乎再次验证了地址,并且也启动了异常。
Please, let us know if it helped!
请让我们知道它是否有帮助!
回答by Stephen P
Verify that your ? is actuallyan n-tilde. The following works perfectly for me
验证您的 ? 是真正的正波浪线。以下非常适合我
InternetAddress[] list = InternetAddress.parse("[email protected]", false);
for (InternetAddress a : list) {
System.out.println("[" + a + "]");
}
It also works when replacing the ? with a unicode escape (in the source .java file)
它也适用于替换 ? 使用 unicode 转义(在源 .java 文件中)
InternetAddress[] list = InternetAddress.parse("fred.jo\[email protected]", 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.
编辑:顺便说一句,我还使用了 parse("...") 与地址和没有第二个参数,并通过true
严格,这两个也有效。
回答by jun
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.
也许您的文本编辑器没有将您的源文件保存为 UTF8,而是保存为 ASCII,所以您的 Nya 变得一团糟。确保将其保存为 UTF8。当 .java 文件保存为 UTF8 时,可能还需要一个编译选项来编译它。我有一段时间没有这样做了,但我记得它是那样的。
回答by dkarp
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:
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
Make sure that the address is correct before you go any further.
在继续之前,请确保地址正确。
回答by Nicolas Riousset
To send emails to addresses with non printable characters (like ?), you must encode the email address using UTF-8.
要将电子邮件发送到带有不可打印字符(如 ?)的地址,您必须使用 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.
原始RFC 5322 Internet Message Format不允许重音字符。但由于RFC 6532(国际化电子邮件标头)提供了一种在电子邮件地址中使用国际字符的方法。
Check this StackOverflow questionfor more details.
查看此 StackOverflow 问题以获取更多详细信息。
回答by Mark Baijens
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 {
}
}