[译]如何在带有gSOAP的C ++中使用Exchange Web服务
By robot-v1.0
本文链接 https://www.kyfws.com/applications/how-to-use-exchange-web-service-in-cplusplus-with-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 13 分钟阅读 - 6123 个词 阅读量 0[译]如何在带有gSOAP的C ++中使用Exchange Web服务
原文地址:https://www.codeproject.com/Articles/1119224/How-to-Use-Exchange-Web-Service-in-Cplusplus-With
原文作者:Bethany_Sanders
译文由本站 robot-v1.0 翻译
前言
How to connect EWS with gSOAP to send, delete, and retrieve emails.
如何将EWS与gSOAP连接以发送,删除和检索电子邮件.
介绍(Introduction)
本文将介绍使用gSOAP连接到Exchange Web服务的过程.本文没有定义EWS中的所有功能,但是阅读它应该为您提供使用C ++和gSOAP访问EWS操作的坚实基础.(This article will describe the process of using gSOAP to connect to the Exchange Web Service. All functionalities in EWS are not defined in this article, but reading it should give you a solid basis of using C++ and gSOAP to access EWS operations.)
gSOAP是一个工具包,它通过为C和C ++创建SOAP/XML数据绑定来简化SOAP/XML Web服务的开发.使用gSOAP,调用SOAP Web服务非常容易,因为该工具包会自动生成C/C ++代码来处理发送和接收的SOAP消息.这消除了开发人员编写复杂的代码来解析大型XML消息的需要.(gSOAP is a toolkit that simplifies development with SOAP/XML web services by creating SOAP/XML data bindings for C and C++. With gSOAP, calling SOAP web services is extremely easy because the toolkit auto generates C/C++ code to handle the SOAP messages that are sent and received. This eleminates the developer’s need to write complicated code to parse large XML messages.)
EWS是一种Web服务,用于访问现有电子邮件帐户中的电子邮件.它是SOAP API,因此是展示gSOAP多么有用的完美示例.(EWS is a web service that is used to access emails in a pre existing email account. It is a SOAP API, so it is the perfect example to show just how useful gSOAP can be.)
本文所附的代码由我本人和Ning Xie等分编写.(The code attached to this article was written in equal parts by myself and Ning Xie.)
安装(Installation)
SOAP(gSOAP)
检查一下(Check) 这一页(this page) 了解如何在系统上下载和安装gSOAP.(to see how to download and install gSOAP on your system.)
的OpenSSL(OpenSSL)
您将需要OpenSSL才能将gSOAP与EWS连接.您可以看到如何将其安装在(You will need OpenSSL in order to connect gSOAP with EWS. You can see how to install it on) 他们的网站(their website) .(.)
Outlook电子邮件帐户(Outlook email account)
您将需要一个Outlook电子邮件帐户来测试您的EWS客户端.您可以在上创建一个帐户(You will need an Outlook email account in order to test your EWS client. You can create an account on) 这一页(this page) 如果您还没有.(if you don’t already have one.)
使用gSOAP从EWS WSDL生成C ++绑定(Generating C++ Bindings from the EWS WSDL with gSOAP)
获取.wsdl文件(Obtaining the .wsdl file)
为了运行wsdl2h工具(在下一步中描述),您需要要为其创建头文件的Web服务的wsdl文件.您将需要下载三个文件:(In order for the wsdl2h tool(described in the next step) to run, you need the wsdl file of the web service you wish to create a header file for. You will need to download three files:) Services.wsdl
,(,) messages.xsd
和(, and) types.xsd
.为了下载这些文件,您需要知道您的交换服务器.(. In order to download these files, you need to to know your exchange server.)
Exchange服务器的格式应类似于以下示例:exchange.your_company.com,exchange.your_shcool.edu.(The format of your exchange server should look something like these examples: exchange.your_company.com, exchange.your_shcool.edu.)
去下载(To download) Services.wsdl
,(,) messages.xsd
和(, and) types.xsd
,请在您的网络浏览器中输入以下链接:(, enter these links into your web browser:)
https://{your-exchange-server}/ews/Services.wsdl
https://{your-exchange-server}/ews/messages.xsd
https://{your-exchange-server}/ews/types.xsd
当您访问这些链接时,将提示您输入用户名和密码.使用您的Exchange电子邮件凭据,您可以查看和下载文件.(When you visit these links, you will be prompted to enter a username and password. Use your Exchange email credentials, and you can view and download the files.)
确保将文件保存在同一目录中.另外,您可能需要修改Services.wsdl以确保用于(Make sure to save the files in the same directory. Also, you might need to modify Services.wsdl to make sure that the locations for) messages.xsd
和(and) types.xsd
没有连接到绝对路径(请参阅下文).(are not attached to absolute paths (see below).)
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="http://schemas.microsoft.com/exchange/services/2006/messages" schemaLocation="messages.xsd"/>
<xs:schema/>
<wsdl:types/>
并且该schemaLocation =" types.xsd"在message.xsd中使用而没有路径:(and that schemaLocation=“types.xsd” is used in message.xsd without path:)
<xs:import namespace="http://schemas.microsoft.com/exchange/services/2006/types" schemaLocation="types.xsd"/>
第1步(Step 1)
为了生成EWS头文件,请运行(In order to generate the EWS header file, run the) wsdl2h
EWS WSDL文件以及所需的头文件标题的工具(我们将使用service.h).您将需要该文件(tool with the EWS WSDL file as well as the desired title of you header file (we’ll use service.h). You will need the file) typemap.dat
在您的工作目录中,以便该工具正常工作.(in your working directory in order for the tool to work.)
注意:您的typemap.dat文件仅需要以下两行即可在此示例中正常工作:(Note: Your typemap.dat file only needs the following two lines to work correctly with this example:) ewsmsg ="(ewsmsg = “) http://schemas.microsoft.com/exchange/services/2006/messages(http://schemas.microsoft.com/exchange/services/2006/messages) “(") ewstype ="(ewstype = “) http://schemas.microsoft.com/exchange/services/2006/types(http://schemas.microsoft.com/exchange/services/2006/types) “(")
wsdl2h -o service.h -u Services.wsdl
第2步(Step 2)
现在我们将使用(Now we will use) soapcpp2
生成将我们的代码连接到服务操作的数据绑定.确保(to generate the data bindings that will connect our code to the service operations. Make sure that) stdsoap2.h
和(and) stdsoap2.cpp
位于您的工作目录中,或者gsoap文件夹的路径包含在以下命令中.(are either in either your working directory or the path to the gsoap folder is included in the following command.)
soapcpp2 -j -CL -I /path/to/gSOAP/import service.h
选项-j生成C ++代理类,而-CL表示我们将仅创建客户端.您需要为#import语句中的gSOAP导入文件提供路径.(Option -j produces C++ proxy classes while -CL indicates that we will only create the client side. You will need the path to the gSOAP import file for the #import statements in) service.h
.将生成几个文件.(. Several files will be generated.)
将gSOAP与EWS连接(Connecting gSOAP with EWS)
以下示例将向您展示如何在电子邮件帐户中查找项目,以及如何以编程方式删除该特定项目.我们将使用生成的代理类来这样做.(The following examples will show you how to find an item in your email account and delete that specific item programatically. We will be using the generated proxy classes to do so.)
示例1:SendItem服务操作(从"草稿"文件夹发送电子邮件)(Example 1: SendItem service operation(Sending an email from the Drafts folder))
在我们开始之前,请打开您的Outlook电子邮件帐户.创建一封电子邮件并将自己列为收件人.您可以根据需要设置主题和消息,但这不是必需的.现在,放弃此电子邮件,以便将其保存在"草稿"文件夹中.这是我们将在下一步中发送的电子邮件.为了发送电子邮件,我们将必须知道其ID和更改密钥.为了获得此信息,我们将运行本文所附的tar文件中定义的服务操作:(Before we begin, open up your Outlook email account. Create an email and list yourself as the recipient. You may set the subject and message if you want to, but it is not necessary. Now, abandon this email so that it is saved within the Drafts folder. This is the email that we will send in the next step. In order to send the email, we will have to know its Id and Change Key. In order to get this information, we will run a service operation defined in the tar file attached to this article:) testClient_FindItem.cpp
.该代码将在您的Drafts文件夹中查找并返回所有项目ID.要获取您的商品的ID和更改密钥,请从命令行运行FindItem操作,然后像这样传递您的用户名和密码(我正在使用clang):(. This code will look in your Drafts folder and return all of the item Ids. To get your item’s Id and Change Key, run the FindItem operation from the command line and pass in your username and password like this(I am using clang):)
clang++ -Wno-undefined-bool-conversion -o fi -DWITH_OPENSSL testClient_FindItem.cpp soapC.cpp soapExchangeServiceBindingProxy.cpp /Path/to/stdsoap2.cpp -I. /path/to/gSOAP/gsoap/custom/duration.c -lssl -lcrypto
./fi emailusername@outlook.com email_password
注意:在此步骤的最后,有一个更详细的编译说明.(Note: there is a more detailed explanation of compilation at the end of this step.)
草稿的ID和更改密钥将被打印到屏幕上.(The Id and Change Key of your draft will be printed to your screen.)
注意:在测试EWS客户端时,我更喜欢处理在Drafts文件夹中找到的电子邮件,因为它通常比其他文件夹包含更少的内容.您可以继续使用您喜欢的任何电子邮件位置,但是将用于查找电子邮件ID的代码设置为在"草稿"文件夹中查找.(Note: When testing the EWS client I prefer to handle emails found in the Drafts folder because it generally holds much less content than the other folders. You can continue using whatever email location you prefer,but the code you will use in order to find the email’s Id is set to look in the Drafts folder.)
现在我们已有一个要发送的电子邮件,我们可以开始编码了.可以在以下代码中找到(Now that we have a pre existing email to send, we can begin coding. The following code can be found in) testClient_SendItem.cpp
在本文所附的tar文件中.(in the tar file attached to this article.)
我们必须包括名称空间映射文件以及生成的代理头文件,以便gSOAP生成的代码可以帮助我们访问服务操作.(We must include the namespace mapping file as well as the generated proxy header file so that gSOAP’s generated code can help us access the service operations.)
#include "ExchangeServiceBinding.nsmap"
#include "soapExchangeServiceBindingProxy.h"
#include "soapH.h"
#include <iostream>
#include <string>
接下来,我们将编写代码,以便我们可以从命令行传递用户名和密码参数,并处理登录到您的电子邮件帐户的安全措施.请注意,这也是声明ewsBinding代理变量的地方.这就是允许我们调用SendItem函数,以便将草稿发送到收件箱的原因!(Next, we will write our code so that we can pass in username and password parameters from the command line and handle the security measures of logging into your email accout. Notice this is also where the ewsBinding proxy variable is declared. This is what will allow us to call the SendItem function so that our draft is sent to the inbox!)
static const char *userid;
static const char *passwd;
int main(int argc, char **argv)
{
if(argc>=3)
{
userid = argv[1];
passwd = argv[2];
}
ExchangeServiceBindingProxy ewsBinding("https://outlook.office365.com/EWS/Exchange.asmx");
soap_init1(ewsBinding.soap, SOAP_IO_DEFAULT | SOAP_IO_KEEPALIVE | SOAP_C_UTFSTRING | SOAP_XML_INDENT | SOAP_XML_NOTYPE);
ewsBinding.soap->userid = userid;
ewsBinding.soap->passwd = passwd;
soap_ssl_init();
if(soap_ssl_client_context(ewsBinding.soap,SOAP_SSL_DEFAULT | SOAP_SSL_SKIP_HOST_CHECK,NULL,NULL,"cacerts.pem",NULL,NULL))
{
soap_print_fault(ewsBinding.soap, stderr);
exit(1);
}
// set request server version
_ewstype__RequestServerVersion serVer;
enum ewstype__ExchangeVersionType exchangeVer = ewstype__ExchangeVersionType__Exchange2010_USCORESP2;
serVer.Version = exchangeVer;
ewsBinding.soap_header(NULL, NULL, NULL, &serVer,NULL,NULL);
现在,我们将设置发送到Web服务的请求所需的所有信息.记住设置(Now we will set all of the information needed for the request we send to the web service. Remeber to set) itemtosend.Id
等于从FindItem返回的ID并(equal to the Id returned from FindItem and) change
等于从FindItem返回的Change Key.(equal to the Change Key returned from FindItem.)```c++
ewstype__NonEmptyArrayOfBaseItemIdsType itemArray;
itemArray.__size_NonEmptyArrayOfBaseItemIdsType = 1;
__ewstype__union_NonEmptyArrayOfBaseItemIdsType member;
ewstype__ItemIdType itemtosend; itemtosend.Id = “"; std::string change = “"; itemtosend.ChangeKey = &change;
member.ItemId = &itemtosend; itemArray.__union_NonEmptyArrayOfBaseItemIdsType = &member;
ewsmsg__SendItemType request; __ewsmsg__SendItemResponse response; request.ItemIds = &itemArray; request.SaveItemToFolder = true;
现在该打电话给服务并处理我们的结果了.(*Now it is time to call the service and handle our results.*)
```c++
if(ewsBinding.SendItem(&request, response)==SOAP_OK)
{
ewsmsg__ResponseMessageType* iirmt
= response.ewsmsg__SendItemResponse->ResponseMessages->__union_ArrayOfResponseMessagesType->SendItemResponseMessage;
if(iirmt != NULL)
{
if(iirmt->ResponseClass == ewstype__ResponseClassType__Error)
std::cout << *iirmt->__ResponseMessageType_sequence->MessageText <<std::endl;
else
std::cout << "Message Sent!";
}
}
else
ewsBinding.soap_stream_fault(std::cerr);
ewsBinding.destroy();
}
这将在变量中发送SOAP请求(This will send the SOAP request in the variable) request
并发送回SOAP响应消息(and send the SOAP response message back in) response
.呼唤(. Calling) ewsBinding.destroy()
删除所有托管数据.(deletes all of the managed data.)
这是完整的testClient_SendItem.cpp:(Here is testClient_SendItem.cpp in its entirety:)
#include "ExchangeServiceBinding.nsmap"
#include "soapExchangeServiceBindingProxy.h"
#include "soapH.h"
#include <iostream>
#include <string>
static const char *userid;
static const char *passwd;
int main(int argc, char **argv)
{
if(argc>=3)
{
userid = argv[1];
passwd = argv[2];
}
ExchangeServiceBindingProxy ewsBinding("https://outlook.office365.com/EWS/Exchange.asmx");
soap_init1(ewsBinding.soap, SOAP_IO_DEFAULT | SOAP_IO_KEEPALIVE | SOAP_C_UTFSTRING | SOAP_XML_INDENT | SOAP_XML_NOTYPE);
ewsBinding.soap->userid = userid;
ewsBinding.soap->passwd = passwd;
soap_ssl_init();
if(soap_ssl_client_context(ewsBinding.soap,SOAP_SSL_DEFAULT | SOAP_SSL_SKIP_HOST_CHECK,NULL,NULL,"cacerts.pem",NULL,NULL))
{
soap_print_fault(ewsBinding.soap, stderr);
exit(1);
}
// set request server version
_ewstype__RequestServerVersion serVer;
enum ewstype__ExchangeVersionType exchangeVer = ewstype__ExchangeVersionType__Exchange2010_USCORESP2;
serVer.Version = exchangeVer;
ewsBinding.soap_header(NULL, NULL, NULL, &serVer,NULL,NULL);
ewstype__NonEmptyArrayOfBaseItemIdsType itemArray;
itemArray.__size_NonEmptyArrayOfBaseItemIdsType = 1;
__ewstype__union_NonEmptyArrayOfBaseItemIdsType member;
ewstype__ItemIdType itemtosend;
itemtosend.Id = "AAMkAGQ0MDg4MjMwLTYyZjEtNDc1Zi1hNWQwLWY4ZGFiODIyY2ZiOABGAAAAAADQJ5K0osuKQatv50kfuGI4BwALbeOJ+btaT4I3Qf8SQ+rtAAAAAAEQAAALbeOJ+btaT4I3Qf8SQ+rtAAKA2ysiAAA=";
std::string change = "CQAAABYAAAALbeOJ+btaT4I3Qf8SQ+rtAAKBBRvI";
itemtosend.ChangeKey = &change;
member.ItemId = &itemtosend;
itemArray.__union_NonEmptyArrayOfBaseItemIdsType = &member;
ewsmsg__SendItemType request;
__ewsmsg__SendItemResponse response;
request.ItemIds = &itemArray;
request.SaveItemToFolder = true;
if(ewsBinding.SendItem(&request, response)==SOAP_OK)
{
ewsmsg__ResponseMessageType* iirmt
= response.ewsmsg__SendItemResponse->ResponseMessages->__union_ArrayOfResponseMessagesType->SendItemResponseMessage;
if(iirmt != NULL)
{
if(iirmt->ResponseClass == ewstype__ResponseClassType__Error)
std::cout << *iirmt->__ResponseMessageType_sequence->MessageText <<std::endl;
else
std::cout << "Message Sent!";
}
}
else
ewsBinding.soap_stream_fault(std::cerr);
ewsBinding.destroy();
}
编译SendItem示例(Compiling the SendItem Example)
我们必须使用OPENSSL进行编译,以确保安全地传输我们的凭据.这需要OpenSSL和OpenSSL加密库.(We must compile with OPENSSL to ensure that our credentials are transmitted securely. This requires the OpenSSL and OpenSSL crypto libraries.)
您将需要包括通往(You will need to include the paths to the) stdsoap2.cpp
和(and) duration.c
gSOAP库中的文件.(files in the gSOAP library.)
与FindItem编译类似,您将需要将用户名和密码参数传递给可执行文件.(Similarly to the FindItem compilation, you will need to pass username and password arguments to the executable.)
clang++ -Wno-undefined-bool-conversion -o si -DWITH_OPENSSL testClient_SendItem.cpp soapC.cpp soapExchangeServiceBindingProxy.cpp /Path/to/stdsoap2.cpp -I. /path/to/gSOAP/gsoap/custom/duration.c -lssl -lcrypto
./si emailusername@outlook.com email_password
运行SendItem操作时,您应该在收件箱中收到一封新电子邮件!(When yout run the SendItem operation, you should recieve a new email in your inbox!)
示例2:DeleteItem服务操作(Example 2: DeleteItem service operation)
如前所述,为简单起见,我更喜欢从"草稿"文件夹中工作,因此我们将在"草稿"文件夹中删除一封电子邮件.如果您希望从收件箱或其他位置删除,则可以轻松地在(As I mentioned before, I prefer to work from the Drafts folder for simplicity, so we will be deleting an email in the Drafts folder. If you wish to delete from the inbox or other locations, you can easily modify the code in) testClient_FindItem.cpp
在其他文件夹中搜索(设置变量(to search in other folders (set the variable) dfit
至(to) ewstype__DistinguishedFolderIdNameType__inbox
在第80行搜索收件箱).(on line 80 to search in the inbox).)
像我们之前所做的那样,在您的电子邮件帐户中创建草稿,然后运行FindItem服务操作以获取项目的ID. DeleteItem服务操作不需要更改密钥.(Like we did before, create a draft in your email account, and then run the FindItem service operation to get the item’s Id. The Change Key is not needed for the DeleteItem service operation.)
clang++ -Wno-undefined-bool-conversion -o fi -DWITH_OPENSSL testClient_FindItem.cpp soapC.cpp soapExchangeServiceBindingProxy.cpp /Path/to/stdsoap2.cpp -I. /path/to/gSOAP/gsoap/custom/duration.c -lssl -lcrypto
./fi emailusername@outlook.com email_password
现在我们已有一个要发送的电子邮件,我们可以开始编码了.可以在以下代码中找到(Now that we have a pre existing email to send, we can begin coding. The following code can be found in) testClient_DeleteItem.cpp
在本文所附的tar文件中.(in the tar file attached to this article.)
我们必须包括名称空间映射文件以及生成的代理头文件,以便gSOAP生成的代码可以帮助我们访问服务操作.(We must include the namespace mapping file as well as the generated proxy header file so that gSOAP’s generated code can help us access the service operations.)
#include "ExchangeServiceBinding.nsmap"
#include "soapExchangeServiceBindingProxy.h"
#include "soapH.h"
#include <iostream>
#include <string>
现在,我们设置了凭据,以便我们可以登录到电子邮件帐户以及创建用于调用Web服务并接收结果的代理.(Now we set up the credentials so we can log into the email account as well as create the proxy we will use to call the web service and receive the results.)
static const char *userid;
static const char *passwd;
int main(int argc, char **argv)
{
if(argc>=3)
{
userid = argv[1];
passwd = argv[2];
}
ExchangeServiceBindingProxy ewsBinding("https://outlook.office365.com/EWS/Exchange.asmx");
ewsmsg__DeleteItemType request;
__ewsmsg__DeleteItemResponse response;
soap_init1(ewsBinding.soap, SOAP_IO_DEFAULT | SOAP_IO_KEEPALIVE | SOAP_C_UTFSTRING | SOAP_XML_INDENT | SOAP_XML_NOTYPE);
ewsBinding.soap->userid = userid;
ewsBinding.soap->passwd = passwd;
soap_ssl_init();
if(soap_ssl_client_context(ewsBinding.soap,SOAP_SSL_DEFAULT | SOAP_SSL_SKIP_HOST_CHECK,NULL,NULL,"cacerts.pem",NULL,NULL))
{
soap_print_fault(ewsBinding.soap, stderr);
exit(1);
}
// set request server version
_ewstype__RequestServerVersion serVer;
enum ewstype__ExchangeVersionType exchangeVer = ewstype__ExchangeVersionType__Exchange2010_USCORESP2;
serVer.Version = exchangeVer;
ewsBinding.soap_header(NULL, NULL, NULL, &serVer,NULL,NULL);
现在是时候设置DeleteItem服务完成其工作所需的所有属性了.不要忘记设置(It is now time to set all of the attributes the DeleteItem service needs to do its job. Don’t forget to set) item.Id
到FindItem服务返回的项目ID.(to the item Id that the FindItem service returned.)
ewstype__ItemIdType item;
item.Id = "";
__ewstype__union_NonEmptyArrayOfBaseItemIdsType member;
member.ItemId = &item;
ewstype__NonEmptyArrayOfBaseItemIdsType itemArray;
itemArray.__size_NonEmptyArrayOfBaseItemIdsType = 1;
itemArray.__union_NonEmptyArrayOfBaseItemIdsType = &member;
request.ItemIds = &itemArray;
request.DeleteType = ewstype__DisposalType__SoftDelete;
ewstype__AffectedTaskOccurrencesType ATOType = ewstype__AffectedTaskOccurrencesType__SpecifiedOccurrenceOnly;
request.AffectedTaskOccurrences = &ATOType;
// Identify how meeting cancellations are handled.
ewstype__CalendarItemCreateOrDeleteOperationType CalType = ewstype__CalendarItemCreateOrDeleteOperationType__SendOnlyToAll;
request.SendMeetingCancellations = &CalType;
现在剩下要做的就是调用Web服务了!(Now all that’s left to do is call the web service!)
if(ewsBinding.DeleteItem(&request, response)==SOAP_OK)
{
ewsmsg__ResponseMessageType *iirmt
= response.ewsmsg__DeleteItemResponse->ResponseMessages->__union_ArrayOfResponseMessagesType->DeleteItemResponseMessage;
if(iirmt != NULL)
{
if (iirmt->ResponseClass == ewstype__ResponseClassType__Error)
std::cout << *iirmt->__ResponseMessageType_sequence->MessageText <<std::endl;
else
std::cout << "Message deleted!";
}
}
else
ewsBinding.soap_stream_fault(std::cerr);
ewsBinding.destroy();
}
同样,不要忘了打电话(Again, don’t forget to call) ewsBinding.destroy()
清理所有托管数据!(to clean up all managed data!)
这是(Here is) testClient_DeleteItem.cpp
整体上:(in its entirety:)
#include "ExchangeServiceBinding.nsmap"
#include "soapExchangeServiceBindingProxy.h"
#include "soapH.h"
#include <iostream>
#include <string>
static const char *userid;
static const char *passwd;
int main(int argc, char **argv)
{
if(argc>=3)
{
userid = argv[1];
passwd = argv[2];
}
ExchangeServiceBindingProxy ewsBinding("https://outlook.office365.com/EWS/Exchange.asmx");
ewsmsg__DeleteItemType request;
__ewsmsg__DeleteItemResponse response;
soap_init1(ewsBinding.soap, SOAP_IO_DEFAULT | SOAP_IO_KEEPALIVE | SOAP_C_UTFSTRING | SOAP_XML_INDENT | SOAP_XML_NOTYPE);
ewsBinding.soap->userid = userid;
ewsBinding.soap->passwd = passwd;
soap_ssl_init();
if(soap_ssl_client_context(ewsBinding.soap,SOAP_SSL_DEFAULT | SOAP_SSL_SKIP_HOST_CHECK,NULL,NULL,"cacerts.pem",NULL,NULL))
{
soap_print_fault(ewsBinding.soap, stderr);
exit(1);
}
// set request server version
_ewstype__RequestServerVersion serVer;
enum ewstype__ExchangeVersionType exchangeVer = ewstype__ExchangeVersionType__Exchange2010_USCORESP2;
serVer.Version = exchangeVer;
ewsBinding.soap_header(NULL, NULL, NULL, &serVer,NULL,NULL);
ewstype__ItemIdType item;
item.Id = "AAMkAGQ0MDg4MjMwLTYyZjEtNDc1Zi1hNWQwLWY4ZGFiODIyY2ZiOABGAAAAAADQJ5K0osuKQatv50kfuGI4BwALbeOJ+btaT4I3Qf8SQ+rtAAAAAAEQAAALbeOJ+btaT4I3Qf8SQ+rtAAKA2yshAAA=";
__ewstype__union_NonEmptyArrayOfBaseItemIdsType member;
member.ItemId = &item;
ewstype__NonEmptyArrayOfBaseItemIdsType itemArray;
itemArray.__size_NonEmptyArrayOfBaseItemIdsType = 1;
itemArray.__union_NonEmptyArrayOfBaseItemIdsType = &member;
request.ItemIds = &itemArray;
request.DeleteType = ewstype__DisposalType__SoftDelete;
ewstype__AffectedTaskOccurrencesType ATOType = ewstype__AffectedTaskOccurrencesType__SpecifiedOccurrenceOnly;
request.AffectedTaskOccurrences = &ATOType;
// Identify how meeting cancellations are handled.
ewstype__CalendarItemCreateOrDeleteOperationType CalType = ewstype__CalendarItemCreateOrDeleteOperationType__SendOnlyToAll;
request.SendMeetingCancellations = &CalType;
if(ewsBinding.DeleteItem(&request, response)==SOAP_OK)
{
ewsmsg__ResponseMessageType *iirmt
= response.ewsmsg__DeleteItemResponse->ResponseMessages->__union_ArrayOfResponseMessagesType->DeleteItemResponseMessage;
if(iirmt != NULL)
{
if (iirmt->ResponseClass == ewstype__ResponseClassType__Error)
std::cout << *iirmt->__ResponseMessageType_sequence->MessageText <<std::endl;
else
std::cout << "Message deleted!";
}
}
else
ewsBinding.soap_stream_fault(std::cerr);
ewsBinding.destroy();
}
编译DeleteItem示例(Compiling the DeleteItem example)
我们必须使用OPENSSL进行编译,以确保安全地传输我们的凭据.这需要OpenSSL和OpenSSL加密库.(We must compile with OPENSSL to ensure that our credentials are transmitted securely. This requires the OpenSSL and OpenSSL crypto libraries.)
您将需要包括通往(You will need to include the paths to the) stdsoap2.cpp
和(and) duration.c
gSOAP库中的文件.(files in the gSOAP library.)
与FindItem编译类似,您将需要将用户名和密码参数传递给可执行文件.(Similarly to the FindItem compilation, you will need to pass username and password arguments to the executable.)
clang++ -Wno-undefined-bool-conversion -o di -DWITH_OPENSSL testClient_DeleteItem.cpp soapC.cpp soapExchangeServiceBindingProxy.cpp /Path/to/stdsoap2.cpp -I. /path/to/gSOAP/gsoap/custom/duration.c -lssl -lcrypto
./di emailusername@outlook.com email_password
使用软删除选项,电子邮件将不会发送到您的删除文件夹.如果您只想将项目移动到删除文件夹,请使用(With the soft delete option, the email will not be sent to your delete folder. If you would only like to move the item to the delete folder, use) testClient_MoveItem.cpp
(包含在本文所附的tar中).编译并运行此示例时,您的电子邮件应从"草稿"文件夹中消失.((included in the tar attached to this article). When you compile and run this example, your email should dissapear from the drafts folder.)
更多例子(More Examples)
既然您已经熟悉了如何连接gSOAP和EWS以创建客户端,那么您就可以自己完成tar文件中的其他示例! tar中包含的文件如下:(Now that you have familiarized yourself with connecting gSOAP and EWS to make a client, you can work through the other examples in the tar file on your own! The files included in the tar are as follows:)
testClient_CreateAttachment.cpp
testClient_DeleteAttachment.cpp
testClient_CreateFolder.cpp
testClient_DeleteFolder.cpp
testClient_FindFolder.cpp
testClient_MoveFolder.cpp
testClient_CreateItem.cpp
testClient_DeleteItem.cpp(described in this article)
testClient_FindItem.cpp
testClient_SendItem.cpp(described in this article)
testClient_MoveItem.cpp
testClient_PwdExpirationDate.cpp
如果您想了解其他服务操作,(If you want to learn about other service operations,) 这一页(this page) 将非常有用.如果您想了解有关CreateItem的信息,可以在搜索栏中搜索CreateItemtype类,然后将显示一个CreateItemtype类的结果(ExchangeWebServices).如果单击该结果,将提供示例代码,其中显示了需要设置哪些属性才能调用服务操作(代码在C#中).当您输入并以Function_Nametype Class的格式运行时,此搜索有效.(will be very useful. If you wanted to learn about CreateItem, you can search CreateItemtype Class in the search bar and a resut of CreateItemtype Class (ExchangeWebServices) will show. If you click on that result, exampe code is provided which shows which attributes need to be set in order to call the service operation (code is in C#). This search works when you enter and function in the format of Function_Nametype Class.)
如果您有任何疑问,请随时发表评论!(If you have any questions feel free to leave a comment!)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C++ Mobile Office 新闻 翻译