다음을 통해 공유


Adding Inline Attachments with CDOSYS and System.Net.Mail.

In the following samples, I'm going to send an inline attachment using CDOSYS and System.Net.Mail (SNM).

Lets say you have an html document with text above and below a jpg file and you want this document inside of a message you are going to send.  Below is the the basic HTML document.   Notice that img tag holder the picture and is kept in a div statement. Also, note that the img tag has an src set to "mytestimage".  "mytestimage" is the Content ID (ie its a handle/lable/id/etc) for the image content.  When the attachment is added, "mytestimage" is referenced so that the inline attachment will be associated with the img HTML tag.   Please note that it seems that cid: is needed for the HTML of SNM, but will not work with  CDOSYS.

The HTML for CDOSYS:
--------------------
    <div style="font-family:Arial">This is an INLINE attachment:<BR>
        <BR>
        <img src="mytestimage" mce_src="mytestimage" alt=""><BR><BR>
    </div>
    <BR><BR>
    <B>NOTE: </B> The "cid:" used in this HTML will work with System.Net.Mail but not CDOSYS.<BR>

The HTML for System.Net.Mail:
-----------------------------

    <div style="font-family:Arial">This is an INLINE attachment:<BR>
        <BR>
        <img src="cid:mytestimage" mce_src="cid:mytestimage" alt=""><BR><BR>
    </div>
    <BR><BR>
    <B>NOTE: </B> The "cid:" used in this HTML will work with System.Net.Mail but not CDOSYS.<BR>

    Note: The string "mytestimage" inside of the img tag is the ContentId in the CDOSYS/System.Net.Mail code.

CDOSYS:
-------
    oMsg.AddRelatedBodyPart(InlineAttach1, InlineAttachContentId1, CDO.CdoReferenceType.cdoRefTypeLocation, "", "");

    // InlineAttach1 is the attachment file.
    // InlineAttachContentId1 is the Content ID tag from the html ("mytestimage").

System.Net.Mail
---------------
With System.Net.Mail (SNM) you can add an attchment with code similar to the following:
 
    Attachment oInlineAttachment = new Attachment(InlineAttach1);
    oInlineAttachment.ContentDisposition.Inline = true;
    oInlineAttachment.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;
    oInlineAttachment.ContentId = InlineAttachContentId1;
    oInlineAttachment.ContentType.MediaType = "image/jpg";
    oInlineAttachment.ContentType.Name = Path.GetFileName(InlineAttach1);
    oMsg.Attachments.Add(oInlineAttachment);

    // InlineAttach1 is the attachment file.
    // InlineAttachContentId1 is the Content ID tag from the html ("mytestimage").

It is also probably possible to add the image as a body part under CDOSY or Alternate View under System.Net.Mail. 

Adding an inline attachment does not seem to be possible with System.Web.Mail (SWM). There is no ContentId setting on the attachment.  There is no concept of body parts or Alternate Views.  So, this looks like a no-go as far as SWM is concerned.

I'll try to find out more about why cid is needed in one case and not the other.  Also, I'll try whip-up a sample of adding an inline attachment using a body part (CDOSYS) and SNM.