/// <summary> /// Handles the Update event of the grdMailboxes control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Web.UI.WebControls.DataGridCommandEventArgs"/> instance containing the event data.</param> protected void dtgMailboxes_Update(object sender, DataGridCommandEventArgs e) { var emailAddress = (TextBox)e.Item.FindControl("txtEmailAddress"); var pickUser = (PickSingleUser)e.Item.FindControl("IssueAssignedUser"); var pickType = (PickType)e.Item.FindControl("IssueType"); var id = Convert.ToInt32(grdMailboxes.DataKeys[e.Item.ItemIndex]); if (emailAddress.Text.Trim() == "") { throw new ArgumentNullException("Email Address empty"); } var mailbox = ProjectMailboxManager.GetById(id); if (mailbox != null) { mailbox.Mailbox = emailAddress.Text; mailbox.IssueTypeId = pickType.SelectedValue; mailbox.AssignToUserName = pickUser.SelectedValue; ProjectMailboxManager.SaveOrUpdate(mailbox); } grdMailboxes.EditItemIndex = -1; BindMailboxes(); }
/// <summary> /// Handles the DeleteCommand event of the grdMailboxes control. /// </summary> /// <param name="source">The source of the event.</param> /// <param name="e">The <see cref="T:System.Web.UI.WebControls.DataGridCommandEventArgs"/> instance containing the event data.</param> protected void dtgMailboxes_Delete(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { var id = (int)grdMailboxes.DataKeys[e.Item.ItemIndex]; if (!ProjectMailboxManager.Delete(id)) { ActionMessage.ShowErrorMessage(LoggingManager.GetErrorMessageResource("DeleteMailboxError")); } else { BindMailboxes(); } }
/// <summary> /// Handles the Click event of the btnAdd control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param> protected void btnAdd_Click(object sender, EventArgs e) { var mailbox = new ProjectMailbox { AssignToDisplayName = string.Empty, AssignToId = Guid.Empty, AssignToUserName = IssueAssignedUser.SelectedValue, IssueTypeId = IssueAssignedType.SelectedValue, Mailbox = txtMailbox.Text, ProjectId = ProjectId }; if (ProjectMailboxManager.SaveOrUpdate(mailbox)) { BindMailboxes(); } }
/// <summary> /// Binds the mailboxes. /// </summary> private void BindMailboxes() { grdMailboxes.Columns[1].HeaderText = GetLocalResourceObject("EmailAddressLabel.Text").ToString(); grdMailboxes.Columns[2].HeaderText = GetLocalResourceObject("IssueAssignedUserLabel.Text").ToString(); grdMailboxes.Columns[3].HeaderText = GetLocalResourceObject("IssueTypeLabel.Text").ToString(); grdMailboxes.DataSource = ProjectMailboxManager.GetByProjectId(ProjectId); grdMailboxes.DataKeyField = "Id"; grdMailboxes.DataBind(); grdMailboxes.Visible = grdMailboxes.Items.Count != 0; //IssueAssignedUser.SelectedValue = -1; //IssueAssignedType.SelectedValue = 0; //if (Security.GetUserRole().Equals(Globals.ReadOnlyRole)) // AddMailbox.Visible = false; }
/// <summary> /// Reads the mail. /// </summary> public MailboxReaderResult ReadMail() { var result = new MailboxReaderResult(); IList <Project> projects = new List <Project>(); LogInfo("MailboxReader: Begin read mail."); try { using (var pop3Client = new POP3_Client()) { // configure the logger pop3Client.Logger = new Logger(); pop3Client.Logger.WriteLog += LogPop3Client; // connect to the server pop3Client.Connect(Config.Server, Config.Port, Config.UseSsl); // authenticate pop3Client.Login(Config.Username, Config.Password); // process the messages on the server foreach (POP3_ClientMessage message in pop3Client.Messages) { var mailHeader = Mail_Message.ParseFromByte(message.HeaderToByte()); if (mailHeader != null) { var messageFrom = string.Empty; if (mailHeader.From.Count > 0) { messageFrom = string.Join("; ", mailHeader.From.ToList().Select(p => p.Address).ToArray()).Trim(); } var recipients = mailHeader.To.Mailboxes.Select(mailbox => mailbox.Address).ToList(); if (mailHeader.Cc != null) { recipients.AddRange(mailHeader.Cc.Mailboxes.Select(mailbox => mailbox.Address)); } if (mailHeader.Bcc != null) { recipients.AddRange(mailHeader.Bcc.Mailboxes.Select(mailbox => mailbox.Address)); } // loop through the mailboxes foreach (var address in recipients) { var pmbox = ProjectMailboxManager.GetByMailbox(address); // cannot find the mailbox skip the rest if (pmbox == null) { LogWarning(string.Format("MailboxReader: could not find project mailbox: {0} skipping.", address)); continue; } var project = projects.FirstOrDefault(p => p.Id == pmbox.ProjectId); if (project == null) { project = ProjectManager.GetById(pmbox.ProjectId); // project is disabled skip if (project.Disabled) { LogWarning(string.Format("MailboxReader: Project {0} - {1} is flagged as disabled skipping.", project.Id, project.Code)); continue; } projects.Add(project); } var entry = new MailboxEntry { Title = mailHeader.Subject.Trim(), From = messageFrom, ProjectMailbox = pmbox, Date = mailHeader.Date, Project = project, Content = "Email Body could not be parsed." }; var mailbody = Mail_Message.ParseFromByte(message.MessageToByte()); if (string.IsNullOrEmpty(mailbody.BodyHtmlText)) // no html must be text { entry.Content = mailbody.BodyText.Replace("\n\r", "<br/>").Replace("\r\n", "<br/>").Replace("\r", ""); } else { //TODO: Enhancements could include regular expressions / string matching or not matching // for particular strings values in the subject or body. // strip the <body> out of the message (using code from below) var bodyExtractor = new Regex("<body.*?>(?<content>.*)</body>", RegexOptions.IgnoreCase | RegexOptions.Singleline); var match = bodyExtractor.Match(mailbody.BodyHtmlText); var emailContent = match.Success && match.Groups["content"] != null ? match.Groups["content"].Value : mailbody.BodyHtmlText; entry.Content = emailContent.Replace("<", "<").Replace(">", ">"); entry.IsHtml = true; } if (Config.ProcessAttachments && project.AllowAttachments) { foreach (var attachment in mailbody.GetAttachments(Config.ProcessInlineAttachedPictures).Where(p => p.ContentType != null)) { entry.MailAttachments.Add(attachment); } } //save this message SaveMailboxEntry(entry); // add the entry if the save did not throw any exceptions result.MailboxEntries.Add(entry); LogInfo(string.Format( "MailboxReader: Message #{0} processing finished, found [{1}] attachments, total saved [{2}].", message.SequenceNumber, entry.MailAttachments.Count, entry.AttachmentsSavedCount)); // delete the message?. if (!Config.DeleteAllMessages) { continue; } try { message.MarkForDeletion(); } catch (Exception) { } } } else { LogWarning(string.Format("pop3Client: Message #{0} header could not be parsed.", message.SequenceNumber)); } } } } catch (Exception ex) { LogException(ex); result.LastException = ex; result.Status = ResultStatuses.FailedWithException; } LogInfo("MailboxReader: End read mail."); return(result); }
private bool ProcessNewIssue(List <string> recipients, POP3_ClientMessage message, Mail_Message mailHeader, IList <Project> projects, MailboxReaderResult result) { var messageFrom = string.Empty; if (mailHeader.From.Count > 0) { messageFrom = string.Join("; ", mailHeader.From.ToList().Select(p => p.Address).ToArray()).Trim(); } bool processed = false; // loop through the mailboxes foreach (var address in recipients) { var pmbox = ProjectMailboxManager.GetByMailbox(address); // cannot find the mailbox skip the rest if (pmbox == null) { LogWarning(string.Format("MailboxReader: could not find project mailbox: {0} skipping.", address)); continue; } var project = projects.FirstOrDefault(p => p.Id == pmbox.ProjectId); if (project == null) { project = ProjectManager.GetById(pmbox.ProjectId); // project is disabled skip if (project.Disabled) { LogWarning(string.Format("MailboxReader: Project {0} - {1} is flagged as disabled skipping.", project.Id, project.Code)); continue; } projects.Add(project); } var entry = new MailboxEntry { Title = mailHeader.Subject.Trim(), From = messageFrom, ProjectMailbox = pmbox, Date = mailHeader.Date, Project = project, Content = "Email Body could not be parsed." }; var mailbody = Mail_Message.ParseFromByte(message.MessageToByte()); bool isHtml; List <MIME_Entity> attachments = null; string content = GetMessageContent(mailbody, project, out isHtml, ref attachments); entry.Content = content; entry.IsHtml = isHtml; foreach (var attachment in attachments) { entry.MailAttachments.Add(attachment); } //save this message Issue issue = SaveMailboxEntry(entry); //send notifications for the new issue SendNotifications(issue); // add the entry if the save did not throw any exceptions result.MailboxEntries.Add(entry); processed = true; } return(processed); }