gpt4 book ai didi

java - 将 EWS 协议(protocol)用于 Java API

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:58:26 27 4
gpt4 key购买 nike

我正在尝试使用 EWS 协议(protocol)发送电子邮件。用于相同目的的代码片段:

private String username = "xxx@xxx.com";
private String password = "*****";

public void testMethod() throws Exception {

ExchangeService service = new ExchangeService(
ExchangeVersion.Exchange2010_SP2);
ExchangeCredentials credentials = new WebCredentials(username, password);
service.setTraceEnabled(true);
service.setCredentials(credentials);

try {
service.setUrl(new URI("https://someurl/ews/exchange.asmx"));
} catch (URISyntaxException e) {
e.printStackTrace();
}

EmailMessage msg;
try {
msg = new EmailMessage(service);
msg.setSubject("hello world");
msg.setBody(MessageBody
.getMessageBodyFromText("Sent using the EWS API"));
msg.getToRecipients().add("test@test.com");
msg.send();
} catch (Exception e) {
e.printStackTrace();
}

当我执行上面的代码时,出现如下异常:

microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException: The request failed. The request failed. The remote server returned an error: (401)Unauthorized
at microsoft.exchange.webservices.data.core.request.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:74)
at microsoft.exchange.webservices.data.core.request.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:158)
at

如何确保我有权连接该 URL?可以通过cmd提示检查吗?我该如何解决?

此外,如果有任何域正在使用 EWS 协议(protocol)来测试目的。我用谷歌搜索并了解到 gmail 没有使用它。还请包括一些示例以用于测试目的。

谢谢

最佳答案

我知道为时已晚,但还没有人回答,所以我发布了我的答案。我之前在尝试使用此 API 发送电子邮件时也遇到过类似的问题。

package testEWS;
import java.net.URI;
import java.net.URISyntaxException;
import microsoft.exchange.webservices.data.EmailMessage;
import microsoft.exchange.webservices.data.ExchangeCredentials;
import microsoft.exchange.webservices.data.ExchangeService;
import microsoft.exchange.webservices.data.ExchangeVersion;
import microsoft.exchange.webservices.data.MessageBody;
import microsoft.exchange.webservices.data.WebCredentials;


public class Sendmail {

public static void main(String[] args) throws Exception {
testMethod();
System.out.println("mail sent.. have fun");

}


public static void testMethod() throws Exception {


ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
ExchangeCredentials credentials = new WebCredentials("milidFromWhichMailToBeSent@Host.com ", "Password");
service.setCredentials(credentials);

try {
service.setUrl(new URI("https://myexchange.XXXX.com/EWS/Exchange.asmx"));
} catch (URISyntaxException e) {
e.printStackTrace();
}

EmailMessage msg;
try {
msg = new EmailMessage(service);
msg.setSubject("hello world");
msg.setBody(MessageBody.getMessageBodyFromText("Sent using the EWS API"));
msg.getToRecipients().add("testMail@hOST.com");
msg.send();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Hello");
}
}

如果您想在本地试用,可以使用 Microsoft Outlook 试用。 Outlook 还使用 EWS API。要从 outlook 获取端点,请点击此链接: http://blogs.msdn.com/b/deva/archive/2011/12/02/how-to-get-the-ews-endpoint-url-from-outlook-2007-2010.aspx

关于java - 将 EWS 协议(protocol)用于 Java API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31541879/

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