使用Outlook Interop在电子邮件中嵌入图像

时间:2011-12-12 20:41:42

标签: c# image email

我有一个简单的小电子邮件应用程序,允许用户选择生成字符串并发送电子邮件的某些选项。我想知道是否有可能在电子邮件中添加图像,即标题徽标或签名等。我一直在研究的研究非常重,我知道HTML很少。有人可以帮忙吗?我的代码如下......

using System;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Configuration;

namespace My_EmailSender
{
    public class EmailSender:Notification
    {
        string emailRecipient = ConfigurationManager.AppSettings["emailRecipient"];

        public void SendMail(string message)
        {
            try
            {
                var oApp = new Outlook.Application();
                var oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
                var oRecip = (Outlook.Recipient)oMsg.Recipients.Add(emailRecipient);
                oRecip.Resolve();
                oMsg.Subject = "Email Notification";
                oMsg.Body = message;

                // Display the message before sending could save() also but no need
                oMsg.Send();
                oMsg.Display(true);
                oRecip = null;
                oMsg = null;
                oApp = null;
            }
            catch (Exception e)
            {
                Console.WriteLine("Problem with email execution. Exception caught: ", e);
            }

            return;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

以下是在c#

中通过outlook发送图像的示例代码
        Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Request.ApplicationPath);
        System.Net.Configuration.MailSettingsSectionGroup settings = (System.Net.Configuration.MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
        System.Net.Configuration.SmtpSection smtp = settings.Smtp;
        System.Net.Configuration.SmtpNetworkElement network = smtp.Network;
        Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application();
        MailItem mailItem = (MailItem)outlookApp.CreateItem(OlItemType.olMailItem);
        mailItem.To = network.TargetName;


        Attachment attachment = mailItem.Attachments.Add(
        "C://test.bmp"
        , OlAttachmentType.olEmbeddeditem
        , null
        , "test image"
        );
        string imageCid = "test.bmp@123";

        attachment.PropertyAccessor.SetProperty(
            "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
            , imageCid
            );
        mailItem.BodyFormat = OlBodyFormat.olFormatRichText;
        mailItem.HTMLBody = String.Format(
              "<body><img src=\"cid:{0}\"></body>"
             , imageCid
             );

        mailItem.Importance = OlImportance.olImportanceNormal;
        mailItem.Display(false);

                      

答案 1 :(得分:0)

我会一直使用System.Net.Mail发送电子邮件,但这可能是您的要求吗?

Read up on system.net.mail here.