1

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

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

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

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

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

第二个麻烦是如何格式化mailitem的HTMLBody属性,当它不接受普通的HTML而只接受他们所谓的html时。真的需要研究他们的word 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);
        }
    }

任何帮助将非常感激!

4

4 回答 4

2

这是在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>
于 2012-11-08T07:46:21.263 回答
0

在嵌入图像时检查此 msdn 线程

于 2012-07-31T08:13:52.010 回答
0

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

System.Net.Mail.Attachment inline = new System.Net.Mail.Attachment(@"imagepath\filename.png");
inline.ContentDisposition.Inline = true;
于 2012-07-31T09:03:36.637 回答
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);
        }
    }
于 2012-08-01T13:39:00.473 回答