使用嵌入式图像发送电子邮件而不使用wordeditor

时间:2012-07-31 07:37:35

标签: c# .net image sendmail outlook-2007

我发送带有嵌入式图片的电子邮件时遇到了麻烦。我尝试使用文字编辑器的一些解决方案,但他们无法解决我的情况。我不能使用SmtpClient因为客户端不想要它。他有交换,需要在已发送的文件夹中发送电子邮件。

我想以html格式发送电子邮件,例如。标题中的图像 - 页脚中的徽标和图像 - 符号。

我将HTML模板作为字符串存储在数据库中,以提供更多电子邮件的外观和目的。我使用在发送时替换的变量插入的特定数据。

有没有人知道如何在不使用mailitem.wordeditor的情况下将存储在数据库中的图像添加到电子邮件中,而无需显示检查器?我们假设图像已经存在于光盘上,或者可能是以某种方式使用的流?

我的应用程序需要在后台发送而不通过其他窗口通知用户。使用wordeditor添加图像需要显示检查器。即使我立即关闭它,它也会闪烁。

第二个问题是如何格式化mailitem的HTMLBody属性,当它不接受普通HTML但只接受他们所谓的html时。是否真的需要研究他们的单词html?

首先,我使用了MailMessage,这个模板甚至可以用于图像和替代视图。也许存在使用MailMessage通过outlook发送它的一些可能性,但我不知道。

anoyne是否与之相关?

    public void SendEmailViaOutlook()
    {
        //in template I mostly need to use table and css to divide email into blocks - header, content, footer

        String htmlTemplate = "<html>\n";
        htmlTemplate += "  <head>\n";
        htmlTemplate += "    <style type=\"text//css\">\n";
        htmlTemplate += "       #Header  {border-width: 1; border: solid; text-align: center}\n";
        htmlTemplate += "       #Content {border-width: 1; border: solid; text-align: center}\n";
        htmlTemplate += "       #Footer   {border-width: 1; border: solid; text-align: center}\n";
        htmlTemplate += "    </style>\n";
        htmlTemplate += "  </head>\n";
        htmlTemplate += "  <body>\n";
        htmlTemplate += "    <table>\n";
        htmlTemplate += "      <tr><td><img src=\"cid:companylogo\"/></td></tr>\n";
        htmlTemplate += "      <tr><td><div id=\"Header\">$HEADER$</div></td></tr>\n";
        htmlTemplate += "      <tr><td><div id=\"Contentr\">$CONTENT$</div></td></tr>\n";
        htmlTemplate += "      <tr><td><div id=\"Footer\">$FOOTER$</div></td></tr>\n";
        htmlTemplate += "      <tr><td><img src=\"cid:usersign\"/></td></tr>\n";
        htmlTemplate += "    </table>\n";
        htmlTemplate += "  </body>n";
        htmlTemplate += "</html>\n";

        //the code is simplified to demostrate problem
        //$CONTENT etc. will be replaced by another html from custom html wysiwyg editor

        try
        {
            Outlook.Application outlook = new Outlook.Application();
            Outlook._MailItem outLookMailMessage = outlook.CreateItem(Outlook.OlItemType.olMailItem) as Outlook._MailItem;
            outLookMailMessage.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;

            /*

            here a I have problem to set the property - my template is not
            set and a blank email is sent - almost none html it takes except the example from msdn http://support.microsoft.com/kb/310262, are there some rules how to write it?

            */

            outLookMailMessage.HTMLBody = htmlTemplate;
            outLookMailMessage.Subject = this.Subject;

            outLookMailMessage.Recipients.Add("somenone@somewhere.com");

            /*
             here I woud need somehow link 2 images with cid companylogo and usersign
             */

            outLookMailMessage.Send();
            outLookMailMessage = null;
        }
        catch (System.Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

非常感谢任何帮助!

4 个答案:

答案 0 :(得分:2)

以下是在Outlook中发送图片的代码

Configuration config = System.Web.Configuration.WebConfigurationManager
    .OpenWebConfiguration(HttpContext.Request.ApplicationPath);
var settings = (System.Net.Configuration.MailSettingsSectionGroup)
    config.GetSectionGroup("system.net/mailSettings");
var smtp = settings.Smtp;
System.Net.Configuration.SmtpNetworkElement network = smtp.Network;
var outlookApp = new Microsoft.Office.Interop.Outlook.Application();
var 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)

答案 2 :(得分:0)

您是否尝试使用SmtpClient所示的here

System.Net.Mail.Attachment inline = new System.Net.Mail.Attachment(@"imagepath\filename.png");
inline.ContentDisposition.Inline = true;

答案 3 :(得分:0)

  public void SendEmailViaOutlook()
    {
        String htmlTemplate = "<html>\n";
        htmlTemplate += "  <head>\n";
        htmlTemplate += "    <style type=\"text//css\">\n";
        htmlTemplate += "       #Header  {border-width: 1; border: solid; text-align: center}\n";
        htmlTemplate += "       #Content {border-width: 1; border: solid; text-align: center}\n";
        htmlTemplate += "       #Footer   {border-width: 1; border: solid; text-align: center}\n";
        htmlTemplate += "    </style>\n";
        htmlTemplate += "  </head>\n";
        htmlTemplate += "  <body>\n";
        htmlTemplate += "    <table>\n";
        htmlTemplate += "      <tr><td><img src=\"cid:companylogo.jpg@embed\"/></td></tr>\n";
        htmlTemplate += "      <tr><td><div id=\"Header\">$HEADER$</div></td></tr>\n";
        htmlTemplate += "      <tr><td><div id=\"Contentr\">$CONTENT$</div></td></tr>\n";
        htmlTemplate += "      <tr><td><div id=\"Footer\">$FOOTER$</div></td></tr>\n";
        htmlTemplate += "      <tr><td><img src=\"cid:usersign.jpg@embed\"/></td></tr>\n";
        htmlTemplate += "    </table>\n";
        htmlTemplate += "  </body>n";
        htmlTemplate += "</html>\n";

        //the code is simplified to demostrate problem
        //$CONTENT etc. will be replaced by another html from custom html wysiwyg editor

        try
        {
            Outlook.Application outlook = new Outlook.Application();
            Outlook._MailItem outLookMailMessage = outlook.CreateItem(Outlook.OlItemType.olMailItem) as Outlook._MailItem;
            outLookMailMessage.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;

            outLookMailMessage.HTMLBody = htmlTemplate;
            outLookMailMessage.Subject = this.Subject;

            outLookMailMessage.Recipients.Add("somenone@somewhere.com");


            path = ""; //set some path to folder with images

            Outlook.Attachment attachment1 = outLookMailMessage.Attachments.Add(path, Outlook.OlAttachmentType.olEmbeddeditem, null, "");                                 
            attachment1.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", "companylogo.jpg@EMBED");
            Outlook.Attachment attachment2 = outLookMailMessage.Attachments.Add(path, Outlook.OlAttachmentType.olEmbeddeditem, null, "");                                  
            attachment2.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", "usersign.jpg@EMBED");
            outLookMailMessage.Send();
            outLookMailMessage = null;
        }
        catch (System.Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }