/// <summary> /// Process the new lead emails. /// </summary> /// <returns>True if it worked.</returns> private bool ProcessIncomingLeadEmails() { Log("Gettting email leads..."); string msg = string.Empty; MailMan mailman = ApplicationManager.GetLeadMailman(); bool result = false; int nonLeadEmails = 0; int duplicateEmails = 0; int successfulWrites = 0; int failedWrites = 0; int bouncedEmails = 0; if (mailman != null) { Log("Getting all emails..."); EmailBundle leadEmails = ApplicationManager.GetLeadEmails(mailman, out msg); if (leadEmails != null) { Log("Found {0} emails.", leadEmails.MessageCount); int i; Email email = null; for (i = 0; i <= leadEmails.MessageCount - 1; i++) { email = leadEmails.GetEmail(i); Lead lead = ApplicationManager.GetLeadFromFundraisingIdeasEmail(email); bool deleteThisEmail = false; if (lead != null) { Log("Retrieved lead: {0}", lead.ToString()); Lead existingLead = DataManager.GetLead(lead.EmailAddress); bool res = false; if (existingLead == null) { // res = DataManager.WriteLead(lead, DataManager.ObjectWriteMode.Insert); res = DataManager.WriteCustomer(lead, DataManager.ObjectWriteMode.Insert); Log("Write lead to db: {0}", res.ToString()); if (res) { successfulWrites++; } else { failedWrites++; throw new ApplicationException("Cannot write lead to db - cannot continue"); } } else { Log("Lead exists in db, skipping..."); duplicateEmails++; } if (existingLead != null || res) { // Its either a duplicate, or it was inserted ok, either way let's delete it. deleteThisEmail = true; } } else if (email.From.Contains("Mail Delivery Subsystem")) { // // We have a bounced email // string bounceEmail = Functions.GetValueFromText(email.Body, "<(.*@.*)>"); string bounceReason = Functions.GetValueFromText(email.Body, "<.*@.*>...(.*)"); if (bounceReason == string.Empty) { bounceReason = Functions.GetValueFromText(email.Body, "\\((reason.*55.*)\\)"); } if (bounceEmail != string.Empty) { Lead existingLead = DataManager.GetLead(bounceEmail); // email.GetAttachedMessage(0).GetToName(0) if (existingLead == null) { // // No email address, let's try by name and status // if (email.NumAttachedMessages > 0 && email.GetAttachedMessage(0).NumTo > 0) { PersonName name = new PersonName(); if (name.ParseFullName(email.GetAttachedMessage(0).GetToName(0))) { existingLead = DataManager.GetLeadByNameAndStatus(name.FirstName, name.LastName, Lead.LeadStatusCode.lscEmailSent); } } } if (existingLead != null) { bool res = DataManager.WriteLeadStatusLog(existingLead, Lead.LeadStatusCode.lscEmailBounced1, bounceReason.Substring(0, Math.Min(150, bounceReason.Length))); Log("Bounced email from {0} for reason \"{1}\", update status log = {2}", bounceEmail, bounceReason, res); if (res) { deleteThisEmail = true; } } else { Log("Bounced email from {0}, but can't find email addr in db.", bounceEmail); } } else { Log("We have a bounced email, but can't get the email address from the body."); } bouncedEmails++; } else if (email.Subject.Contains("Out of Office") || email.Subject.Contains("AutoReply")) { Log("Found autoreply from: {0}, deleting", email.From); deleteThisEmail = true; } else if (email.Subject == "echo") { Log("Found echo request from: {0}", email.FromAddress); Lead testLead = new Lead(); testLead.ParseFullName(email.FromName); testLead.LeadInfoText = email.GetPlainTextBody(); testLead.EmailAddress = email.FromAddress; ApplicationManager.SetFundraisingIdeasProperties(testLead); if (testLead.TemplateCurrent == null) { testLead.TemplateCurrent = DataManager.GetTemplate("Hockey"); // Hockey template } ProcessNewDatabaseLeads(testLead); deleteThisEmail = true; } else if (email.Subject == "unsubscribe") { Log("Found unsubscribe request from: {0}", email.FromAddress); Lead existingLead = DataManager.GetLead(email.FromAddress); if (existingLead != null) { bool res = DataManager.WriteLeadStatusLog(existingLead, Lead.LeadStatusCode.lscUnsubscribeRequestReceived); Log("Bye bye {0}; update status log = {1}", existingLead.FirstName, res); if (res) { deleteThisEmail = true; } } else { Log("Odd, can't find email address in db. Doing nothing."); } } else { Log("Skipping non-lead email from: {0}", email.FromAddress); nonLeadEmails++; } if (deleteThisEmail) { // Delete this email. bool success = mailman.DeleteEmail(email); if (success != true) { Log("Strangeness, can't delete POP3 email: {0}", mailman.LastErrorText); } } } Log("Ending email session..."); mailman.Pop3EndSession(); Log("Summary - total emails found: {0}, non-lead: {1}, duplicate: {2}, newly inserted: {3}", leadEmails.MessageCount, nonLeadEmails, duplicateEmails, successfulWrites); result = true; } else { // // Happens if tcp/ip issue. Thanks Time Warner! // Log("Can't get POP3 emails. Msg was: {0}", msg); } } else { Log("Can't get mailman component - is it registered and licensed?"); } return(result); }