/// <summary> /// Emails with Attachments. Content of Email in Email Details /// </summary> /// <param name="emailDetails"></param> /// <returns></returns> public bool DetailsWithAttachment(ModelEmailDetails emailDetails) { bool isSuccessful = true; try { ExchangeServiceBinding esb = new ExchangeServiceBinding(); esb.Credentials = new NetworkCredential(_loginDetails.UserName, _loginDetails.Password, _loginDetails.Domain); esb.Url = _loginDetails.ExchangeServerAddress; //Text for Just sending Email Only //MessageType emailMessage = new MessageType(); ////Add the sender/recipient to the email message //emailMessage.ToRecipients = new EmailAddressType[1]; //emailMessage.ToRecipients[0] = new EmailAddressType(); //emailMessage.ToRecipients[0].EmailAddress = to; //Currently there is only one recipient //emailMessage.From = new SingleRecipientType(); //set up a single sender //emailMessage.From.Item = new EmailAddressType(); //emailMessage.From.Item.EmailAddress = from; //emailMessage.Subject = subject; //emailMessage.Body = new BodyType(); //emailMessage.Body.BodyType1 = BodyTypeType.HTML; //specify HTML or plain Text //emailMessage.Body.Value = body; //CreateItemType emailToSave = new CreateItemType(); //emailToSave.Items = new NonEmptyArrayOfAllItemsType(); //emailToSave.Items.Items = new ItemType[1]; //emailToSave.Items.Items[0] = emailMessage; //emailToSave.MessageDisposition = MessageDispositionType.SendAndSaveCopy; //emailToSave.MessageDispositionSpecified = true; //CreateItemResponseType response = esb.CreateItem(emailToSave); //// Get the response messages. //ResponseMessageType[] rmta = response.ResponseMessages.Items; //Create an email message and initialize it with the from address, to address, subject and the body of the email. MessageType email = new MessageType(); email.ToRecipients = new EmailAddressType[1]; email.ToRecipients[0] = new EmailAddressType(); email.ToRecipients[0].EmailAddress = emailDetails.SenderEmail; email.From = new SingleRecipientType(); email.From.Item = new EmailAddressType(); email.From.Item.EmailAddress = emailDetails.RecepientEmail; email.Subject = emailDetails.SubjectOfEmail; email.Body = emailDetails.BodyType; //email.Body = new BodyType(); //email.Body.BodyType1 = BodyTypeType.Text; email.Body.BodyType1 = emailDetails.BodyType.BodyType1; email.Body.Value = emailDetails.BodyOfEmail; //Save the created email to the drafts folder so that we can attach a file to it. CreateItemType emailToSave = new CreateItemType(); emailToSave.Items = new NonEmptyArrayOfAllItemsType(); emailToSave.Items.Items = new ItemType[1]; emailToSave.Items.Items[0] = email; emailToSave.MessageDisposition = MessageDispositionType.SaveOnly; emailToSave.MessageDispositionSpecified = true; CreateItemResponseType response = esb.CreateItem(emailToSave); ResponseMessageType[] rmta = response.ResponseMessages.Items; ItemInfoResponseMessageType emailResponseMessage = (ItemInfoResponseMessageType)rmta[0]; //Create the file attachment. FileAttachmentType fileAttachment = new FileAttachmentType(); fileAttachment.Content = System.IO.File.ReadAllBytes(emailDetails.AttachmentLocation); ; fileAttachment.Name = Path.GetFileName(emailDetails.AttachmentLocation); //fileAttachment.ContentType = "application/ms-excel"; fileAttachment.ContentType = emailDetails.ContentType; CreateAttachmentType attachmentRequest = new CreateAttachmentType(); attachmentRequest.Attachments = new AttachmentType[1]; attachmentRequest.Attachments[0] = fileAttachment; attachmentRequest.ParentItemId = emailResponseMessage.Items.Items[0].ItemId; //Attach the file to the message. CreateAttachmentResponseType attachmentResponse = (CreateAttachmentResponseType)esb.CreateAttachment(attachmentRequest); AttachmentInfoResponseMessageType attachmentResponseMessage = (AttachmentInfoResponseMessageType)attachmentResponse.ResponseMessages.Items[0]; //Create a new item id type using the change key and item id of the email message so that we know what email to send. ItemIdType attachmentItemId = new ItemIdType(); attachmentItemId.ChangeKey = attachmentResponseMessage.Attachments[0].AttachmentId.RootItemChangeKey; attachmentItemId.Id = attachmentResponseMessage.Attachments[0].AttachmentId.RootItemId; string test = attachmentResponseMessage.Attachments[0].Name; //Send the email. SendItemType si = new SendItemType(); si.ItemIds = new BaseItemIdType[1]; si.SavedItemFolderId = new TargetFolderIdType(); si.ItemIds[0] = attachmentItemId; DistinguishedFolderIdType siSentItemsFolder = new DistinguishedFolderIdType(); siSentItemsFolder.Id = DistinguishedFolderIdNameType.sentitems; si.SavedItemFolderId.Item = siSentItemsFolder; si.SaveItemToFolder = true; SendItemResponseType siSendItemResponse = esb.SendItem(si); //Log Email Response if Tracing is on CreateItemResponseType responseToEmail = esb.CreateItem(emailToSave); this._responseMessage = responseToEmail.ResponseMessages.Items[0].ResponseCode.ToString(); } catch (Exception err) { isSuccessful = false; string errorMessage = string.Format("Error in sending Emails to Server {0} - {1}", err.Message, err.StackTrace); _responseMessage = errorMessage; Logger.LogWriter.Instance.WriteToLog(errorMessage); Debug.WriteLine(errorMessage); return isSuccessful; } return isSuccessful; }
/// <summary> /// Helper method for setting the underlying fields represented by the CompleteName property /// </summary> /// <param name="binding">ExchangeServiceBinding to use for the call</param> /// <param name="contactId">Id and change key of the contact to update</param> /// <param name="completeName">The complete name to set on the contact</param> /// <returns>ItemInfoResponse message due to UpdateItem call</returns> /// public static ItemInfoResponseMessageType SetCompleteName( ExchangeServiceBinding binding, ItemIdType contactId, CompleteNameType completeName) { // Create our request. We will do a single UpdateItem call with a bunch of change descriptions. // UpdateItemType updateRequest = new UpdateItemType(); // We are updating a single item // ItemChangeType itemChange = new ItemChangeType(); itemChange.Item = contactId; updateRequest.ItemChanges = new ItemChangeType[] { itemChange }; // We will only set those props that are not null in the complete name. So right now, we // don't know how many that will be, so let's create a list to hold the change descriptions. // List<ItemChangeDescriptionType> changeList = new List<ItemChangeDescriptionType>(); // Now, for each possible property, let's check to make sure it is not null, then we will set the // value on a ContactItem instance needed for our change description and add it to our change list. // // Title if (completeName.Title != null) { ContactItemType titleContact = new ContactItemType(); ExtendedPropertyType titleProp = new ExtendedPropertyType( TitlePath, completeName.Title); titleContact.ExtendedProperty = new ExtendedPropertyType[] { titleProp }; changeList.Add(new SetItemFieldType(TitlePath, titleContact)); } // GivenName if (completeName.FirstName != null) { ContactItemType givenNameContact = new ContactItemType(); givenNameContact.GivenName = completeName.FirstName; changeList.Add(new SetItemFieldType(GivenNamePath, givenNameContact)); } // MiddleName if (completeName.MiddleName != null) { ContactItemType middleNameContact = new ContactItemType(); middleNameContact.MiddleName = completeName.MiddleName; changeList.Add(new SetItemFieldType(MiddleNamePath, middleNameContact)); } // Surname if (completeName.LastName != null) { ContactItemType surnameContact = new ContactItemType(); surnameContact.Surname = completeName.LastName; changeList.Add(new SetItemFieldType(SurnamePath, surnameContact)); } // Generation if (completeName.Suffix != null) { ContactItemType generationContact = new ContactItemType(); generationContact.Generation = completeName.Suffix; changeList.Add(new SetItemFieldType(GenerationPath, generationContact)); } // Initials if (completeName.Initials != null) { ContactItemType initialsContact = new ContactItemType(); initialsContact.Initials = completeName.Initials; changeList.Add(new SetItemFieldType(InitialsPath, initialsContact)); } // DisplayName if (completeName.FullName != null) { ContactItemType displayNameContact = new ContactItemType(); displayNameContact.DisplayName = completeName.FullName; changeList.Add(new SetItemFieldType(DisplayNamePath, displayNameContact)); } // Nickname if (completeName.Nickname != null) { ContactItemType nicknameContact = new ContactItemType(); nicknameContact.Nickname = completeName.Nickname; changeList.Add(new SetItemFieldType(NicknamePath, nicknameContact)); } // YomiFirstName if (completeName.YomiFirstName != null) { ContactItemType yomiFirstContact = new ContactItemType(); ExtendedPropertyType yomiFirstProp = new ExtendedPropertyType( YomiFirstNamePath, completeName.YomiFirstName); yomiFirstContact.ExtendedProperty = new ExtendedPropertyType[] { yomiFirstProp }; changeList.Add(new SetItemFieldType(YomiFirstNamePath, yomiFirstContact)); } // YomiLastName if (completeName.YomiLastName != null) { ContactItemType yomiLastContact = new ContactItemType(); ExtendedPropertyType yomiLastProp = new ExtendedPropertyType( YomiLastNamePath, completeName.YomiLastName); yomiLastContact.ExtendedProperty = new ExtendedPropertyType[] { yomiLastProp }; changeList.Add(new SetItemFieldType(YomiLastNamePath, yomiLastContact)); } // If they passed in a CompleteName with all NULL props, we should fail. // if (changeList.Count == 0) { throw new ArgumentException("No parts of CompleteName were set", "completeName"); } itemChange.Updates = changeList.ToArray(); updateRequest.ConflictResolution = ConflictResolutionType.AlwaysOverwrite; // Make the call and return the response message // return binding.UpdateItem(updateRequest).ResponseMessages.Items[0] as ItemInfoResponseMessageType; }