AddInline Method


Add an embedded image file to current email.

[Syntax]
Visual C++: HRESULT AddInline( BSTR FileName, BSTR* pVal )
Visual Basic: AddInline( FileName As String ) As String 

Parameters

FileName
File or URL to be added.

Return Value

If this method succeeds, the return value is the Content-ID generated automatically by this method; otherwise the return value is null.

Remarks

In some cases, the e-mail body includes a hyper-link which refers to a picture. In order to embed the picture in current email, you can use AddInline/AddInlineEx method. First developer can use AddInline/AddInlineEx method to add a picture to an e-mail, then change the hyper-link so that it points to <img src="cid:Content-ID">. This Content-ID is returned by this method.
To attach embedded images or inline images, Mail.ImportHtml and Mail.ImportMailEx methods are strongly recommended.

Example

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

[VB6, VBA - Send Email with Embedded Images]

Private Sub btnSendMail_Click() 

    Dim oSmtp As New EASendMailObjLib.Mail 
    oSmtp.LicenseCode = "TryIt" 

    ' Set your sender email address
    oSmtp.FromAddr = "test@emailarchitect.net" 

    ' Add recipient email address
    oSmtp.AddRecipientEx "support@emailarchitect.net", 0 

    ' Set email subject
    oSmtp.Subject = "test HTML email from VB 6.0 with embedded images" 

    Dim cid As String 
    ' Add embedded image and return the unique identifier of the attachment
    cid = oSmtp.AddInline( "c:\test.gif" ) 
    If cid = "" Then 
        MsgBox "failed add embedded image with error:" & oSmtp.GetLastErrDescription() 
        Exit Sub 
    End If 

    ' Set HTML body format
    oSmtp.BodyFormat = 1 

    ' Use the cid as link in the body text
    oSmtp.BodyText = "<html><body>Hello, this is a embedded <img src=""cid:" & cid & _ 
            """ > picture.</body><html>" 

    ' Your SMTP server address
    oSmtp.ServerAddr = "smtp.emailarchitect.net" 

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

    ' If your smtp server requires SSL connection, please add this line
    ' oSmtp.SSL_init

    MsgBox "start to send email ..." 

    If oSmtp.SendMail() = 0 Then 
        MsgBox "email was sent successfully!" 
    Else 
        MsgBox "failed to send email with the following error:" & oSmtp.GetLastErrDescription() 
    End If 

End Sub 

[Delphi - Send Email with Embedded Images]

Unit Unit1; 

Interface 

Uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls, EASendMailObjLib_TLB; // add EASendMail Unit 

Type 
  TForm1 = Class(TForm) 
    Button1: TButton; 
    Procedure Button1Click(Sender: TObject); 
  private 
    { Private declarations } 
  public 
    { Public declarations } 
  End; 

Var 
  Form1: TForm1; 

Implementation 

{$R *.dfm} 

Procedure TForm1.Button1Click(Sender: TObject); 
Var 
  oSmtp : TMail; 
  cid : WideString; 
Begin 
  oSmtp := TMail.Create(Application); 
  oSmtp.LicenseCode := 'TryIt'; 

  // Set your sender email address
  oSmtp.FromAddr := 'test@emailarchitect.net'; 

  // Add recipient email address
  oSmtp.AddRecipientEx( 'support@emailarchitect.net', 0); 

  // Set email subject
  oSmtp.Subject := 'test HTML email from Delphi with embedded images'; 

  // Add embedded image and return the unique identifier of the attachment
  cid := oSmtp.AddInline('c:\test.jpg' ); 
  If cid = '' Then 
  Begin 
  ShowMessage( 'Failed to add embedded image with error: ' + 
      oSmtp.GetLastErrDescription()); 
      exit; 
  End; 

  // Set HTML body format
  oSmtp.BodyFormat := 1; 

  // Set HTML body
  oSmtp.BodyText := '<html><body>Hello, this is an embedded <img src="cid:' + 
  cid + '"> picture.</body></html>'; 

  // Your SMTP server address
  oSmtp.ServerAddr := 'smtp.emailarchitect.net'; 

  // User and password for ESMTP authentication
  oSmtp.UserName := 'test@emailarchitect.net'; 
  oSmtp.Password := 'testpassword'; 

  // If your SMTP server requires SSL connection, please add this line
  // oSmtp.SSL_init();

  ShowMessage( 'start to send email ...' ); 

  If oSmtp.SendMail() = 0 Then 
    ShowMessage( 'email was sent successfully!' ) 
  Else 
    ShowMessage( 'failed to send email with the following error: ' 
    + oSmtp.GetLastErrDescription()); 

End; 

End. 

[Visual C++ - Send Email with Embedded Images]

#include "stdafx.h" 

#include "easendmailobj.tlh" 
using namespace EASendMailObjLib; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    ::CoInitialize( NULL ); 

    IMailPtr oSmtp = NULL; 
    oSmtp.CreateInstance( __uuidof(EASendMailObjLib::Mail)); 
    oSmtp->LicenseCode = _T("TryIt"); 

    // Set your sender email address
    oSmtp->FromAddr = _T("test@emailarchitect.net"); 

    // Add recipient email address
    oSmtp->AddRecipientEx( _T("support@emailarchitect.net"), 0 ); 

    // Set email subject
    oSmtp->Subject = _T("HTML email from Visual C++ project with inline image"); 

    // Add embedded image and return the unique identifier of the attachment
    _bstr_t cid = oSmtp->AddInline( _T("c:\\test.gif")); 
    if( cid.length() == 0 ) 
    { 
        _tprintf(_T("failed add embedded image with error: %s"), 
            (const TCHAR*)oSmtp->GetLastErrDescription()); 
    } 

    // Set HTML body format
    oSmtp->BodyFormat = 1; 

    // Use the cid as link in the body text
    oSmtp->BodyText = _bstr_t("<html><body>Hello, this is a embedded <img src=\"cid:") + cid 
           + _bstr_t("\" > picture.</body><html>"); 

    // Your SMTP server address
    oSmtp->ServerAddr = _T("smtp.emailarchitect.net"); 

    // User and password for ESMTP authentication
    oSmtp->UserName = _T("test@emailarchitect.net"); 
    oSmtp->Password = _T("testpassword"); 

    // If your SMTP server requires SSL connection, please add this line
    // oSmtp->SSL_init();<

    _tprintf(_T("Start to send HTML email with embedded image ...\r\n" )); 

    if( oSmtp->SendMail() == 0 ) 
    { 
        _tprintf( _T("email was sent successfully!\r\n")); 
    } 
    else 
    { 
        _tprintf( _T("failed to send email with the following error: %s\r\n"), 
            (const TCHAR*)oSmtp->GetLastErrDescription()); 
    } 

    if( oSmtp != NULL ) 
        oSmtp.Release(); 

    return 0; 
} 

Online Examples

Visual C++ - Send HTML Email with Embedded Images
Visual Basic - Send HTML Email with Embedded Images
Delphi - Send HTML Email with Embedded Images

See Also

ImportHtml
ImportMailEx
ClearInline Method
AddAttachment Method