不使用文字编辑器发送带有嵌入图像的电子邮件



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

我想发送一封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);
        }
    }

任何帮助都将不胜感激!

这是在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>

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

您是否尝试过使用此处显示的SmtpClient?

System.Net.Mail.Attachment inline = new System.Net.Mail.Attachment(@"imagepathfilename.png");
inline.ContentDisposition.Inline = true;
  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);
        }
    }

最新更新