/// <summary> /// Helper method to get the Outlook signature /// </summary> /// <returns></returns> private static string GetOutlookSignature(EmailFormats emailFormat) { using (var profilesKey = Registry.CurrentUser.OpenSubKey(ProfilesKey, false)) { if (profilesKey == null) { return(null); } var defaultProfile = (string)profilesKey.GetValue(DefaultProfileValue); Log.Debug().WriteLine("defaultProfile={0}", defaultProfile); using (var profileKey = profilesKey.OpenSubKey(defaultProfile + @"\" + AccountKey, false)) { if (profileKey == null) { return(null); } var numbers = profileKey.GetSubKeyNames(); foreach (var number in numbers) { Log.Debug().WriteLine("Found subkey {0}", number); using (var numberKey = profileKey.OpenSubKey(number, false)) { var val = (byte[])numberKey?.GetValue(NewSignatureValue); if (val == null) { continue; } var signatureName = ""; foreach (var b in val) { if (b != 0) { signatureName += (char)b; } } Log.Debug().WriteLine("Found email signature: {0}", signatureName); string extension; switch (emailFormat) { case EmailFormats.Text: extension = ".txt"; break; default: extension = ".htm"; break; } var signatureFile = Path.Combine(SignaturePath, signatureName + extension); if (File.Exists(signatureFile)) { Log.Debug().WriteLine("Found email signature file: {0}", signatureFile); return(File.ReadAllText(signatureFile, Encoding.Default)); } } } } } return(null); }
/// <summary> /// Helper method to create an outlook mail item with attachment /// </summary> /// <param name="emailFormat"></param> /// <param name="tmpFile">The file to send, do not delete the file right away!</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> /// <returns>true if it worked, false if not</returns> public static bool ExportToOutlook(EmailFormats emailFormat, string tmpFile, string subject, string attachmentName, string to, string cc, string bcc, string url) { var exported = false; try { using (var outlookApplication = GetOrCreateOutlookApplication()) { if (outlookApplication != null) { ExportToNewEmail(outlookApplication, emailFormat, tmpFile, subject, attachmentName, to, cc, bcc, url); exported = true; } } return(exported); } catch (Exception e) { Log.Error().WriteLine(e, "Error while creating an outlook mail item: "); } return(exported); }
/// <summary> /// Export image to a new email /// </summary> /// <param name="outlookApplication"></param> /// <param name="emailFormat"></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, EmailFormats emailFormat, string tmpFile, string subject, string attachmentName, string to, string cc, string bcc, string url) { using (var newItem = outlookApplication.CreateItem(OlItemType.olMailItem)) { if (newItem == null) { return; } var newMail = (IMailItem)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(emailFormat); } catch (Exception e) { Log.Error().WriteLine(e, "Problem reading signature!"); } switch (emailFormat) { case EmailFormats.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: var contentId = Path.GetFileName(tmpFile); // Create the attachment (and dispose the COM object after using) using (var attachment = newMail.Attachments.Add(tmpFile, OlAttachmentType.olByValue, 0, attachmentName)) { // add content ID to the attachment if (_outlookVersion.Major >= (int)OfficeVersions.Office2007) { try { contentId = Guid.NewGuid().ToString(); var propertyAccessor = attachment.PropertyAccessor; propertyAccessor.SetProperty(PropTag.ATTACHMENT_CONTENT_ID, contentId); } catch { Log.Info().WriteLine("Error working with the PropertyAccessor, using filename as contentid"); contentId = Path.GetFileName(tmpFile); } } } newMail.BodyFormat = OlBodyFormat.olFormatHTML; var href = ""; var 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 { var 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 (var inspector = newMail.GetInspector()) { if (inspector == null) { return; } try { inspector.Activate(); } catch { // Ignore } } } }