namespace ewsSync
{
class Program
{
static void Main(string[] args)
{
//Deal with Self Signed Certificate Errors
ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate
certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
};
//
Program coControlobject = new Program();
String snServername = "servername";
String unUserName = "UserName";
String pwPassword = "password";
String dnDomain = "domain";
ExchangeServiceBinding ewsServiceBinding = new ExchangeServiceBinding();
ewsServiceBinding.Credentials = new NetworkCredential(unUserName,pwPassword,dnDomain);
ewsServiceBinding.Url = @"https://" + snServername + "/EWS/exchange.asmx";
ContactItemType cnContact = new ContactItemType();
cnContact.FileAs = "Fred,Flint";
cnContact.GivenName = "Fred";
cnContact.Surname = "Flint";
cnContact.Subject = "Fred,Flint";
cnContact.EmailAddresses = new EmailAddressDictionaryEntryType[1];
EmailAddressDictionaryEntryType emEmailAddress = new EmailAddressDictionaryEntryType();
emEmailAddress.Key = EmailAddressKeyType.EmailAddress1;
emEmailAddress.Value = "fred@bedrock.com";
cnContact.EmailAddresses[0] = emEmailAddress;
ExtendedPropertyType emEmailType = new ExtendedPropertyType();
PathToExtendedFieldType epExPath = new PathToExtendedFieldType();
epExPath.PropertySetId = "00062004-0000-0000-C000-000000000046";
epExPath.PropertyId = 0x8082;
epExPath.PropertyIdSpecified = true;
epExPath.PropertyType = MapiPropertyTypeType.String;
emEmailType.ExtendedFieldURI = epExPath;
emEmailType.Item = "SMTP";
ExtendedPropertyType emDisplayName = new ExtendedPropertyType();
PathToExtendedFieldType epExPath1 = new PathToExtendedFieldType();
epExPath1.PropertySetId = "00062004-0000-0000-C000-000000000046";
epExPath1.PropertyId = 0x8080;
epExPath1.PropertyIdSpecified = true;
epExPath1.PropertyType = MapiPropertyTypeType.String;
emDisplayName.ExtendedFieldURI = epExPath1;
emDisplayName.Item = "fred@bedrock.com";
ExtendedPropertyType emCardDisplayName = new ExtendedPropertyType();
PathToExtendedFieldType epExPath2 = new PathToExtendedFieldType();
epExPath2.PropertySetId = "00062004-0000-0000-C000-000000000046";
epExPath2.PropertyId = 0x8080;
epExPath2.PropertyIdSpecified = true;
epExPath2.PropertyType = MapiPropertyTypeType.String;
emCardDisplayName.ExtendedFieldURI = epExPath2;
emCardDisplayName.Item = "fred,Flient(fred@bedrock.com)";
cnContact.ExtendedProperty = new ExtendedPropertyType[3];
cnContact.ExtendedProperty[0] = emEmailType;
cnContact.ExtendedProperty[1] = emDisplayName;
cnContact.ExtendedProperty[2] = emCardDisplayName;
ItemIdType iiCreateItemid = coControlobject.CreateContact(ewsServiceBinding, cnContact);
iiCreateItemid = coControlobject.CreateAttachment(ewsServiceBinding, "c:\\spreasheet.xls",
iiCreateItemid);
}
private ItemIdType CreateContact(ExchangeServiceBinding ewsServiceBinding, ContactItemType
emMessage)
{
ItemIdType iiItemid = new ItemIdType();
CreateItemType ciCreateItemRequest = new CreateItemType();
ciCreateItemRequest.MessageDisposition = MessageDispositionType.SaveOnly;
ciCreateItemRequest.MessageDispositionSpecified = true;
ciCreateItemRequest.SavedItemFolderId = new TargetFolderIdType();
DistinguishedFolderIdType cfContactsFolder = new DistinguishedFolderIdType();
cfContactsFolder.Id = DistinguishedFolderIdNameType.contacts;
ciCreateItemRequest.SavedItemFolderId.Item = cfContactsFolder;
ciCreateItemRequest.Items = new NonEmptyArrayOfAllItemsType();
ciCreateItemRequest.Items.Items = new ItemType[1];
ciCreateItemRequest.Items.Items[0] = emMessage;
CreateItemResponseType createItemResponse = ewsServiceBinding.CreateItem(ciCreateItemRequest);
if (createItemResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Error)
{
Console.WriteLine("Error Occured");
Console.WriteLine(createItemResponse.ResponseMessages.Items[0].MessageText);
}
else
{
ItemInfoResponseMessageType rmResponseMessage = createItemResponse.ResponseMessages.Items[0] as
ItemInfoResponseMessageType;
Console.WriteLine("Item was created");
Console.WriteLine("Item ID : " + rmResponseMessage.Items.Items[0].ItemId.Id.ToString());
Console.WriteLine("ChangeKey : " + rmResponseMessage.Items.Items[0].ItemId.ChangeKey.ToString());
iiItemid.Id = rmResponseMessage.Items.Items[0].ItemId.Id.ToString();
iiItemid.ChangeKey = rmResponseMessage.Items.Items[0].ItemId.ChangeKey.ToString();
}
return iiItemid;
}
private ItemIdType CreateAttachment(ExchangeServiceBinding ewsServiceBinding, String fnFileName,
ItemIdType iiCreateItemid)
{
ItemIdType iiAttachmentItemid = new ItemIdType();
FileStream fsFileStream = new FileStream(fnFileName, System.IO.FileMode.Open,
System.IO.FileAccess.Read);
byte[] bdBinaryData = new byte[fsFileStream.Length];
long brBytesRead = fsFileStream.Read(bdBinaryData, 0, (int)fsFileStream.Length);
fsFileStream.Close();
FileAttachmentType faFileAttach = new FileAttachmentType();
faFileAttach.Content = bdBinaryData;
faFileAttach.Name = fnFileName;
CreateAttachmentType amAttachmentMessage = new CreateAttachmentType();
amAttachmentMessage.Attachments = new AttachmentType[1];
amAttachmentMessage.Attachments[0] = faFileAttach;
amAttachmentMessage.ParentItemId = iiCreateItemid;
CreateAttachmentResponseType caCreateAttachmentResponse = ewsServiceBinding.CreateAttachment
(amAttachmentMessage);
if (caCreateAttachmentResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Error)
{
Console.WriteLine("Error Occured");
Console.WriteLine(caCreateAttachmentResponse.ResponseMessages.Items[0].MessageText);
}
else
{
AttachmentInfoResponseMessageType amAttachmentResponseMessage =
caCreateAttachmentResponse.ResponseMessages.Items[0] as AttachmentInfoResponseMessageType;
Console.WriteLine("Attachment was created");
Console.WriteLine("Change Key : " + amAttachmentResponseMessage.Attachments
[0].AttachmentId.RootItemChangeKey.ToString());
iiAttachmentItemid.Id = amAttachmentResponseMessage.Attachments[0].AttachmentId.RootItemId.ToString
();
iiAttachmentItemid.ChangeKey = amAttachmentResponseMessage.Attachments
[0].AttachmentId.RootItemChangeKey.ToString();
}
return iiAttachmentItemid;
}
}