/// <summary> /// Export image to a new email /// </summary> /// <param name="outlookApplication"></param> /// <param name="format"></param> /// <param name="tmpFile"></param> /// <param name="subject"></param> /// <param name="attachmentName"></param> /// <param name="to"></param> /// <param name="cc"></param> /// <param name="bcc"></param> /// <param name="url"></param> private static void ExportToNewEmail(IOutlookApplication outlookApplication, EmailFormat format, string tmpFile, string subject, string attachmentName, string to, string cc, string bcc, string url) { using (IItem newItem = outlookApplication.CreateItem(OlItemType.olMailItem)) { if (newItem == null) { return; } //MailItem newMail = COMWrapper.Cast<MailItem>(newItem); MailItem newMail = (MailItem)newItem; newMail.Subject = subject; if (!string.IsNullOrEmpty(to)) { newMail.To = to; } if (!string.IsNullOrEmpty(cc)) { newMail.CC = cc; } if (!string.IsNullOrEmpty(bcc)) { newMail.BCC = bcc; } newMail.BodyFormat = OlBodyFormat.olFormatHTML; string bodyString = null; // Read the default signature, if nothing found use empty email try { bodyString = GetOutlookSignature(format); } catch (Exception e) { Log.Error("Problem reading signature!", e); } switch (format) { case EmailFormat.Text: // Create the attachment (and dispose the COM object after using) using (newMail.Attachments.Add(tmpFile, OlAttachmentType.olByValue, 1, attachmentName)) { newMail.BodyFormat = OlBodyFormat.olFormatPlain; if (bodyString == null) { bodyString = ""; } newMail.Body = bodyString; } break; default: string contentId = Path.GetFileName(tmpFile); // Create the attachment (and dispose the COM object after using) using (IAttachment attachment = newMail.Attachments.Add(tmpFile, OlAttachmentType.olByValue, 0, attachmentName)) { // add content ID to the attachment if (_outlookVersion.Major >= (int)OfficeVersion.OFFICE_2007) { try { contentId = Guid.NewGuid().ToString(); IPropertyAccessor propertyAccessor = attachment.PropertyAccessor; propertyAccessor.SetProperty(PropTag.ATTACHMENT_CONTENT_ID, contentId); } catch { Log.Info("Error working with the PropertyAccessor, using filename as contentid"); contentId = Path.GetFileName(tmpFile); } } } newMail.BodyFormat = OlBodyFormat.olFormatHTML; string href = ""; string hrefEnd = ""; if (!string.IsNullOrEmpty(url)) { href = $"<A HREF=\"{url}\">"; hrefEnd = "</A>"; } string htmlImgEmbedded = $"<BR/>{href}<IMG border=0 hspace=0 alt=\"{attachmentName}\" align=baseline src=\"cid:{contentId}\">{hrefEnd}<BR/>"; string fallbackBody = $"<HTML><BODY>{htmlImgEmbedded}</BODY></HTML>"; if (bodyString == null) { bodyString = fallbackBody; } else { int bodyIndex = bodyString.IndexOf("<body", StringComparison.CurrentCultureIgnoreCase); if (bodyIndex >= 0) { bodyIndex = bodyString.IndexOf(">", bodyIndex, StringComparison.Ordinal) + 1; bodyString = bodyIndex >= 0 ? bodyString.Insert(bodyIndex, htmlImgEmbedded) : fallbackBody; } else { bodyString = fallbackBody; } } newMail.HTMLBody = bodyString; break; } // So not save, otherwise the email is always stored in Draft folder.. (newMail.Save();) newMail.Display(false); using (IInspector inspector = newMail.GetInspector()) { if (inspector != null) { try { inspector.Activate(); } catch { // Ignore } } } } }
/// <summary> /// Export image to a new email /// </summary> /// <param name="outlookApplication"></param> /// <param name="tmpFile"></param> /// <param name="captureDetails"></param> private static void ExportToNewEmail(IOutlookApplication outlookApplication, EmailFormat format, string tmpFile, string subject, string attachmentName, string to, string CC, string BCC, string url) { Item newItem = outlookApplication.CreateItem(OlItemType.olMailItem); if (newItem == null) { return; } //MailItem newMail = COMWrapper.Cast<MailItem>(newItem); MailItem newMail = (MailItem)newItem; newMail.Subject = subject; if (!string.IsNullOrEmpty(to)) { newMail.To = to; } if (!string.IsNullOrEmpty(CC)) { newMail.CC = CC; } if (!string.IsNullOrEmpty(BCC)) { newMail.BCC = BCC; } newMail.BodyFormat = OlBodyFormat.olFormatHTML; string bodyString = null; // If enabled, read the default signature (if nothing found use empty email) if (officeConfiguration.OutlookIncludeDefaultSignature) { try { bodyString = GetOutlookSignature(format); } catch (Exception e) { LOG.Error("Problem reading signature!", e); } } switch (format) { case EmailFormat.Text: newMail.Attachments.Add(tmpFile, OlAttachmentType.olByValue, 1, attachmentName); newMail.BodyFormat = OlBodyFormat.olFormatPlain; if (bodyString == null) { bodyString = ""; } newMail.Body = bodyString; break; case EmailFormat.HTML: default: string contentID = Path.GetFileName(tmpFile); // Create the attachment using (Attachment attachment = newMail.Attachments.Add(tmpFile, OlAttachmentType.olByValue, 0, attachmentName)) { // add content ID to the attachment if (outlookVersion.Major >= OUTLOOK_2007) { try { contentID = Guid.NewGuid().ToString(); PropertyAccessor propertyAccessor = attachment.PropertyAccessor; propertyAccessor.SetProperty(PropTag.ATTACHMENT_CONTENT_ID, contentID); } catch { LOG.Info("Error working with the PropertyAccessor, using filename as contentid"); contentID = Path.GetFileName(tmpFile); } } } newMail.BodyFormat = OlBodyFormat.olFormatHTML; string href = ""; string hrefEnd = ""; if (!string.IsNullOrEmpty(url)) { href = string.Format("<A HREF=\"{0}\">", url); hrefEnd = "</A>"; } string htmlImgEmbedded = string.Format("<BR/>{0}<IMG border=0 hspace=0 alt=\"{1}\" align=baseline src=\"cid:{2}\">{3}<BR/>", href, attachmentName, contentID, hrefEnd); string fallbackBody = string.Format("<HTML><BODY>{0}</BODY></HTML>", htmlImgEmbedded); if (bodyString == null) { bodyString = fallbackBody; } else { int bodyIndex = bodyString.IndexOf("<body", StringComparison.CurrentCultureIgnoreCase); if (bodyIndex >= 0) { bodyIndex = bodyString.IndexOf(">", bodyIndex) + 1; if (bodyIndex >= 0) { bodyString = bodyString.Insert(bodyIndex, htmlImgEmbedded); } else { bodyString = fallbackBody; } } else { bodyString = fallbackBody; } } newMail.HTMLBody = bodyString; break; } // So not save, otherwise the email is always stored in Draft folder.. (newMail.Save();) newMail.Display(false); newMail.GetInspector().Activate(); if (newItem != null) { newItem.Dispose(); } }
/// <summary> /// Export image to a new email /// </summary> /// <param name="outlookApplication"></param> /// <param name="tmpFile"></param> /// <param name="captureDetails"></param> private static void ExportToNewEmail(IOutlookApplication outlookApplication, EmailFormat format, string tmpFile, string subject, string attachmentName) { Item newItem = outlookApplication.CreateItem(OlItemType.olMailItem); if (newItem == null) { return; } //MailItem newMail = COMWrapper.Cast<MailItem>(newItem); MailItem newMail = (MailItem)newItem; newMail.Subject = subject; newMail.BodyFormat = OlBodyFormat.olFormatHTML; string bodyString = null; // Read the default signature, if nothing found use empty email try { bodyString = GetOutlookSignature(format); } catch (Exception e) { LOG.Error("Problem reading signature!", e); } switch (format) { case EmailFormat.Text: newMail.Attachments.Add(tmpFile, OlAttachmentType.olByValue, 1, attachmentName); newMail.BodyFormat = OlBodyFormat.olFormatPlain; if (bodyString == null) { bodyString = ""; } newMail.Body = bodyString; break; case EmailFormat.HTML: default: // Create the attachment Attachment attachment = newMail.Attachments.Add(tmpFile, OlAttachmentType.olByValue, 0, attachmentName); // add content ID to the attachment string contentID = Path.GetFileName(tmpFile); if (outlookVersion.Major >= 12) { // Add the content id to the attachment try { contentID = Guid.NewGuid().ToString(); PropertyAccessor propertyAccessor = attachment.PropertyAccessor; propertyAccessor.SetProperty(PropTag.ATTACHMENT_CONTENT_ID, contentID); } catch { LOG.Info("Error working with the PropertyAccessor, using filename as contentid"); contentID = Path.GetFileName(tmpFile); } } newMail.BodyFormat = OlBodyFormat.olFormatHTML; string htmlImgEmbedded = "<BR/><IMG border=0 hspace=0 alt=\"" + attachmentName + "\" align=baseline src=\"cid:" + contentID + "\"><BR/>"; string fallbackBody = "<HTML><BODY>" + htmlImgEmbedded + "</BODY></HTML>"; if (bodyString == null) { bodyString = fallbackBody; } else { int bodyIndex = bodyString.IndexOf("<body", StringComparison.CurrentCultureIgnoreCase); if (bodyIndex >= 0) { bodyIndex = bodyString.IndexOf(">", bodyIndex) + 1; if (bodyIndex >= 0) { bodyString = bodyString.Insert(bodyIndex, htmlImgEmbedded); } else { bodyString = fallbackBody; } } else { bodyString = fallbackBody; } } newMail.HTMLBody = bodyString; break; } // So not save, otherwise the email is always stored in Draft folder.. (newMail.Save();) try { newMail.Display(false); newMail.GetInspector().Activate(); } catch (Exception ex) { LOG.WarnFormat("Problem displaying the new email, retrying to display it. Problem: {0}", ex.Message); Thread retryDisplayEmail = new Thread(delegate() { int retries = 60; int retryInXSeconds = 5; while (retries-- > 0) { Thread.Sleep(retryInXSeconds * 1000); try { newMail.Display(false); newMail.GetInspector().Activate(); LOG.InfoFormat("Managed to display the message."); return; } catch (Exception) { LOG.WarnFormat("Retrying to show email in {0} seconds... Retries left: {1}", retryInXSeconds, retries); } } LOG.WarnFormat("Retry failed, saving message to draft."); newMail.Save(); }); retryDisplayEmail.Name = "Retry to display email"; retryDisplayEmail.IsBackground = true; retryDisplayEmail.Start(); } }