赞
踩
日常开发过程中,我们经常需要使用到邮件解析任务,本文主要针对masl方式读取OutLook 微软邮箱附件
提示:以下是本篇文章正文内容,下面案例可供参考
- <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>
代码如下(示例):
- 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();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。