【问题标题】:Send email with embedded image without using wordeditor在不使用 wordeditor 的情况下发送带有嵌入图像的电子邮件
【发布时间】:2012-07-28 22:31:43
【问题描述】:

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

我想以 html 格式发送一封电子邮件,例如。页眉中的图像 - 徽标和页脚中的图像 - 标志。

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

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

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

第二个麻烦是如何格式化mailitem的HTMLBody属性,当它不接受普通的HTML而只接受他们所谓的html时。真的有必要研究他们的词html吗?

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

有没有遇到过它?

    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);
        }
    }

任何帮助将不胜感激!

【问题讨论】:

  • 我想你只是将它添加为附件,与html正文中指定的cid名称相同。
  • 我试过了,但它只显示空白图像
  • 文件名为“companylogo”和“usersign”的文件是否像html正文中的那样?附件是否也被称为相同的名称?您能否将您的问题更新为您尝试过的代码?
  • 不,它们不是,它们存储在数据库中,公司徽标是附件的别名,不必这样命名
  • 我将 cid 属性重命名为 companylogo.jpg@embed 一组附件到:attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", name + ".jpg@EMBED");,现在它可以工作了

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


【解决方案1】:

这里是oulook中发送图片的代码

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);

<mailSettings>
      <smtp from="test@gmail.com">
        <network host="hostname" port="portrnumber" 
            userName="domain/username" password="password" 
            targetName="targetname@gmail.com"/>
      </smtp>
</mailSettings>

【讨论】:

    【解决方案2】:
      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);
            }
        }
    

    【讨论】:

      【解决方案3】:

      您是否尝试过使用SmtpClient,如here 所示?

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

      【讨论】:

      • 是的,但我不能使用 SmtpClient,因为客户不允许 smtp 中继
      • 是的,但是服务器上运行的 Outlook 会使用他自己的 Smtp Client 来发送电子邮件,所以你会遇到同样的问题
      • 在这种情况下没有,因为客户的coupmuter的Outlooks使用Exchange,所以我的任务只是提交到Outlook,但感谢您的帮助
      【解决方案4】:

      【讨论】:

      • 谢谢,让我更好地了解问题
      猜你喜欢
      • 2012-04-07
      • 2021-10-31
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 2018-03-13
      • 2015-07-29
      • 2011-02-05
      • 2014-01-25
      相关资源
      最近更新 更多