Ejemplo n.º 1
0
        /// <summary>
        /// Demonstrates three ways to create file attachments and two different ways to create copies of an
        /// item and add it to an email message.
        /// </summary>
        /// <param name="service">An ExchangeService object with credentials and the EWS URL.</param>
        private static void CreateAttachments(ExchangeService service)
        {
            // Create an email message.
            EmailMessage email = new EmailMessage(service);

            email.Subject = "Message with Attachments";
            email.Body    = "This message contains three file attachments and two item attachments.";
            email.ToRecipients.Add("*****@*****.**");

            // This must be set on the item attachments so that they can be consumed in Outlook.
            ExtendedPropertyDefinition PR_MESSAGE_FLAGS_msgflag_read = new ExtendedPropertyDefinition(3591, MapiPropertyType.Integer);

            // Use OpenFileDialog to get the file that will be used to create file attachments.
            string         filename;
            string         fullfilename;
            Stream         myStream       = null;
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter           = "All files (*.*)|*.*";
            openFileDialog.InitialDirectory = "C:";
            openFileDialog.Title            = "Select an attachment";
            openFileDialog.FilterIndex      = 1;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog.OpenFile()) != null)
                    {
                        filename     = openFileDialog.SafeFileName;
                        fullfilename = openFileDialog.FileName;

                        using (myStream)
                        {
                            #region Add file attachments
                            // Add the file attachment to the email message by using the file name and stream.
                            var fileAttach = email.Attachments.AddFileAttachment(filename, myStream);

                            // Add the file attachment to the email message by using the full file name.
                            var fileAttach2 = email.Attachments.AddFileAttachment(fullfilename);

                            // Add the file attachment to the email message by using the full file name and the file name.
                            var fileAttach3 = email.Attachments.AddFileAttachment(filename, fullfilename);

                            // A fourth option is to add a file attachment by using the the file
                            // name and byte array for the file.
                            #endregion

                            #region Add item attachments.
                            // Return a single item.
                            ItemView view = new ItemView(1);

                            // Find the first email message in the Inbox. You will "attach" this email message to the email message you are drafting.
                            // This results in a FindItem operation call to EWS.
                            FindItemsResults <Item> results = service.FindItems(WellKnownFolderName.Inbox, view);

                            // Verify that the item is an email message.
                            if (results.Items[0] is EmailMessage)
                            {
                                EmailMessage msg = results.Items[0] as EmailMessage;
                                System.Collections.ObjectModel.Collection <PropertyDefinitionBase> currentProps = msg.GetLoadedPropertyDefinitions();
                                currentProps.Add(EmailMessageSchema.Body);
                                currentProps.Add(EmailMessageSchema.ToRecipients);
                                currentProps.Add(EmailMessageSchema.CcRecipients);

                                msg.Load(new PropertySet(currentProps));

                                // Attach the email message found from search to the new email message. In reality, you can't attach
                                // existing items, you have to make a copy of the item property by property. The Item type does not
                                // implement ICloneable.
                                ItemAttachment <EmailMessage> itemAttachment = email.Attachments.AddItemAttachment <EmailMessage>();
                                itemAttachment.Name         = "Item attachment based on copied property values";
                                itemAttachment.Item.Subject = msg.Subject;
                                itemAttachment.Item.Body    = msg.Body;
                                foreach (EmailAddress address in msg.ToRecipients)
                                {
                                    itemAttachment.Item.ToRecipients.Add(address);
                                }
                                foreach (EmailAddress address in msg.CcRecipients)
                                {
                                    itemAttachment.Item.ToRecipients.Add(address);
                                }
                                // The Sent flag should be set.
                                itemAttachment.Item.SetExtendedProperty(PR_MESSAGE_FLAGS_msgflag_read, 1);

                                // Unfortunately, not all properties are settable, like DateTimeReceived. If the item is an email message,
                                // you can use the MIME content, which provides a decent copy of the email message without any of the
                                // Exchange store properties. You can add store properties to the item as well, as long
                                // as they are writeable.
                                msg.Load(new PropertySet(EmailMessageSchema.MimeContent));
                                ItemAttachment <EmailMessage> itemAttachment2 = email.Attachments.AddItemAttachment <EmailMessage>();
                                itemAttachment2.Name             = "Item attachment based on copied MIME content";
                                itemAttachment2.Item.MimeContent = msg.MimeContent;
                                // The Sent flag should be set.
                                itemAttachment2.Item.SetExtendedProperty(PR_MESSAGE_FLAGS_msgflag_read, 1);
                            }
                            #endregion

                            /* This results in two calls to EWS. The first call is the CreateItem operation, which creates the
                             * email message in the Drafts folder and adds the attachment. The second call is a SendItem call, which sends
                             * the email message and creates a copy in the default Sent Items folder.*/
                            email.SendAndSaveCopy();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: " + ex.Message);
                }
            }
        }
Ejemplo n.º 2
0
        public static void GetItemInfo(EmailMessage oItem, ref string sInfo)
        {
            string sRet = string.Empty;

            PropertySet oPropSet = new PropertySet(PropertySet.FirstClassProperties);

            oItem.Service.ClientRequestId = Guid.NewGuid().ToString();  // Set a new GUID
            oItem.Load(PropertySet.FirstClassProperties);


            foreach (PropertyDefinitionBase o in oItem.GetLoadedPropertyDefinitions())
            {
                Console.WriteLine(o.ToString(), "  Type.FullName: " + o.Type.FullName + "  Type.GUID: " + o.Type.GUID + "  Type.DeclaringType: " + o.Type.DeclaringType + "\r\n");
            }

            sRet += string.Format("Subject: {0}\r\n", oItem.Subject);
            //if (_ExchangeService.ServerInfo.MajorVersion >= 12)
            //sRet += GetPropString("ConversationId.UniqueId: {0}\r\n", oItem.ConversationId.UniqueId);

            //sRet += string.Format("ConversationId.UniqueId: {0}\r\n", oItem.ConversationId);
            sRet += string.Format("ConversationTopic: {0}\r\n", oItem.ConversationTopic);
            sRet += string.Format("ConversationIndex: {0}\r\n", StringHelper.HexStringFromByteArray(oItem.ConversationIndex, true));


            sRet += "\r\n";
            sRet += string.Format("Id.UniqueId: {0}\r\n", oItem.Id.UniqueId);
            sRet += string.Format("Id.ChangeKey: {0}\r\n", oItem.Id.ChangeKey);


            sRet += "\r\n";


            sRet += string.Format("LastModifiedName: {0}\r\n", oItem.LastModifiedName.ToString());
            sRet += string.Format("LastModifiedTime: {0}\r\n", oItem.LastModifiedTime.ToString());
            sRet += string.Format("ItemClass: {0}\r\n", oItem.ItemClass);
            sRet += "\r\n";
            sRet += string.Format("DisplayTo: {0}\r\n", oItem.DisplayTo);
            sRet += string.Format("DisplayCc: {0}\r\n", oItem.DisplayCc);
            sRet += string.Format("DateTimeCreated: {0}\r\n", oItem.DateTimeCreated.ToString());
            if (oItem.IsSubmitted == true)
            {
                sRet += string.Format("DateTimeSent: {0}\r\n", oItem.DateTimeSent.ToString());
            }
            sRet += string.Format("DateTimeReceived: {0}\r\n", oItem.DateTimeReceived.ToString());
            sRet += string.Format("HasAttachments: {0}\r\n", oItem.HasAttachments.ToString());
            sRet += string.Format("Size: {0}\r\n", oItem.Size.ToString());

            sRet += string.Format("Sensitivity: {0}\r\n", oItem.Sensitivity.ToString());
            sRet += string.Format("Schema: {0}\r\n", oItem.Schema.ToString());

            sRet += "\r\n";
            sRet += string.Format("IsAssociated: {0}\r\n", oItem.IsAssociated.ToString());
            sRet += string.Format("IsDeliveryReceiptRequested: {0}\r\n", oItem.IsDeliveryReceiptRequested.ToString());
            sRet += string.Format("IsDirty: {0}\r\n", oItem.IsDirty.ToString());
            sRet += string.Format("IsDraft: {0}\r\n", oItem.IsDraft.ToString());
            sRet += string.Format("IsFromMe: {0}\r\n", oItem.IsFromMe.ToString());
            sRet += string.Format("IsNew: {0}\r\n", oItem.IsNew.ToString());
            sRet += string.Format("IsRead: {0}\r\n", oItem.IsRead.ToString());
            sRet += string.Format("IsReadReceiptRequested: {0}\r\n", oItem.IsReadReceiptRequested.ToString());

            //sRet += string.Format("IsReminderSet: {0}\r\n", oItem.IsReminderSet.ToString());
            sRet += string.Format("IsResend: {0}\r\n", oItem.IsResend.ToString());
            sRet += string.Format("IsResponseRequested: {0}\r\n", oItem.IsResponseRequested.ToString());
            sRet += string.Format("IsSubmitted: {0}\r\n", oItem.IsSubmitted.ToString());
            sRet += string.Format("IsUnmodified: {0}\r\n", oItem.IsUnmodified.ToString());


            sRet += "\r\n";

            sRet += string.Format("\r\n\r\n----[Body]--------------------------------------------------------------\r\n\r\n{0}\r\n", oItem.Body);


            PropertySet oPropSetForBodyText = new PropertySet(PropertySet.FirstClassProperties);

            oPropSetForBodyText.RequestedBodyType = BodyType.Text;
            oPropSetForBodyText.Add(ItemSchema.Body);
            oItem.Service.ClientRequestId = Guid.NewGuid().ToString();  // Set a new GUID
            Item oItemForBodyText = Item.Bind((ExchangeService)oItem.Service, oItem.Id, (PropertySet)oPropSetForBodyText);

            oItem.Service.ClientRequestId = Guid.NewGuid().ToString();  // Set a new GUID
            oItem.Load(oPropSetForBodyText);

            sRet += string.Format("\r\n\r\n----[Body.Text]--------------------------------------------------------------\r\n\r\n{0}\r\n", oItem.Body.Text);

            sInfo = sRet;
        }