gpt4 book ai didi

java - Exchange Web 服务 (EWS) Java Api : 401 Unauthorized

转载 作者:行者123 更新时间:2023-12-05 07:47:25 26 4
gpt4 key购买 nike

当我尝试连接到交换服务器时,出现 401 错误:

 microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException: The request failed. The request failed. The remote server returned an error: (401)Unauthorized

我用 https://ewseditor.codeplex.com/ 测试过(具有相同的 userId、userPw、域和 url)并且它完美地工作。

这是我的实际代码:

public static void main( String[] args )
{
try {
CustomExchangeService service = new CustomExchangeService(version);
ExchangeCredentials credentials = new WebCredentials(userId, userPw,domain);
service.setUrl(new URI(url));
service.setCredentials(credentials);
service.setTraceEnabled(true);
Folder.bind( getInstance().service, WellKnownFolderName.Inbox, PropertySet.IdOnly );

} catch (Exception e) {
e.printStackTrace();
}
}

以及此方法的类 CustomExchangeService:

protected Registry<ConnectionSocketFactory>     createConnectionSocketFactoryRegistry() {
try {
return RegistryBuilder.<ConnectionSocketFactory>create()
.register(EWSConstants.HTTP_SCHEME, new PlainConnectionSocketFactory())
.register(EWSConstants.HTTPS_SCHEME, EwsSSLProtocolSocketFactory.build(
null, NoopHostnameVerifier.INSTANCE
))
.build();
} catch (GeneralSecurityException e) {
throw new RuntimeException(
"Could not initialize ConnectionSocketFactory instances for HttpClientConnectionManager", e
);
}
}

为什么会出现这个错误?可能是因为 SSL 的问题?

谢谢你的帮助

最佳答案

这是个好问题,但我找到了另一种解决方法!

首先,您应该导入 jar ews-java-api-2.0.jar。如果你使用 maven,你可以将以下代码添加到你的 pom.xml

<dependency>
<groupId>com.microsoft.ews-java-api</groupId>
<artifactId>ews-java-api</artifactId>
<version>2.0</version>
</dependency>

其次,你应该新建名为MailUtil.java的java类。一些Exchange服务器默认不启动SMTP服务,所以我们使用Microsoft Exchange WebServices (EWS) 而不是 SMTP 服务。

MailUtil.java

package com.spacex.util;


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

import java.net.URI;

/**
* Exchange send email util
*
* @author vino.dang
* @create 2017/01/08
*/
public class MailUtil {

private static Logger logger = LoggerFactory.getLogger(MailUtil.class);



/**
* send emial
* @return
*/
public static boolean sendEmail() {

Boolean flag = false;
try {
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1); // your server version
ExchangeCredentials credentials = new WebCredentials("vino", "abcd123", "spacex"); // change them to your email username, password, email domain
service.setCredentials(credentials);
service.setUrl(new URI("https://outlook.spacex.com/EWS/Exchange.asmx")); //outlook.spacex.com change it to your email server address
EmailMessage msg = new EmailMessage(service);
msg.setSubject("This is a test!!!"); //email subject
msg.setBody(MessageBody.getMessageBodyFromText("This is a test!!! pls ignore it!")); //email body
msg.getToRecipients().add("123@hotmail.com"); //email receiver
// msg.getCcRecipients().add("test2@test.com"); // email cc recipients
// msg.getAttachments().addFileAttachment("D:\\Downloads\\EWSJavaAPI_1.2\\EWSJavaAPI_1.2\\Getting started with EWS Java API.RTF"); // email attachment
msg.send(); //send email
flag = true;
} catch (Exception e) {
e.printStackTrace();
}

return flag;

}


public static void main(String[] args) {

sendEmail();

}
}

关于java - Exchange Web 服务 (EWS) Java Api : 401 Unauthorized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39770288/

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