Java jdk8代码写一个指定exchange 2019 版本的收取收件箱邮箱代码的demo并列举需要导入的pom.xml文件
时间: 2024-03-05 10:47:23 浏览: 133
pom.xml android maven 工程
5星 · 资源好评率100%
以下是使用JavaMail API和Exchange Web Services API收取Exchange 2019版本的收件箱邮件的示例代码,需要导入的pom.xml文件如下:
```xml
<!-- JavaMail API -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<!-- Exchange Web Services API -->
<dependency>
<groupId>com.microsoft.ews-java-api</groupId>
<artifactId>ews-java-api</artifactId>
<version>2.0</version>
</dependency>
```
```java
import java.net.URI;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.PropertySet;
import microsoft.exchange.webservices.data.core.enumeration.property.BasePropertySet;
import microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName;
import microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException;
import microsoft.exchange.webservices.data.core.service.item.EmailMessage;
import microsoft.exchange.webservices.data.search.FindItemsResults;
import microsoft.exchange.webservices.data.search.ItemView;
public class ExchangeEmailDemo {
public static void main(String[] args) throws Exception {
String username = "your-email-address";
String password = "your-password";
String server = "https://your-exchange-server/EWS/Exchange.asmx";
Properties properties = new Properties();
properties.setProperty("mail.store.protocol", "imap");
properties.setProperty("mail.imap.host", server);
Session session = Session.getDefaultInstance(properties);
Store store = session.getStore();
store.connect(username, password);
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
MimeMessage[] messages = (MimeMessage[]) inbox.getMessages();
for (MimeMessage message : messages) {
System.out.println("Subject: " + message.getSubject());
System.out.println("From: " + ((InternetAddress) message.getFrom()[0]).getAddress());
System.out.println("Body: " + message.getContent());
}
inbox.close(false);
store.close();
}
}
```
注意:需要将代码中的 `your-email-address`、`your-password` 和 `your-exchange-server` 替换为你自己的邮箱地址、密码和Exchange服务器地址。同时,需要根据实际情况修改代码中的收件箱名称、收件数量等参数。
阅读全文