如何从通知电子邮件中的链接获取网址

时间:2016-08-17 06:52:00

标签: email url hyperlink outlook webdriver

我希望用web驱动程序编写一个脚本,我需要等待通知电子邮件,然后需要从该电子邮件的链接中获取URL。

1 个答案:

答案 0 :(得分:0)

识别URL的模式(基于RFC 3986)

msgListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    MyClass favouriteMessage = favourites.get(position);
                                   String favorite_message = favouriteMessage.STRING_NAME;
                                }
                            });

更新: 如果您想阅读FROM等邮件标题,我建议使用Mime4J

private static final Pattern urlPattern = Pattern.compile(
        "(?:^|[\\W])((ht|f)tp(s?):\\/\\/|www\\.)"
                + "(([\\w\\-]+\\.){1,}?([\\w\\-.~]+\\/?)*"
                + "[\\p{Alnum}.,%_=?&#\\-+()\\[\\]\\*$~@!:/{};']*)",
        Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);


//Usage: email content goes as input here..
Matcher matcher = urlPattern.matcher("foo bar http://example.com baz");

while (matcher.find()) {
    int matchOffsetStart = matcher.start(1);
    int matchOffsetEnd = matcher.end();
    // now you have the offsets of a URL match
}

Message msg = new Message(new FileInputStream("mime.msg")); 会给你。同样,你可以提取你想要的任何东西。

Refer for more details here