Attachment.ContentID Property


Gets or sets the Content-ID of the embedded attachment.

[Visual Basic]
Public Property ContentID As String
[C#]
public string ContentID {get; set;}
[C++]
public: __property String^ get_ContentID();
public: __property void set_ContentID(String^);
[JScript]
public function get ContentID() : String;
public function set ContentID(String);

Property Value

A String value indicating the Content-ID of the attachment.

Remarks

It is not recommended to construct Attachment class instance directly. To add an attachment to email, please use SmtpMail.AddAttachment method, this method returns an Attachment class instance, ContentID and Name of the Attachment instance can be changed then.
To attach an embedded images to email, you should add an attachment to email at first. Then you should assign an unique identifier(contentid) to this attachment. Finally, you need to replace the <img src="your file name" /> to <img src="cid:yourcontentid" />.
To attach embedded images or pictures, SmtpMail.ImportHtmlBody and SmtpMail.ImportHtml methods are strongly recommended. You can also use the following sample code to attach an embedded picture manually.

Example

[Visual Basic, C#, C++, JScript.NET] To get the full samples of EASendMail, please refer to Samples section.

[VB - Email Attachment]

Imports EASendMail

Public Sub AttachFile()
    Try
        Dim oMail As SmtpMail = New SmtpMail("TryIt")
        Dim oAttachment As Attachment = oMail.AddAttachment("c:\test.gif")

        ' adds attachment from remote host
        ' Dim oAttachment As Attachment = oMail.AddAttachment("http://www.adminsystem.com/test.gif")

        ' changes attachment name to another name
        ' oAttachment.Name = "mytest.gif"

        ' specifies the attachment as an embedded picture
        ' Dim contentID As String = "test001@host"
        ' oAttachment.ContentID = contentID
        ' oMail.HtmlBody = "<html><body>this is a <img src=""cid:" & contentID & """> embedded picture.</body></html>"

        ' save as an eml file, you can use outlook express to open and check it.
        oMail.SaveAs("c:\test.eml", True)

    Catch exp As Exception
        Console.WriteLine(exp.Message)
    End Try
End Sub


[C# - Email Attachment] using System; using System.Collections; using EASendMail; void AttachFile() { try { SmtpMail ^oMail = gcnew SmtpMail("TryIt"); Attachment ^oAttachment = oMail->AddAttachment("c:\\test.gif"); // adds attachment from remote website // Attachment ^oAttachment = oMail->AddAttachment("http://www.adminsystem.com/test.gif"); // changes attachment name to another name // oAttachment->Name = "mytest.gif"; // specifies the attachment as an embedded picture // String^ contentID = "test001@host"; // oAttachment->ContentID = contentID; // oMail->HtmlBody = String::Format( "<html><body>this is a <img src=\"cid:{0}\"> embedded picture.</body></html>", contentID); // save as an eml file, you can use outlook express to open and check it. oMail->SaveAs("c:\\test.eml", true); } catch (Exception ^exp) { Console::WriteLine(exp->Message); } }
[C++ - Email Attachment] using namespace System; using namespace System::Collections; using namespace EASendMail; void AttachFile() { try { SmtpMail ^oMail = gcnew SmtpMail("TryIt"); Attachment ^oAttachment = oMail->AddAttachment("c:\\test.gif"); // adds attachment from remote website // Attachment ^oAttachment = oMail->AddAttachment("http://www.adminsystem.com/test.gif"); // changes attachment name to another name // oAttachment->Name = "mytest.gif"; // specifies the attachment as an embedded picture // String^ contentID = "test001@host"; // oAttachment->ContentID = contentID; // oMail->HtmlBody = String::Format( "<html><body>this is a <img src=\"cid:{0}\"> embedded picture.</body></html>", contentID); // save as an eml file, you can use outlook express to open and check it. oMail->SaveAs("c:\\test.eml", true); } catch (Exception ^exp) { Console::WriteLine(exp->Message); } }
[JScript - Email Attachment] public function AttachFile() { try { var oMail:SmtpMail = new SmtpMail("TryIt"); Attachment oAttachment = oMail.AddAttachment("c:\\test.gif"); // adds attachment from remote website // Attachment oAttachment = oMail.AddAttachment("http://www.adminsystem.com/test.gif"); // changes attachment name to another name // oAttachment.Name = "mytest.gif"; // specifies the attachment as an embedded picture // var contentID : String = "test001@host"; // oAttachment.ContentID = contentID; // oMail.HtmlBody = "<html><body>this is a <img src=\"cid:" + contentID + "\"> embedded picture.</body></html>"; // save as an eml file, you can use outlook express to open and check it. oMail.SaveAs("c:\\test.eml"); } catch(exp:System.Exception) { Console.WriteLine(exp.Message); } }

Online Examples

C# - Send Email with Attachment
C# - Send Email with Embedded Images
VB - Send Email with Attachment
VB - Send Email with Embedded Images
C++/CLI - Send Email with Attachment
C++/CLI - Send Email with Embedded Images