从Selenium Java连接到OUTLOOK365

从Selenium Java连接到OUTLOOK365,java,selenium,outlook,jakarta-mail,Java,Selenium,Outlook,Jakarta Mail,我有一个应用程序,它在输入凭据后向我的电子邮件发送验证码。我需要从收件箱中读取验证码。我正在使用Outlook,我的组织使用Outlook 365的MAPI协议 有人能帮我吗?这是将打印所有收件箱邮件的完整代码。您需要提取正文并找到验证代码 package solution; import java.util.Properties; import javax.mail.Folder; import javax.mail.Message; import javax.mail.Messagin

我有一个应用程序,它在输入凭据后向我的电子邮件发送验证码。我需要从收件箱中读取验证码。我正在使用Outlook,我的组织使用Outlook 365的MAPI协议


有人能帮我吗?

这是将打印所有收件箱邮件的完整代码。您需要提取正文并找到验证代码

 package solution;
import java.util.Properties;


import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
public class test {
    public static String username =null;
    public static String password1 =null;
    public static void check(String host, String storeType, String user,
                             String password)
    { username= user;
        password1 = 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.fallback", "false");
            // properties.setProperty("mail.imao.socketFactory.port",
            //       String.valueOf("993"));
            //properties.put("mail.imap.auth", "false");
            // properties.put("mail.debug.auth", "true");
            properties.put("mail.imaps.ssl.trust", "*"); // This is the most IMP property

            Session emailSession = Session.getDefaultInstance(properties);





            //create the POP3 store object and connect with the pop server

            Store store = emailSession.getStore("imaps"); //try imap or impas
            store.connect(host, user, password);
            //  store.connect(host, 993, 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);

            for (int i = 0, n = messages.length; i < 10; i++) {
                Message message = messages[i];
                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) {

        String host = "outlook.office365.com";
        String mailStoreType = "imaps";
        String username = "USERNAME";
        String password = "PASSWORD";

        check(host, mailStoreType, username, password);

    }
}
封装解决方案;
导入java.util.Properties;
导入javax.mail.Folder;
导入javax.mail.Message;
导入javax.mail.MessaginException;
导入javax.mail.NoSuchProviderException;
导入javax.mail.PasswordAuthentication;
导入javax.mail.Session;
导入javax.mail.Store;
公开课考试{
公共静态字符串username=null;
公共静态字符串password1=null;
公共静态无效检查(字符串主机、字符串存储类型、字符串用户、,
字符串(密码)
{用户名=用户;
password1=密码;
试一试{
//创建属性字段
属性=新属性();
properties.put(“mail.imap.host”,host);
properties.put(“mail.imap.port”、“993”);
//properties.put(“mail.imap.starttls.enable”、“true”);
//properties.setProperty(“mail.imap.socketFactory.fallback”、“false”);
//properties.setProperty(“mail.imao.socketFactory.port”,
//字符串。value of(“993”);
//properties.put(“mail.imap.auth”、“false”);
//properties.put(“mail.debug.auth”、“true”);
properties.put(“mail.imaps.ssl.trust”、“*”;//这是最重要的属性
会话emailSession=Session.getDefaultInstance(属性);
//创建POP3存储对象并连接pop服务器
Store Store=emailSession.getStore(“imaps”);//试试imap或impas
store.connect(主机、用户、密码);
//store.connect(主机,993,用户,密码);
//创建文件夹对象并将其打开
文件夹emailFolder=store.getFolder(“收件箱”);
emailFolder.open(文件夹只读);
//从数组中的文件夹中检索邮件并打印
Message[]messages=emailFolder.getMessages();
System.out.println(“messages.length----”+messages.length);
for(int i=0,n=messages.length;i<10;i++){
消息消息=消息[i];
System.out.println(“-------------------------------------”);
System.out.println(“电子邮件号码”+(i+1));
System.out.println(“主题:+message.getSubject());
System.out.println(“From:”+message.getFrom()[0]);
System.out.println(“文本:+message.getContent().toString());
}
//关闭存储和文件夹对象
emailFolder.close(假);
store.close();
}捕获(无此提供异常e){
e、 printStackTrace();
}捕获(消息异常e){
e、 printStackTrace();
}捕获(例外e){
e、 printStackTrace();
}
}
公共静态void main(字符串[]args){
字符串host=“outlook.office365.com”;
字符串mailStoreType=“imaps”;
字符串username=“username”;
字符串password=“password”;
检查(主机、邮箱类型、用户名、密码);
}
}
此外,您还需要以下依赖项

 <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>

javax.mail
邮件
1.4.7

您好,谢谢您的回复。但我的组织正在使用MAPI协议。我已经尝试使用类似的代码使用IMAPS,但失败了,因为它给出了连接超时错误,我同意。您的outlook协议可能是IMAPS。我的组织对Outlook使用MAPI协议。因此,正在寻找使用MAPI协议的解决方案