java 在 JavaMail 中使用 message.getFrom() 时只显示要显示的电子邮件地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5214372/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:00:10  来源:igfitidea点击:

Getting only email address to display when using message.getFrom() in JavaMail

javajavamail

提问by Rhys12341111

Currently when using JavaMail if I use getFrom()to decompose a message into its separate parts the getFrom()will also display the name of the sender. This may be a simple question but how do you make it so only the email address is returned. Sorry if this is a simple question but I cannot seem to find an answer.

目前,在使用 JavaMail 时,如果我getFrom()用来将消息分解为单独的部分,getFrom()则还将显示发件人的姓名。这可能是一个简单的问题,但您如何才能只返回电子邮件地址。对不起,如果这是一个简单的问题,但我似乎找不到答案。

回答by dkarp

As it turns out, the address has already been parsed for you. Because of JavaMail's silly extra layer of abstraction, it's returning InternetAddressobjects as their Addresssuperclass. Addressobjects are pretty much useless. You need to cast them back down to InternetAddressand then just get the email part:

事实证明,该地址已经为您解析过了。由于 JavaMail 愚蠢的额外抽象层,它返回InternetAddress对象作为它们的Address超类。 Address对象几乎没用。您需要将它们转换回InternetAddress,然后获取电子邮件部分:

Address[] froms = message.getFrom();
String email = froms == null ? null : ((InternetAddress) froms[0]).getAddress();