How to embed images in email

后端 未结 6 1529
感情败类
感情败类 2020-11-22 11:57

I need to embed an image in e-mail. How do I do it?

I do not want to use third party tool, nor am I interested in language specific answer (but it is PHP, in case yo

6条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 12:30

    Correct way of embedding images into Outlook and avoiding security problems is the next:

    1. Use interop for Outlook 2003;
    2. Create new email and set it save folder;
    3. Do not use base64 embedding, outlook 2007 does not support it; do not reference files on your disk, they won't be send; do not use word editor inspector because you will get security warnings on some machines;
    4. Attachment must have png/jpg extension. If it will have for instance tmp extension - Outlook will warn user;
    5. Pay attention how CID is generated without mapi;
    6. Do not access properties via getters or you will get security warnings on some machines.

      public static void PrepareEmail()
      {
          var attachFile = Path.Combine(
              Application.StartupPath, "mySuperImage.png"); // pay attention that image must not contain spaces, because Outlook cannot inline such images
      
          Microsoft.Office.Interop.Outlook.Application outlook = null;
          NameSpace space = null;
          MAPIFolder folder = null;
          MailItem mail = null;
          Attachment attachment = null;
          try
          {
              outlook = new Microsoft.Office.Interop.Outlook.Application();
              space = outlook.GetNamespace("MAPI");
              space.Logon(null, null, true, true);
      
              folder = space.GetDefaultFolder(OlDefaultFolders.olFolderSentMail);
              mail = (MailItem) outlook.CreateItem(OlItemType.olMailItem);
      
              mail.SaveSentMessageFolder = folder;
              mail.Subject = "Hi Everyone";
              mail.Attachments.Add(attachFile, OlAttachmentType.olByValue, 0, Type.Missing); 
              // Last Type.Missing - is for not to show attachment in attachments list.
      
              string attachmentId = Path.GetFileName(attachFile);
              mail.BodyFormat = OlBodyFormat.olFormatHTML;
      
               mail.HTMLBody = string.Format("
      ", attachmentId); mail.Display(false); } finally { ReleaseComObject(outlook, space, folder, mail, attachment); } }

提交回复
热议问题