如何从邮件内容中提取“注册”URL

use*_*333 4 java email

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

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

在超链接“单击此处”上的 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);


    }

}
Run Code Online (Sandbox Code Playgroud)

执行时我得到的输出为

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

如何仅提取 href 值,即https://newstaging.mobilous.com/en/user-register/ ******

请推荐一下,谢谢。

bil*_*dev 5

你很接近了。您正在使用 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));
} 
Run Code Online (Sandbox Code Playgroud)

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

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

告诉你真相,你可能可以使用这种模式(连同更改.group(1)):

Pattern linkPattern = Pattern.compile("href=\"([^\"]*)",  Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
Run Code Online (Sandbox Code Playgroud)