locked
EWS (Exchange Web service) sending email with attachment RRS feed

  • Question

  • User1536947745 posted

    Hello;

    Below code sends email. But there is no attachment after it sends. Also there is a email in my  draft folder in outlook with attachment.

    Can somebody help me to resolve this please? I think I couldn't entegrate email body with attachment!!!

    Thank you

     

    // Identify the service binding and the user.

    Live.ExchangeWebServices.

    esb.Credentials =

    esb.Url =

     

    ExchangeServiceBinding esb = new Live.ExchangeWebServices.ExchangeServiceBinding();new NetworkCredential(user, pass, "domn");@"https://mail.domain.com/EWS/Exchange.asmx";// esb.Url = @"https://192.168.1.61/EWS/Exchange.asmx";

     

    // esb.Url = @"https://mail.domain.local/EWS/Exchange.asmx";

     

     

    // Specify the request version.

    esb.RequestServerVersionValue =

    esb.RequestServerVersionValue.Version =

     

    new RequestServerVersion();ExchangeVersionType.Exchange2007_SP1;// Create the CreateItem request.

     

     

    CreateItemType createEmailRequest = new CreateItemType();// Specifiy how the e-mail will be handled.

    createEmailRequest.MessageDisposition =

    createEmailRequest.MessageDispositionSpecified =

     

    MessageDispositionType.SendAndSaveCopy;true;// Specify the location of sent items.

    createEmailRequest.SavedItemFolderId =

     

    sentitems.Id =

    createEmailRequest.SavedItemFolderId.Item = sentitems;

     

    new TargetFolderIdType();DistinguishedFolderIdType sentitems = new DistinguishedFolderIdType();DistinguishedFolderIdNameType.sentitems;// Create the array of items.

    createEmailRequest.Items =

     

    new NonEmptyArrayOfAllItemsType();// Create a single e-mail message.

     

    message.Subject = subject;

    message.Body =

    message.Body.BodyType1 =

    message.Body.Value = body;

     

    message.Sender =

    message.Sender.Item =

    message.Sender.Item.EmailAddress = emailFrom;

     

    MessageType message = new MessageType();new Live.ExchangeWebServices.BodyType();BodyTypeType.Text;new SingleRecipientType();new EmailAddressType();// message.Sender.Item.Name = "Benim";

    message.ReplyTo =

    message.ReplyTo[0] =

    message.ReplyTo[0].EmailAddress = replyFrom;

     

     

     

    new EmailAddressType[1];new EmailAddressType();//message.From = new SingleRecipientType();

     

    //message.From.Item = new EmailAddressType();

     

    //message.From.Item.EmailAddress = "aa";

     

    //message.From.Item.EmailAddress = "benim";

    message.ToRecipients =

    message.ToRecipients[0] =

    message.ToRecipients[0].EmailAddress = emailTo;

    message.Sensitivity =

     

    {

    message.CcRecipients =

    message.CcRecipients[0] =

    message.CcRecipients[0].EmailAddress = emailCC;

    }

     

    {

    message.BccRecipients =

    message.BccRecipients[0] =

    message.BccRecipients[0].EmailAddress = emailBCC;

    }

     

    new EmailAddressType[1];new EmailAddressType();SensitivityChoicesType.Normal;if (emailCC != "")new EmailAddressType[1];new EmailAddressType();if (emailBCC != "")new EmailAddressType[1];new EmailAddressType();///////////////////////////////////////////////

     

    //Save the created email to the drafts folder so that we can attach a file to it.

     

    emailToSave.Items =

    emailToSave.Items.Items =

    emailToSave.Items.Items[0] = message;

    emailToSave.MessageDisposition =

    emailToSave.MessageDispositionSpecified =

     

     

     

     

     

     

     

    CreateItemType emailToSave = new CreateItemType();new NonEmptyArrayOfAllItemsType();new ItemType[1];MessageDispositionType.SendAndSaveCopy;true;CreateItemResponseType response = esb.CreateItem(emailToSave);ResponseMessageType[] rmta = response.ResponseMessages.Items;ItemInfoResponseMessageType emailResponseMessage = (ItemInfoResponseMessageType)rmta[0];//Create the file attachment.

     

    fileAttachment.Content = attachmentAsBytes;

    fileAttachment.Name = attachmentName;

    fileAttachment.ContentType =

     

    attachmentRequest.Attachments =

    attachmentRequest.Attachments[0] = fileAttachment;

    attachmentRequest.ParentItemId = emailResponseMessage.Items.Items[0].ItemId;

     

    FileAttachmentType fileAttachment = new FileAttachmentType();"application/ms-excel";CreateAttachmentType attachmentRequest = new CreateAttachmentType();new AttachmentType[1];//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.

     

    attachmentItemId.ChangeKey = attachmentResponseMessage.Attachments[0].AttachmentId.RootItemChangeKey;

    attachmentItemId.Id = attachmentResponseMessage.Attachments[0].AttachmentId.RootItemId;

     

    ItemIdType attachmentItemId = new ItemIdType();///////////////////////////////////////////////

     

    ////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);

     

    // Add the message to the array of items to be created.

    createEmailRequest.Items.Items =

    createEmailRequest.Items.Items[0] = message;

     

     

    new ItemType[1];//try

     

    //{

     

    // Send a CreateItem request and get the CreateItem

     

    // response.

     

     

    Live.ExchangeWebServices.

     

    CreateItemResponseType createItemResponse = esb.CreateItem(createEmailRequest);ArrayOfResponseMessagesType responses = createItemResponse.ResponseMessages;ResponseMessageType[] responseMessages = responses.Items;MessageBox.Show("Done!");

    Tuesday, November 2, 2010 11:43 PM

All replies