java - 如何从邮件内容中提取 "registration"URL

标签 java email

我成功地使用“JAVAMail”读取了 gmail-email 的内容,并且我能够将它存储在一个字符串中。现在我想从内容(字符串)中获取特定的注册 URL。我该怎么做,该字符串包含大量标签和 href,但我只想提取在下面提到的语句中存在的单词“单击此处”的超链接中提供的 URL

"Please <a class="h5" href="https://newstaging.mobilous.com/en/user-register/******" target="_blank">click here</a> to complete your registration".

在超链接“点击这里”的 url 上

href="https://newstaging.mobilous.com/en/user-register/******"target="_blank"

我已经用下面的代码试过了

package email;

import java.util.ArrayList;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;

public class emailAccess {

    public static void check(String host, String storeType, String user,
             String password) 
          {
             try {

             //create properties field
             Properties properties = new Properties();

             properties.put("mail.imap.host",host);
             properties.put("mail.imap.port", "993");
             properties.put("mail.imap.starttls.enable", "true");
             properties.setProperty("mail.imap.socketFactory.class","javax.net.ssl.SSLSocketFactory");
               properties.setProperty("mail.imap.socketFactory.fallback", "false");
               properties.setProperty("mail.imap.socketFactory.port",String.valueOf(993));
             Session emailSession = Session.getDefaultInstance(properties);

             //create the POP3 store object and connect with the pop server
             Store store = emailSession.getStore("imap");

             store.connect(host, user, password);

             //create the folder object and open it
             Folder emailFolder = store.getFolder("INBOX");
             emailFolder.open(Folder.READ_ONLY);

             // retrieve the messages from the folder in an array and print it
             Message[] messages = emailFolder.getMessages();
             System.out.println("messages.length---" + messages.length);
                int n=messages.length;
                for (int i = 0; i<n; i++) {
                Message message = messages[i];
                ArrayList<String> links = new ArrayList<String>();
                if(message.getSubject().contains("Thank you for signing up for AppExe")){
                String desc=message.getContent().toString();

              // System.out.println(desc);
              Pattern linkPattern = Pattern.compile(" <a\\b[^>]*href=\"[^>]*>(.*?)</a>",  Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
               Matcher pageMatcher = linkPattern.matcher(desc);

               while(pageMatcher.find()){
                   links.add(pageMatcher.group());
               } 
                }else{
                System.out.println("Email:"+ i + " is not a wanted email");
                }
                for(String temp:links){
                if(temp.contains("user-register")){
                    System.out.println(temp);
                }
                }

                /*System.out.println("---------------------------------");
                System.out.println("Email Number " + (i + 1));
                System.out.println("Subject: " + message.getSubject());
                System.out.println("From: " + message.getFrom()[0]);
                System.out.println("Text: " + message.getContent().toString());*/

             }
             //close the store and folder objects
             emailFolder.close(false);
             store.close();

             } catch (NoSuchProviderException e) {
                e.printStackTrace();
             } catch (MessagingException e) {
                e.printStackTrace();
             } catch (Exception e) {
                e.printStackTrace();
             }
          }


    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String host = "imap.gmail.com";
         String mailStoreType = "imap";
         String username = "rameshakur@gmail.com";
         String password = "*****";

         check(host, mailStoreType, username, password);


    }

}

在执行时我得到了输出

我怎样才能只提取 href 值,即 https://newstaging.mobilous.com/en/user-register/ ******

求推荐,谢谢

最佳答案

你很接近。您正在使用 group(),但遇到了一些问题。这里有一些应该可以工作的代码,只替换了您已有的一些代码:

Pattern linkPattern = Pattern.compile(" <a\\b[^>]*href=\"([^\"]*)[^>]*>(.*?)</a>",  Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
Matcher pageMatcher = linkPattern.matcher(desc);
while(pageMatcher.find()){
    links.add(pageMatcher.group(1));
} 

我所做的只是更改您的模式,使其显式查找 href 属性的结束引号,然后将模式中您要查找的字符串部分括在括号中。

我还向 pageMather.group() 方法添加了一个参数,因为它需要一个参数。

说实话,您可能只使用这种模式(连同 .group(1) 更改):

Pattern linkPattern = Pattern.compile("href=\"([^\"]*)",  Pattern.CASE_INSENSITIVE|Pattern.DOTALL);

关于java - 如何从邮件内容中提取 "registration"URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36707939/

相关文章:

java - 在 cucumber 测试期间无法执行目标 org.apache.maven.plugins :maven-surefire-plugin:2. 19.1 :test,

java - 自定义检查组合框

java - 使 JLabel 可复制?

带有链接图像的 HTML 电子邮件在 Outlook 中中断

vba - 使用 Lotus Notes Social Edition 从 Excel 数据生成电子邮件

java - 升级到 spring 3.2 但邮件似乎消失了?

Java int 比较,无法找出问题所在

java - 如何使用 Apache MINA 库编写 SFTP 客户端

asp.net - 如何设置电子邮件超链接的样式以在ASP.Net MVC3中使用Razor显示?

email - Postfix,隔离多个站点的邮件 header ,这样如果一个站点被阻止/列入黑名单,共享服务器的其他站点也不会被列入黑名单