C # code for connecting to the echange server in the domain environment

Source: Internet
Author: User

String userName = "your userName ";
String passWord = "your passWord ";
String domain = "domain Name ";
String url = "https: // exchange server address/EWS/Exchange. asmx ";
ExchangeServiceBinding exchangeServer = new ExchangeServiceBinding ();
ICredentials creds = new NetworkCredential (userName, passWord, domain );
// Establish a trusted connection
ExchangeServer. Credentials = creds;
ExchangeServer. Url = url;

// Define the inbox of the email
DistinguishedFolderIdType [] folderIDArray = new DistinguishedFolderIdType [1];
FolderIDArray [0] = new DistinguishedFolderIdType ();
FolderIDArray [0]. Id = DistinguishedFolderIdNameType. inbox;

PathToUnindexedFieldType ptuftDisplayName = new PathToUnindexedFieldType ();
PtuftDisplayName. FieldURI = UnindexedFieldURIType. folderDisplayName;

PathToExtendedFieldType pteftComment = new PathToExtendedFieldType ();
PteftComment. PropertyTag = "0x3004"; // PR_COMMENT
PteftComment. PropertyType = mapipropertypetype. String;

// Define the GetFolderType object and set the corresponding attributes
GetFolderType myfoldertype = new GetFolderType ();
Myfoldertype. FolderIds = folderIDArray;
Myfoldertype. FolderShape = new FolderResponseShapeType ();
Myfoldertype. FolderShape. BaseShape = DefaultShapeNamesType. IdOnly;
Myfoldertype. FolderShape. AdditionalProperties = new BasePathToElementType [2];
Myfoldertype. FolderShape. AdditionalProperties [0] = ptuftDisplayName;
Myfoldertype. FolderShape. AdditionalProperties [1] = pteftComment;

// Obtain the folder set on the server
GetFolderResponseType myFolder = exchangeServer. GetFolder (myfoldertype );

// Get the inbox
FolderInfoResponseMessageType firmtInbox = (FolderInfoResponseMessageType) myFolder. ResponseMessages. Items [0];

// Display the inbox
// Console. WriteLine (string. Format ("got folder: {0}", firmtInbox. Folders [0]. DisplayName ));


PathToUnindexedFieldType ptuftSubject = new PathToUnindexedFieldType ();
PtuftSubject. FieldURI = UnindexedFieldURIType. itemSubject;

PathToUnindexedFieldType ptuftBody = new PathToUnindexedFieldType ();
PtuftBody. FieldURI = UnindexedFieldURIType. itemAttachments;

PathToExtendedFieldType pteftFlagStatus = new PathToExtendedFieldType ();
PteftFlagStatus. PropertyTag = "0x1090"; // PR_FLAG_STATUS
PteftFlagStatus. PropertyType = MapiPropertyTypeType. Integer;

// Define the FindItemType object to obtain the set in the inbox
FindItemType findItemRequest = new FindItemType ();
FindItemRequest. Traversal = ItemQueryTraversalType. Shallow;
FindItemRequest. ItemShape = new ItemResponseShapeType ();
FindItemRequest. ItemShape. BaseShape = DefaultShapeNamesType. AllProperties;


FindItemRequest. ParentFolderIds = new FolderIdType [1];
FindItemRequest. ParentFolderIds [0] = firmtInbox. Folders [0]. FolderId;
// Obtain the email
FindItemResponseType firt = exchangeServer. FindItem (findItemRequest );
// Cyclically iterate each email
Foreach (FindItemResponseMessageType firmtMessage in firt. ResponseMessages. Items)
{
// If the email is included, it is displayed
If (firmtMessage. RootFolder. TotalItemsInView> 0)
{
// Cyclically iterate the details of each email
Foreach (ItemType it in (ArrayOfRealItemsType) firmtMessage. RootFolder. Item). Items)
{
If (MessageType) (it). IsRead = false)
{
& Nbs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.