Sping boot 整合mail读取OutLook 微软邮箱

前言

日常开发过程中,我们经常需要使用到邮件解析任务,本文主要针对masl方式读取OutLook 微软邮箱附件


提示:以下是本篇文章正文内容,下面案例可供参考

一、使用步骤

1.引入

		<dependency>
			<groupId>javax.mail</groupId>
			<artifactId>mail</artifactId>
			<version>1.4.7</version>
		</dependency>
		<dependency>
			<groupId>com.microsoft.azure</groupId>
			<artifactId>msal4j</artifactId>
			<version>1.11.0</version>
		</dependency>

2.读入数据

代码如下(示例):

    public static void main(String[] args) {
        try {
            // 配置MSAL客户端
            ConfidentialClientApplication application = ConfidentialClientApplication.builder(
                            "client_Id",
                            ClientCredentialFactory.createFromSecret("Secret"))
                    .authority("authority").build();

            ClientCredentialParameters clientCredentialParam = ClientCredentialParameters
                    .builder(Collections.singleton(msalProperties.getScope())).build();
            IAuthenticationResult authenticationResult = application.acquireToken(clientCredentialParam).join();

            Properties properties = new Properties();
            properties.setProperty("mail.store.protocol", "pop3s");

            // 创建邮件会话
            Session session = Session.getInstance(properties);
            // 连接到outlook.live.com邮箱
            Store store = session.getStore("pop3s");
            store.connect("outlook.office365.com", "邮箱", authenticationResult.accessToken());

            // 打开收件箱
            Folder folder = store.getFolder("INBOX");
            folder.open(Folder.READ_ONLY);

            // 只读取未读邮件
            FlagTerm flagTerm = new FlagTerm(new Flags(Flags.Flag.SEEN), false);

            // 获取所有未读邮件
            Message[] messages = folder.search(flagTerm);
            System.out.println(messages);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fashionable ape man

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值