【问题标题】:how to get url from link inside the notification email如何从通知电子邮件中的链接获取 url
【发布时间】:2016-12-23 17:11:47
【问题描述】:

我正在寻找使用网络驱动程序编写一个脚本,我需要等待通知电子邮件,然后需要从该电子邮件的链接中获取 URL。

【问题讨论】:

  • 您更喜欢哪种语言来解析电子邮件?
  • 我的脚本使用 java

标签: email url hyperlink outlook webdriver


【解决方案1】:

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

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
}

更新: 如果你想阅读FROM之类的消息头,我建议使用Mime4J

Message msg = new Message(new FileInputStream("mime.msg"));

msg.getFrom() 会给你从。同样,您可以提取任何您想要的内容。

Refer for more details here

【讨论】:

  • 感谢 Sunil 提供的模式,在此之前我需要先等待并验证电子邮件,然后再解析该电子邮件的内容。
  • 验证电子邮件 - 您是指正确的发件人吗?我没有完全理解你
  • 我的步骤应该是 - 1.需要检查预期的电子邮件是否已发送,2.然后我将解析url并继续。
  • 感谢 Sunil 的另一个补充。我需要通知电子邮件事件。例如:如果任何用户从任何网站提交申请,然后用户会收到一封提交电子邮件。我需要检查该用户的电子邮件是否触发,而不是检查标题、电子邮件的模式或该电子邮件的任何其他内容。希望这是有道理的。
  • 刚刚飞过我的头:P 无论如何,根据我的理解,您需要一个收件箱监听器来接收用户的传入邮件。对吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-08
  • 2021-02-14
  • 2016-01-20
  • 1970-01-01
  • 1970-01-01
  • 2011-12-20
相关资源
最近更新 更多