コード例 #1
0
        /// <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)
        {
            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 (IAttachment attachment = 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 (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    = 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);

                using (IInspector inspector = newMail.GetInspector()) {
                    if (inspector != null)
                    {
                        try {
                            inspector.Activate();
                        } catch {
                            // Ignore
                        }
                    }
                }
            }
        }