Не могу получить пароль от электронной почты, используя JavaMail - PullRequest
0 голосов
/ 22 сентября 2018

На сайте есть возможность сброса пароля.Я хочу написать код, в котором новый пароль из письма будет сохранен в переменной, которая будет использоваться в тесте редактирования профиля.Я использую JavaMail.

Авторизация к почтовому ящику проходит, находит непрочитанное письмо с определенной темой, с помощью getContent () отображает тело сообщения в виде text / html, но не могу понять, какчтобы ввести сообщение и получить оттуда пароль.

    import javax.mail.*;
    import javax.mail.search.SubjectTerm;
    import java.util.Properties;

    public class Mail {
    //public static String pass = null;

    public Message getEmail(String emailID, String password, String subjectToBeSearched) throws Exception {

        Properties props = System.getProperties();
        props.setProperty("mail.store.protocol", "imaps");

        Session session = Session.getDefaultInstance(props, null);
        Store store = session.getStore("imaps");
        store.connect("imap.gmail.com", emailID, password);

        Folder folder = store.getFolder("INBOX");
        folder.open(Folder.READ_WRITE);

        Message[] messages = null;
        boolean mailFound = false;
        Message email = null;

        for (int i = 0; i < 30; i++) {
            messages = folder.search(new SubjectTerm(subjectToBeSearched), folder.getMessages());
            if (messages.length == 0) {
                Thread.sleep(10000);
            }
        }

        for (Message mail : messages) {
            if (!mail.isSet(Flags.Flag.SEEN)) {
                email = mail;
                mailFound = true;
                System.out.println("Mail is found");
            }
        }
        System.out.println(email.getContent());
        if (!mailFound) {
            throw new Exception("Could not found Email");
        }
        return email;
    }
}

Я вызываю в модульном тесте

    import org.junit.Test;

    public class MailTest {
    @Test
    public void getEmail() throws Exception {
        Mail mail = new Mail();
        mail.getEmail("test@gmail.com","11111","Password reset");
    }
}

Что я получаю, используя GetContent ()

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>PSD2HTML</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <style type="text/css">
        .ExternalClass{display:block !important;}
        * {
            -webkit-text-size-adjust:none;
            -webkit-text-resize:100%;
            text-resize:100%;
        }
    </style>
</head>
<body style="margin:0; padding:0;" bgcolor="#ededeb" link="#0086da">
<style type="text/css">
    * {
        -webkit-text-size-adjust:none;
        -webkit-text-resize:100%;
        text-resize:100%;
    }
</style>
<table width="100%" cellspacing="0" cellpadding="0" bgcolor="#ededeb">
    <!--[if IE]>
    <tr>
        <td align="center">
            <table width="609" cellpadding="0" cellspacing="0" border="0" align="center">
    <![endif]-->
    <!--[if mso]>
    <tr>
        <td align="center">
            <table width="609" cellpadding="0" cellspacing="0" border="0" align="center">
    <![endif]-->
    <tr>
        <td align="center" style="padding-top:47px; padding-bottom:46px; padding-left:5px; padding-right:5px;">
            <div style="max-width:599px; margin:0 auto;">
                <table width="100%" cellpadding="0" cellspacing="0" align="center" bgcolor="#ffffff" style="border-bottom:1px solid #cdd1d2; margin:0 auto;">
                    <tr><td colspan="3" height="27" style="line-height:0; font-size:0;"></td></tr>
                    <tr>
                        <td width="8%"></td>
                        <td width="84%">
                                                            <table width="100%" cellpadding="0" cellspacing="0">
                                                                                                                    <tr>
                                            <td style="font:14px Arial, Helvetica, sans-serif; line-height:17px; color:#4c515a;">Hello, </td>
                                        </tr>
                                        <tr><td height="10" style="line-height:0; font-size:0;"></td></tr>
                                                                            <tr>
        <td style="font:14px Arial, Helvetica, sans-serif; line-height:17px; color:#4c515a;"><div style="line-height:10px; width:100%;"><span style="line-height:17px;">We have reset your password. Your login information with the new password is below.</span></div></td>
    </tr>
    <tr><td height="14" style="line-height:0; font-size:0;"></td></tr>
                                </table>
                                                    </td>
                        <td width="8%"></td>
                    </tr>
                                        <tr>
                        <td colspan="3" bgcolor="#f9f9f9">
                            <table width="100%" cellpadding="0" cellspacing="0" bgcolor="#f9f9f9">
                                <tr><td colspan="3" height="16" style="line-height:0; font-size:0;"></td></tr>
                                <tr>
                                    <td width="8%"></td>
                                    <td width="84%">
                                        <table width="100%" cellpadding="0" cellspacing="0">
    <tr>
        <td width="78" style="font:14px Arial, Helvetica, sans-serif; line-height:10px; color:#4c515a; text-align:right;"><span style="line-height:17px;"><strong style="color:#000;">Login:&nbsp;</strong></span></td>
        <td style="font:14px Arial, Helvetica, sans-serif; line-height:10px; color:#4c515a;"><span style="line-height:17px;">vlada.aleksandrova@p2h.com</span></td>
    </tr>
    <tr>
        <td width="78" style="font:14px Arial, Helvetica, sans-serif; line-height:10px; color:#4c515a; text-align:right;"><span style="line-height:17px;"><strong style="color:#000;">Password:&nbsp;</strong> </span></td>
        <td style="font:14px Arial, Helvetica, sans-serif; line-height:10px; color:#4c515a;"><span style="line-height:17px;">vtKKLzwb</span></td>
    </tr>
</table>

Так что мне нужно как-то разобрать

...