/// <summary> /// Archive this email item to CRM. /// </summary> /// <param name="olItem">The email item to archive.</param> /// <param name="reason">The reason it is being archived.</param> /// <returns>A result object indicating success or failure.</returns> public static ArchiveResult Archive(this Outlook.MailItem olItem, EmailArchiveReason reason, string excludedEmails = "") { ArchiveResult result; Outlook.UserProperty olProperty = olItem.UserProperties[CrmIdPropertyName]; if (olProperty == null) { result = olItem.AsArchiveable(reason).Save(excludedEmails); if (result.IsSuccess) { olItem.Categories = string.IsNullOrEmpty(olItem.Categories) ? SuiteCRMCategoryName : $"{olItem.Categories},{SuiteCRMCategoryName}"; olItem.EnsureProperty(CrmIdPropertyName, result.EmailId); } } else { result = ArchiveResult.Success(olProperty.Value, new[] { new AlreadyArchivedException(olItem) }); } return(result); }
/// <summary> /// Construct and despatch a CRM representation of this mail item, without its attachments, to CRM /// </summary> /// <param name="mailItem">The mail item to despatch.</param> /// <param name="type">?unknown.</param> /// <returns>An archive result comprising the CRM id of the email, if stored, /// and a list of exceptions encountered in the process.</returns> private ArchiveResult ConstructAndDespatchCrmItem(Outlook.MailItem mailItem, string type) { ArchiveResult result; eNameValue[] crmItem = ConstructCrmItem(mailItem, type); try { result = ArchiveResult.Success(clsSuiteCRMHelper.SetEntry(crmItem, "Emails"), null); } catch (System.Exception firstFailure) { Log.Warn("EmailArchiving.SaveEmailToCrm: first attempt to upload email failed", firstFailure); try { /* try again without the HTML body. I have no idea why this might make a difference. */ crmItem[5] = clsSuiteCRMHelper.SetNameValuePair("description_html", string.Empty); result = ArchiveResult.Success(clsSuiteCRMHelper.SetEntry(crmItem, "Emails"), new[] { firstFailure }); } catch (System.Exception secondFailure) { Log.Warn("EmailArchiving.SaveEmailToCrm: second attempt to upload email (without HTML body) failed", firstFailure); result = ArchiveResult.Failure(new[] { firstFailure, secondFailure }); } } return(result); }
public ArchiveResult ArchiveEmailWithEntityRelationships(Outlook.MailItem mailItem, IEnumerable <CrmEntity> selectedCrmEntities, string type) { var result = this.SaveEmailToCrm(mailItem, type); if (result.IsFailure) { return(result); } var warnings = CreateEmailRelationshipsWithEntities(result.EmailId, selectedCrmEntities); return(ArchiveResult.Success( result.EmailId, result.Problems.Concat(warnings))); }
public ArchiveResult ArchiveEmailWithEntityRelationships(Outlook.MailItem olItem, IEnumerable <CrmEntity> selectedCrmEntities, EmailArchiveReason reason) { var result = olItem.Archive(reason); if (result.IsSuccess) { var warnings = CreateEmailRelationshipsWithEntities(result.EmailId, selectedCrmEntities); result = ArchiveResult.Success( result.EmailId, result.Problems == null ? warnings : result.Problems.Concat(warnings)); } return(result); }
/// <summary> /// Construct and despatch CRM representations of the attachments of this email item to CRM. /// </summary> /// <param name="mailItem">The mail item whose attachments should be sent.</param> /// <param name="result">The result of transmitting the item itself to CRM.</param> /// <returns>A (possibly modified) archive result.</returns> private ArchiveResult ConstructAndDespatchAttachments(Outlook.MailItem mailItem, ArchiveResult result) { var warnings = new List <System.Exception>(); if (settings.ArchiveAttachments) { foreach (Outlook.Attachment attachment in mailItem.Attachments) { warnings.Add(ConstructAndDespatchCrmAttachment(mailItem, result.EmailId, attachment)); } } if (warnings.Where(w => w != null).Count() > 0) { if (result.Problems != null) { warnings.AddRange(result.Problems); } result = ArchiveResult.Success(result.EmailId, warnings.Where(w => w != null)); } return(result); }
public ArchiveResult SaveEmailToCrm(Outlook.MailItem mailItem, string type) { try { SaveMailItemIfNecessary(mailItem, type); eNameValue[] data = new eNameValue[12]; data[0] = clsSuiteCRMHelper.SetNameValuePair("name", mailItem.Subject ?? ""); data[1] = clsSuiteCRMHelper.SetNameValuePair("date_sent", DateTimeOfMailItem(mailItem, type).ToString("yyyy-MM-dd HH:mm:ss")); data[2] = clsSuiteCRMHelper.SetNameValuePair("message_id", mailItem.EntryID); data[3] = clsSuiteCRMHelper.SetNameValuePair("status", "archived"); data[4] = clsSuiteCRMHelper.SetNameValuePair("description", mailItem.Body ?? ""); data[5] = clsSuiteCRMHelper.SetNameValuePair("description_html", mailItem.HTMLBody); data[6] = clsSuiteCRMHelper.SetNameValuePair("from_addr", clsGlobals.GetSenderAddress(mailItem, type)); data[7] = clsSuiteCRMHelper.SetNameValuePair("to_addrs", mailItem.To); data[8] = clsSuiteCRMHelper.SetNameValuePair("cc_addrs", mailItem.CC); data[9] = clsSuiteCRMHelper.SetNameValuePair("bcc_addrs", mailItem.BCC); data[10] = clsSuiteCRMHelper.SetNameValuePair("reply_to_addr", mailItem.ReplyRecipientNames); data[11] = clsSuiteCRMHelper.SetNameValuePair("assigned_user_id", clsSuiteCRMHelper.GetUserId()); string crmEmailId; try { crmEmailId = clsSuiteCRMHelper.SetEntry(data, "Emails"); } catch (System.Exception firstFailure) { Log.Warn("1st attempt to upload email failed", firstFailure); data[5] = clsSuiteCRMHelper.SetNameValuePair("description_html", ""); try { crmEmailId = clsSuiteCRMHelper.SetEntry(data, "Emails"); } catch (System.Exception secondFailure) { Log.Warn("2nd attempt to upload email failed", secondFailure); return(ArchiveResult.Failure(new[] { firstFailure, secondFailure })); } } mailItem.Categories = "SuiteCRM"; mailItem.Save(); var warnings = new List <System.Exception>(); if (settings.ArchiveAttachments) { foreach (Outlook.Attachment attachment in mailItem.Attachments) { try { clsSuiteCRMHelper.UploadAttachment( new clsEmailAttachments { DisplayName = attachment.DisplayName, FileContentInBase64String = GetAttachmentBytes(attachment, mailItem) }, crmEmailId); } catch (System.Exception problem) { Log.Warn("Failed to upload email attachment", problem); warnings.Add(problem); } } } return(ArchiveResult.Success(crmEmailId, warnings)); } catch (System.Exception failure) { Log.Warn("Could not upload email to CRM", failure); return(ArchiveResult.Failure(failure)); } }