gpt4 book ai didi

java - EWS Java Api 连接到 Office 365

转载 作者:行者123 更新时间:2023-12-01 11:44:03 27 4
gpt4 key购买 nike

我应该在代码中获取日历详细信息,但我决定首先检查是否可以连接到 Outlook 365。

public static void main(String[] args) throws Exception {

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);//not sure if it should be SP1 or SP2
ExchangeCredentials credentials = new WebCredentials("username@domain.com", "Redstorm#02");
service.setCredentials(credentials);


try {
System.out.println("Check");
service.setUrl(new URI("https://outlook.office365.com/EWS/Exchange.asmx"));
service.setTraceEnabled(true);
EmailMessage msg = new EmailMessage(service);
msg.setSubject("Hello world!");
msg.setBody(MessageBody.getMessageBodyFromText("Sent using the EWS Managed API."));
msg.getToRecipients().add("username@domain.com");
msg.send();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

最佳答案

我在将代码与 Office 365 集成时也遇到了一些问题。目前,使用从以下 URL 构建的 ews-java-api-2.0 jar 似乎对我有用: ews-java-api Github page

import java.net.URI;
import java.net.URISyntaxException;

import microsoft.exchange.webservices.data.core.service.item.EmailMessage;
import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import microsoft.exchange.webservices.data.credential.ExchangeCredentials;
import microsoft.exchange.webservices.data.credential.WebCredentials;
import microsoft.exchange.webservices.data.property.complex.MessageBody;

public class App
{
public static void main( String[] args )
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
ExchangeCredentials credentials = new WebCredentials("USERNAME@DOMAIN", "PASSWORD");
service.setCredentials(credentials);
try
{
service.setUrl(new URI("https://outlook.office365.com/EWS/Exchange.asmx"));
EmailMessage msg = new EmailMessage(service);
msg.setSubject("Hello world!");
msg.setBody(MessageBody.getMessageBodyFromText("Sent using the EWS Java API."));
msg.getToRecipients().add("RECIPIENTMAIL@DOMAIN");
msg.send();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

我使用 Maven 项目进行此测试,这是我的 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>Test</groupId>
<artifactId>Office365Test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Office365Test</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>

</dependencies>
</project>

希望这有帮助。

关于java - EWS Java Api 连接到 Office 365,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29306473/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com