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 or SmtpMail.AddAttachmentAsync 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.ImportHtmlBodyAsync and SmtpMail.ImportHtmlAsyncAsync 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.

[C# - Send Email with Embedded Image from Windows Store Apps - XAML]
using EASendMail;
using System.Threading.Tasks;

private async Task SendEmail()
{
    String Result = "";
    try
    {
        SmtpMail oMail = new SmtpMail("TryIt");
        SmtpClient oSmtp = new SmtpClient();

        // Set sender email address, please change it to yours 
        oMail.From = new MailAddress("test@emailarchitect.net");

        // Add recipient email address, please change it to yours
        oMail.To.Add(new MailAddress("support@emailarchitect.net"));

        // Set email subject
        oMail.Subject = "test email from C# XAML project";

        // Your SMTP server address
        SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net");

        // User and password for ESMTP authentication            
        oServer.User = "test@emailarchitect.net";
        oServer.Password = "testpassword";

        // If your SMTP server requires TLS connection on 25 port, please add this line
        // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

        // If your SMTP server requires SSL connection on 465 port, please add this line
        // oServer.Port = 465;
        // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

        // get a file path from PicturesLibrary, 
        // to access files in PicturesLibrary, you MUST have "Pictures Library" checked in
        // your project -> Package.appxmanifest -> Capabilities
        Windows.Storage.StorageFile file = 
            await Windows.Storage.KnownFolders.PicturesLibrary.GetFileAsync("test.jpg");

        string attfile = file.Path;
        Attachment oAttachment = await oMail.AddAttachmentAsync(attfile);

        // if you want to add attachment from remote URL instead of local file.
        // string attfile = "http://www.emailarchitect.net/test.jpg";
        // Attachment oAttachment = await oMail.AddAttachmentAsync(attfile);

        // you can change the Attachment name by
        // oAttachment.Name = "mytest.jpg";

        // Specifies the attachment as an embedded image
        string contentID = "test001@host";
        oAttachment.ContentID = contentID;
        oMail.HtmlBody = "<html><body>this is an <img src=\"cid:" + contentID + "\"> embedded picture.</body></html>";        
        
        await oSmtp.SendMailAsync(oServer, oMail);
        Result = "Email was sent successfully!";
    }
    catch (Exception ep)
    {
        Result = String.Format("Failed to send email with the following error: {0}", ep.Message);
    }

    // Display Result by Diaglog box
    Windows.UI.Popups.MessageDialog dlg = new
        Windows.UI.Popups.MessageDialog(Result);
           
    await dlg.ShowAsync();
}

[VB - Send Email with Embedded Image from Windows Store Apps - XAML]
Imports EASendMail

Private Async Function SendEmail() As Task
    Dim Result As String = ""
    Try

        Dim oMail As New SmtpMail("TryIt")
        Dim oSmtp As New SmtpClient()

        ' Set sender email address, please change it to yours
        oMail.From = New MailAddress("test@emailarchitect.net")

        ' Add recipient email address, please change it to yours
        oMail.To.Add(New MailAddress("support@emailarchitect.net"))

       ' Set email subject
        oMail.Subject = "test email from VB XAML project"

        ' Your SMTP server address
        Dim oServer As New SmtpServer("smtp.emailarchitect.net")

        ' User and password for ESMTP authentication            
        oServer.User = "test@emailarchitect.net"
        oServer.Password = "testpassword"

        ' If your SMTP server requires TLS connection on 25 port, please add this line
        ' oServer.ConnectType = SmtpConnectType.ConnectSSLAuto

        ' If your SMTP server requires SSL connection on 465 port, please add this line
        ' oServer.Port = 465
        ' oServer.ConnectType = SmtpConnectType.ConnectSSLAuto

        ' get a file path from PicturesLibrary, 
        ' to access files in PicturesLibrary, you MUST have "Pictures Library" checked in
        ' your project -> Package.appxmanifest -> Capabilities
        Dim file As Windows.Storage.StorageFile = 
            Await Windows.Storage.KnownFolders.PicturesLibrary.GetFileAsync("test.jpg")

        Dim attfile As String = file.Path
        Dim oAttachment As Attachment = Await oMail.AddAttachmentAsync(attfile)

        ' if you want to add attachment from remote URL instead of local file.
        ' Dim attfile As String = "http://www.emailarchitect.net/test.jpg"
        ' Dim oAttachment As Attachment = Await oMail.AddAttachmentAsync(attfile)

        ' you can change the Attachment name by
        ' oAttachment.Name = "mytest.jpg"

        ' Specifies the attachment as an embedded image 
        Dim contentID As String = "test001@host"
        oAttachment.ContentID = contentID
        oMail.HtmlBody = "<html><body>this is an <img src=""cid:" + contentID + """> embedded picture.</body></html>"        
        
        Await oSmtp.SendMailAsync(oServer, oMail)
        Result = "Email was sent successfully!"

    Catch ep As Exception
        Result = String.Format("Failed to send email with the following error: {0}", ep.Message)
    End Try

    ' Display Result by Diaglog box
    Dim dlg As New Windows.UI.Popups.MessageDialog(Result)
    Await dlg.ShowAsync()
End Function

[JavaScript - Send Email with Embedded Image from Windows Store Apps - HTML5]
function prepareMail() {
    var result = "";

    var oMail = new EASendMail.SmtpMail("TryIt");

    // Set sender email address, please change it to yours 
    oMail.from = new EASendMail.MailAddress("test@emailarchitect.net");

    // Add recipient email address, please change it to yours
    oMail.to.add(new EASendMail.MailAddress("support@emailarchitect.net"));

    // Set email subject
    oMail.subject = "test email from JavaScript HTML5 project";

    // get a file path from PicturesLibrary, 
    // to access files in PicturesLibrary, you MUST have "Pictures Library" checked in
    // your project -> Package.appxmanifest -> Capabilities
    Windows.Storage.KnownFolders.picturesLibrary.getFileAsync("test.jpg").then(function (file) {

        var attfile = file.path;
        // if you want to add attachment from remote URL instead of local file.
        // var attfile = "http://www.emailarchitect.net/test.jpg";
 
        oMail.addAttachmentAsync(attfile).then(function (oAttachment) {
            // you can change the Attachment name by
            // oAttachment.name = "mytest.jpg";

            
            // Specifies the attachment as an embedded image
            var contentID = "test001@host";
            oAttachment.contentID = contentID;
            oMail.htmlBody = "<html><body>this is an <img src=\"cid:" + contentID + "\"> embedded picture.</body></html>";
            
            sendMailEx(oMail);
        },
        // error handle
        function (e) {
            // because javascript exception only gives the stack trace messages, but it is not
            // real description of exception, so we give a property lastErrorMessage for javascript.
            if (oMail.lastErrorMessage != "") {
                result = oMail.lastErrorMessage;
            }
            else {
                result = e.message;
            }

            (new Windows.UI.Popups.MessageDialog(result, "Error Information")).showAsync();
            return;
        });
    },

    // error handle
    function (e) {
        result = e.message;
        (new Windows.UI.Popups.MessageDialog(result, "Error Information")).showAsync();
        return;
    });
}

function sendMailEx( oMail )
{
    var result = "";
    var oSmtp = new EASendMail.SmtpClient();

    // Your SMTP server address
    var oServer = new EASendMail.SmtpServer("smtp.emailarchitect.net");

    // User and password for ESMTP authentication   
    oServer.user = "test@emailarchitect.net";
    oServer.password = "testpassword";

    // If your SMTP server requires TLS connection on 25 port, please add this line
    // oServer.connectType = EASendMail.SmtpConnectType.connectSSLAuto;

    // If your SMTP server requires SSL connection on 465 port, please add this line
    // oServer.port = 465;
    // oServer.connectType = EASendMail.SmtpConnectType.connectSSLAuto;

    oSmtp.sendMailAsync(oServer, oMail).then(function (e) {
        result = "Email was sent successfully!";

        // Display Result by Diaglog box
        (new Windows.UI.Popups.MessageDialog(result, "Success")).showAsync();
    },

    function (e) {
        // because javascript exception only gives the stack trace messages, but it is not
        // real description of exception, so we give a property lastErrorMessage for javascript.
        if (oSmtp.lastErrorMessage != "") {
            result = oSmtp.lastErrorMessage;
        }
        else {
            result = e.message;
        }
        oSmtp.close();

        // Display Result by Diaglog box
        (new Windows.UI.Popups.MessageDialog(result, "Error Information")).showAsync();
    });
}

See Also

SmtpMail.AddAttachmentAsync Method
SmtpMail.AddAttachment Method
SmtpMail.ImportHtmlBodyAsync Method
SmtpMail.ImportHtmlAsync Method