尝试仅从退回的电子邮件(gmail)中检索电子邮件ID并将其打印在屏幕上

时间:2016-10-25 13:08:42

标签: java

你们都知道邮递员的电子邮件是怎样的 enter image description here

我从另一个类中获取此字符串,这是一种响应。从这个字符串,我只想要提到的电子邮件地址,也是字符串格式,所以验证不适用,我试过正则表达式(娱乐我的愚蠢)。我不能接受索引,因为我们现在有gmail,但在生产中,客户端可以正确访问任何电子邮件。什么是你的解决方案。我只想要电子邮件地址。

1 个答案:

答案 0 :(得分:1)

您可以使用正则表达式匹配电子邮件,也可以使用String.replace()String.split()方法从字符串中提取电子邮件:

 //remove leading text
 message = message.replace("Delivery to the following recipient failed permanently:", "");

 //remove and trim newlines
 message = message.replace("\n", "").replace("\r", "").trim();

 //the first part of split message will be the email address
 String emailAddress = message.split()[0];