Adding inline attachments by using the EWS Managed API 2.0

You can use the Exchange Web Services (EWS) Managed API to add inline attachments to an item.

Last modified: February 06, 2014

Applies to: EWS Managed API | Exchange Server 2007 Service Pack 1 (SP1) | Exchange Server 2010

Note: This content applies to the EWS Managed API 2.0 and earlier versions. For the latest information about the EWS Managed API, see Web services in Exchange.

To add an inline attachment to an email message

  1. Create the HTML body with the content identifier of the attachment.

    string html = @"<html>
                      <head>
                      </head>
                      <body>
                        <img width=100 height=100 id=""1"" src=""cid:Party.jpg"">
                      </body>
                    </html>";
    
    Dim html As String = "<html><head></head><body><img width=100 height=100" & _
                         " id=""1"" src=""cid:Party.jpg""></body></html>"
    
  2. Create the email message.

    EmailMessage email = new EmailMessage(service);
    email.Subject = "Test Email";
    email.Body = new MessageBody(BodyType.HTML, html);
    email.ToRecipients.Add("michael@contoso.com");
    
    Dim email As New EmailMessage(service)
    email.Subject = "Test Email"
    email.Body = New MessageBody(BodyType.HTML, html)
    email.ToRecipients.Add("michael@contoso.com")
    
  3. Add the attachment to the local copy of the email message and identify that the attachment is inline.

    string file = @"C:\Users\contoso\Pictures\Party.jpg";
    email.Attachments.AddFileAttachment("Party.jpg", file);
    email.Attachments[0].IsInline = true;
    email.Attachments(0).ContentId = "Party.jpg"
    
    Dim file As String = "C:\Users\contoso\Pictures\Party.jpg"
    email.Attachments.AddFileAttachment("Party.jpg", file)
    email.Attachments(0).IsInline = True
    email.Attachments(0).ContentId = "Party.jpg"
    
  4. Send and save a copy of the email with the inline attachment.

    email.SendAndSaveCopy();
    
    email.SendAndSaveCopy()
    

The SendAndSaveCopy() method results in the following three calls to EWS:

  • A CreateItem call to create the email message on the server

  • A CreateAttachment call to create the attachment on the email message on the server

  • A SendItem call to send the email that includes the inline attachment

Example

The following code example shows how to add an inline attachment named Party.jpg.

Note

Outlook Web App may not display the inline image when the image is saved to the default Drafts folder.

static void EmailWithInlineAttachment(ExchangeService service)
{
   // Create the HTML body with the content identifier of the attachment.
   string html = @"<html>
                     <head>
                     </head>
                     <body>
                        <img width=100 height=100 id=""1"" src=""cid:Party.jpg"">
                     </body>
                     </html>";
         
   // Create the email message.
   EmailMessage email = new EmailMessage(service);
   email.Subject = "Test Email";
   email.Body = new MessageBody(BodyType.HTML, html);
   email.ToRecipients.Add("michael@contoso.com");

   // Add the attachment to the local copy of the email message.
   string file = @"C:\Users\contoso\Pictures\Party.jpg";
   email.Attachments.AddFileAttachment("Party.jpg", file);
   email.Attachments[0].IsInline = true;
   email.Attachments(0).ContentId = "Party.jpg"
         
   // Save a copy of the email, add the attachment, and then send the email. This method results in three calls to EWS.
   email.SendAndSaveCopy();
}
Sub EmailWithInlineAttachment(ByVal service As ExchangeService)
   ' Create the HTML body with the content identifier of the attachment.
   Dim html As String = "<html><head></head><body><img width=100 height=100" & _
                        " id=""1"" src=""cid:Party.jpg""></body></html>"

   ' Create the email message.
   Dim email As New EmailMessage(service)
   email.Subject = "Test Email"
   email.Body = New MessageBody(BodyType.HTML, html)
   email.ToRecipients.Add("michael@contoso.com")

   ' Add the attachment to the local copy of the email message.
   Dim file As String = "C:\Users\contoso\Pictures\Party.jpg"
   email.Attachments.AddFileAttachment("Party.jpg", file)
   email.Attachments(0).IsInline = True
   email.Attachments(0).ContentId = "Party.jpg"

   ' Save a copy of the email, add the attachment, and then send the email. This method results in three calls to EWS.
   email.SendAndSaveCopy()
End Sub

Compiling the code

For information about compiling this code, see Getting started with the EWS Managed API 2.0.

Robust programming

  • Write appropriate error handling code for common search errors.

  • Review the client request XML that is sent to the Exchange server.

  • Review the server response XML that is sent from the Exchange server.

  • Set the service binding as shown in Setting the Exchange service URL by using the EWS Managed API 2.0. Do not hard code URLs because if mailboxes move, they might be serviced by a different Client Access server. If the client cannot connect to the service, retry setting the binding by using the AutodiscoverUrl(String) method.

  • Set the target Exchange Web Services schema version by setting the requestedServerVersion parameter of the ExchangeService constructor. For more information, see Versioning EWS requests by using the EWS Managed API 2.0.

Security

  • Use HTTP with SSL for all communication between client and server.

  • Always validate the server certificate that is used for establishing the SSL connections. For more information, see Validating X509 certificates by using the EWS Managed API 2.0.

  • Do not include user names and passwords in trace files.

  • Verify that Autodiscover lookups that use HTTP GET to find an endpoint always prompt for user confirmation; otherwise, they should be blocked.