首页 > 解决方案 > 如何从 Outlook 电子邮件内容导航到正确的“URL”

问题描述

我正在成功阅读来自 JAVAX 邮件的 Outlook 电子邮件。但是,当我尝试在电子邮件正文中获取“链接”时,它并没有给出确切的 URL,而是给出了带有一些额外字符的 URL,例如“=3D?*/”。我尝试使用下面的代码,但它没有帮助我。

public List<String> getUrlsFromMessage(Message message, String linkText) throws Exception {
    String html = getMessageContent(message);
    List<String> allMatches = new ArrayList<String>();
    // (<a [^>]+>)
    Matcher matcher = Pattern.compile(" (<a [^>]+>)" + linkText + "</a>").matcher(html);
    while (matcher.find()) {
        String aTag = matcher.group(1);
        allMatches.add(aTag.substring(aTag.indexOf("http"), aTag.indexOf("\">")));
    }
    return allMatches;
}

我也将模式更改为

Pattern linkPattern = Pattern.compile(" <a\\b[^>]*href=\"([^\"]*)[^>]*>(.*?)</a>",
                        Pattern.CASE_INSENSITIVE | Pattern.DOTALL);`

但它仍然给了我错误的 URL。

标签: javaseleniumemail

解决方案


最后,我找到了使用 StringBuilder 检索确切 URL 的解决方案。我所做的是我从字符串中删除了不需要的字符,直到我得到正确的 URL。这可能不是一个好的编码实践,但这是唯一适合我的工作。

StringBuilder build = new StringBuilder(link);
        build.deleteCharAt(43);// Shift the positions front.
        build.deleteCharAt(51);
        build.deleteCharAt(51);
        driver.get(build.toString());

推荐阅读