//Listing 15-5. Button Code for Sending E-mail
        partial void SendEmail_Execute()
        {
            using (var tempWorkspace = new DataWorkspace())
            {
                EmailOperation newEmail =
                    tempWorkspace.ApplicationData.EmailOperations.AddNew();

                newEmail.RecipientEmail = User.Email;
                newEmail.SenderEmail    = "*****@*****.**";
                newEmail.Subject        = SubjectProperty;
                newEmail.Body           = BodyProperty;

                try
                {
                    tempWorkspace.ApplicationData.SaveChanges();

                    //If you want, you can write some code here to create a record in an audit table
                    newEmail.Delete();
                    tempWorkspace.ApplicationData.SaveChanges();
                    this.ShowMessageBox("Your email has been sent");
                }
                catch (Exception ex)
                {
                    this.ShowMessageBox(ex.Message);
                }
            }
        }
        //Listing 15-7. Screen Code to Send E-mail Attachments
        partial void UserDetail_InitializeDataWorkspace(List <IDataService> saveChangesTo)
        {
            var control = this.FindControl("FileUploadButton");

            control.ControlAvailable +=
                (object sender, ControlAvailableEventArgs e) =>
            {
                var fileButton = (Button)e.Control;
                fileButton.Content = "Send Message With Attachment";

                fileButton.Click +=
                    (object sender2, RoutedEventArgs e2) =>
                {
                    OpenFileDialog dlg = new OpenFileDialog();
                    if (dlg.ShowDialog().GetValueOrDefault(false) == true)
                    {
                        byte[] data;
                        using (FileStream stream = dlg.File.OpenRead())
                        {
                            data = new byte[stream.Length];
                            stream.Read(data, 0, data.Length);
                        }

                        string filename = dlg.File.Name;
                        //send the email here
                        this.Details.Dispatcher.BeginInvoke(() =>
                        {
                            using (var dw = new DataWorkspace())
                            {
                                EmailOperation newEmail =
                                    dw.ApplicationData.EmailOperations.AddNew();
                                newEmail.RecipientEmail     = User.Email;
                                newEmail.SenderEmail        = "*****@*****.**";
                                newEmail.Subject            = SubjectProperty;
                                newEmail.Body               = BodyProperty;
                                newEmail.Attachment         = data;
                                newEmail.AttachmentFileName = filename;

                                try
                                {
                                    dw.ApplicationData.SaveChanges();
                                    //If you want, you can write some code here to
                                    //create a record in an audit table
                                    newEmail.Delete();
                                    dw.ApplicationData.SaveChanges();
                                }
                                catch (Exception ex)
                                {
                                    this.ShowMessageBox(ex.Message);
                                }
                            }
                        });
                    }
                    ;
                };
            };
        }