# CID

CID is a special way to insert images or other media files directly into the body of an email so that they are displayed immediately, without the need to download them separately.

1. In the email, images are inserted using the tag `<img src='cid:unique_ID'>`.
2. The unique ID (Content-ID) links the image to this tag.
3. These IDs and the images themselves are stored in the database.
4. When displaying the email, the program searches for images by these IDs and shows them.

> This structure works for web pages; however, Outlook may not display such images. This is especially relevant for Office 2019.

## Exchange Connection Settings

<pre class="language-html" data-overflow="wrap"><code class="lang-html">$exchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2016 
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($exchangeVersion) 
<strong>$service.Credentials = New-Object System.Net.NetworkCredential($        , $        ) 
</strong>$service.Url = "https://        /        /Exchange.asmx"
</code></pre>

## Creating an Email

```html
$email = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($service) 
$email.Subject = $subject 
$email.Body = New-Object Microsoft.Exchange.WebServices.Data.MessageBody( 
    [Microsoft.Exchange.WebServices.Data.BodyType]::HTML, 
    "<html><body><p style = `"font-size: 15px; font-family: Calibri;`">${$header}<br>${$link}<br></p><img src='cid:MyImage'>$($        )</body></html>"
)
$emailAddresses = $Row.emails -split ';' | ForEach-Object { $_.Trim() } 
foreach ($address in $emailAddresses) { 
    $email.ToRecipients.Add($address) 
}
```

## Adding an Attachment with ContentId

```html
$filePath = $        
$attachment = $email.Attachments.AddFileAttachment($filePath) 
$attachment.ContentId = "MyImage" 
$attachment.IsInline = $true
```

## Sending the Email

```html
$email.SendAndSaveCopy()
```

<figure><img src="https://3199517203-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4mXXYsqBuhj7RyX6Y4Yw%2Fuploads%2Fgit-blob-a031fdcc9529fce89cfc2f313b392e84562fa83a%2Fimage%20(18).png?alt=media" alt=""><figcaption></figcaption></figure>
