//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); } } }
private bool SendEmail(string newLink) { try { EmailEntity emailEntity = new EmailEntity(); EmailOperation emailOperation = new EmailOperation(); emailEntity.SmtpServer = ConfigurationSettings.AppSettings["SMTPServer"].ToString(); emailEntity.From = ConfigurationSettings.AppSettings["FromMailID"].ToString(); emailEntity.ToEmail = txtContAdminEmailID.Text.ToString(); emailEntity.Password = ConfigurationSettings.AppSettings["Password"].ToString(); emailEntity.Subject = "Your account details for - live.lawallies.com"; emailEntity.Body = "Dear user, <br/> Please use the link below to login to the websitel live.lawllies.com . <br/> " + newLink.ToString() + " <br/> User Id=" + txtUserName.Text.ToString().Trim() + " <br/> Password="******"\n", " "); lblError.Text = "Error : " + strError + "<br/> Stack Trace : " + ex.StackTrace.ToString(); lblError.Visible = true; return(false); } }
//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); } } }); } ; }; }; }
//Listing 15-4. Sending E-mail by Inserting Records into the EmailOperation Table partial void EmailOperations_Inserting(EmailOperation entity) { LightSwitchApplication.UserCode.SmtpMailHelper.SendMail( entity.SenderEmail, entity.RecipientEmail, entity.Subject, entity.Body, entity.Attachment, entity.AttachmentFileName); }
public ActionResult ForgetPass(FormCollection form) { using (HapinessSurveyEntities db = new HapinessSurveyEntities()) { try { string ml = Request.Form["EmailID"].ToString(); var pas = (from m in db.UserTbls where m.user_mail == ml select m.user_Pass).FirstOrDefault(); EmailOperation.SendEmail(ml, "Forget Password", "Happiness Password is = " + pas); ViewBag.messge = "mail sended"; } catch (Exception ex) { return(View("ForgetPass")); } return(View("Login")); } }
ExchangeResult ExecuteOperation(EmailOperation operation, string address, Dictionary <string, bool> processed = null) { // create processed addresses dictionary if it does not exist if (processed == null) { processed = new Dictionary <string, bool>(); } // dont reprocess this address if we have already processed it if (processed.ContainsKey(address)) { return(null); } // try to find a mailbox for the address on one of the tenants ExchangeService service = null; EmailAddress mailbox = null; foreach (WebCredentials cred in credentials) { service = new ExchangeService(); service.Credentials = cred; service.Url = new Uri(ConfigurationManager.AppSettings["url"]); try { NameResolutionCollection results = service.ResolveName("smtp:" + address); if (results.Count > 0) { mailbox = results[0].Mailbox; break; } } catch (Exception e) { return(new ExchangeResult(address, StatusCode.Error, "Failed to resolve name: " + e.Message)); } } // if we did not find a mailbox for the address on any of the tenants then report recipient not found if (mailbox == null) { return(new ExchangeResult(address, StatusCode.RecipientNotFound, "recipient not found")); } // add resolved address to processed list to prevent reprocessing processed.Add(mailbox.Address, true); // if this mailbox is a group/distribution list if (mailbox.MailboxType == MailboxType.PublicGroup) { // attempt to expand the group ExpandGroupResults group = null; try { group = service.ExpandGroup(mailbox.Address); } catch (Exception e) { // report failure to expand group if an exception occurs during expansion return(new ExchangeResult(mailbox.Address, StatusCode.Error, "Failed to expand group: " + e.Message)); } // for every member in the group ExchangeResult result = new ExchangeResult(); foreach (EmailAddress member in group.Members) { // recursively execute operation and log results result.Log(ExecuteOperation(operation, member.Address, processed)); } // return the results return(result); } // if this is just a regular mailbox else if (mailbox.MailboxType == MailboxType.Mailbox) { // set impersonation to the mailbox address service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, mailbox.Address); // attempt to get some info to see if impersonation worked try { DateTime?dt = service.GetPasswordExpirationDate(mailbox.Address); } catch (Exception e) { // if we were unable to impersonate the user then report error return(new ExchangeResult(mailbox.Address, StatusCode.Error, "impersonation failed: " + e.Message)); } // delete email if operation is delete if (operation == EmailOperation.Delete) { try { // find all instances of the email with message_id in the mailbox FolderView folderView = new FolderView(int.MaxValue); folderView.PropertySet = new PropertySet(BasePropertySet.IdOnly, FolderSchema.DisplayName); folderView.Traversal = FolderTraversal.Shallow; SearchFilter folderFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "AllItems"); FindFoldersResults folders = service.FindFolders(WellKnownFolderName.Root, folderFilter, folderView); SearchFilter filter = new SearchFilter.IsEqualTo(EmailMessageSchema.InternetMessageId, message_id); ItemView view = new ItemView(int.MaxValue); view.PropertySet = new PropertySet(BasePropertySet.IdOnly); view.Traversal = ItemTraversal.Shallow; List <ItemId> items = new List <ItemId>(); foreach (Item item in service.FindItems(folders.Folders[0].Id, filter, view)) { items.Add(item.Id); } // if no instances of the email were found in the mailbox then report message_id not found if (items.Count == 0) { return(new ExchangeResult(mailbox.Address, StatusCode.MessageNotFound, "message_id not found")); } // delete all found instances of the email with message_id from the mailbox foreach (ServiceResponse response in service.DeleteItems(items, DeleteMode.SoftDelete, null, null)) { // if we failed to delete an instance of the email then report an error if (response.Result != ServiceResult.Success) { string message = "failed to delete email: " + response.ErrorCode + " " + response.ErrorMessage; return(new ExchangeResult(mailbox.Address, StatusCode.Error, message)); } } } catch (Exception e) { //report any errors we encounter return(new ExchangeResult(mailbox.Address, StatusCode.Error, "failed to delete email: " + e.Message)); } } // recover email if operation is recover else if (operation == EmailOperation.Restore) { try { // find all instances of the email with message_id in the recoverable items folder SearchFilter filter = new SearchFilter.IsEqualTo(EmailMessageSchema.InternetMessageId, message_id); ItemView view = new ItemView(int.MaxValue); view.PropertySet = new PropertySet(BasePropertySet.IdOnly); view.Traversal = ItemTraversal.Shallow; List <ItemId> items = new List <ItemId>(); foreach (Item item in service.FindItems(WellKnownFolderName.RecoverableItemsDeletions, filter, view)) { items.Add(item.Id); } // if no instances of the email with message_id were found in the recoverable items folder of the mailbox if (items.Count == 0) { // report message_id not found return(new ExchangeResult(mailbox.Address, StatusCode.MessageNotFound, "message_id not found")); } // move every instance of the email with message_id in the recoverable items folder to the inbox foreach (ServiceResponse response in service.MoveItems(items, new FolderId(WellKnownFolderName.Inbox))) { // if we failed to move an instance of the email to the inbox then report an error if (response.Result != ServiceResult.Success) { string message = "failed to recover email: " + response.ErrorCode + " " + response.ErrorMessage; return(new ExchangeResult(mailbox.Address, StatusCode.Error, message)); } } } catch (Exception e) { // report any errors we encounter return(new ExchangeResult(mailbox.Address, StatusCode.Error, "failed to recover email: " + e.Message)); } } // report successful operation return(new ExchangeResult(mailbox.Address, StatusCode.Success, "success")); } // report that the mailbox type is not one of the supported types return(new ExchangeResult(mailbox.Address, StatusCode.Error, "Unsupported mailbox type: " + mailbox.MailboxType.ToString())); }