Search code examples
c#jsonemail-client

Microsoft.Graph send email with inline image not showing image inline but as attachment


Our application sends emails for confirmation etc. Previously, it used SMTP. We set up the mail with linked resources and it shows the images inline just fine.

Now one client switched to Azure so I have added code to send an email via Azure. The mail is sent and received but the inline image is not shown in the body. It is there as an attachment where I don't want to see it.

This is my SendMailPostRequestBody (serialized for reading purposes):

    {
      "AdditionalData": {},
      "BackingStore": {
        "ReturnOnlyChangedValues": false,
        "InitializationCompleted": true
      },
      "Message": {
        "Attachments": [{
                "ContentType": "text/plain",
                "IsInline": false,
                "LastModifiedDateTime": null,
                "Name": "file1.PDF",
                "Size": null,
                "AdditionalData": {
                    "ContentBytes": {
                        "ValueKind": 3
                    }
                },
                "BackingStore": {
                    "ReturnOnlyChangedValues": false,
                    "InitializationCompleted": true
                },
                "Id": "file1.PDF",
                "OdataType": "#microsoft.graph.fileAttachment"
            }, {
                "ContentType": "text/plain",
                "IsInline": false,
                "LastModifiedDateTime": null,
                "Name": "file2.pdf",
                "Size": null,
                "AdditionalData": {
                    "ContentBytes": {
                        "ValueKind": 3
                    }
                },
                "BackingStore": {
                    "ReturnOnlyChangedValues": false,
                    "InitializationCompleted": true
                },
                "Id": "file2.pdf",
                "OdataType": "#microsoft.graph.fileAttachment"
            }, {
                "ContentType": "image/jpeg",
                "IsInline": true,
                "LastModifiedDateTime": null,
                "Name": "Logo.jpg",
                "Size": null,
                "AdditionalData": {
                    "ContentBytes": {
                        "ValueKind": 3
                    }
                },
                "BackingStore": {
                    "ReturnOnlyChangedValues": false,
                    "InitializationCompleted": true
                },
                "Id": "Logojpg",
                "OdataType": "#microsoft.graph.fileAttachment"
            }
        ],
        "BccRecipients": null,
        "Body": {
            "AdditionalData": {},
            "BackingStore": {
                "ReturnOnlyChangedValues": false,
                "InitializationCompleted": true
            },
            "Content": "<html>\t<body>\t\t<table cellspacing=\"0\" cellpadding=\"0\" class=\"mailimage mailimage1\"><tbody><tr><td><img src=\"cid:Logojpg\" alt=\"Logojpg\" ></td></tr></tbody></table></body></html>    ",
            "ContentType": 1,
            "OdataType": null
        },
        "BodyPreview": null,
        "CcRecipients": [],
        "ConversationId": null,
        "ConversationIndex": null,
        "Extensions": null,
        "Flag": null,
        "From": {
            "AdditionalData": {},
            "BackingStore": {
                "ReturnOnlyChangedValues": false,
                "InitializationCompleted": true
            },
            "EmailAddress": {
                "AdditionalData": {},
                "Address": "info@client.nl",
                "BackingStore": {
                    "ReturnOnlyChangedValues": false,
                    "InitializationCompleted": true
                },
                "Name": null,
                "OdataType": null
            },
            "OdataType": null
        },
        "HasAttachments": null,
        "Importance": null,
        "InferenceClassification": null,
        "InternetMessageHeaders": null,
        "InternetMessageId": null,
        "IsDeliveryReceiptRequested": null,
        "IsDraft": null,
        "IsRead": null,
        "IsReadReceiptRequested": null,
        "MultiValueExtendedProperties": null,
        "ParentFolderId": null,
        "ReceivedDateTime": null,
        "ReplyTo": null,
        "Sender": {
            "AdditionalData": {},
            "BackingStore": {
                "ReturnOnlyChangedValues": false,
                "InitializationCompleted": true
            },
            "EmailAddress": {
                "AdditionalData": {},
                "Address": "info@client.nl",
                "BackingStore": {
                    "ReturnOnlyChangedValues": false,
                    "InitializationCompleted": true
                },
                "Name": null,
                "OdataType": null
            },
            "OdataType": null
        },
        "SentDateTime": null,
        "SingleValueExtendedProperties": null,
        "Subject": "refnr 00412/16818 *** DEMO ***",
        "ToRecipients": [{
                "AdditionalData": {},
                "BackingStore": {
                    "ReturnOnlyChangedValues": false,
                    "InitializationCompleted": true
                },
                "EmailAddress": {
                    "AdditionalData": {},
                    "Address": "my@mail.nl",
                    "BackingStore": {
                        "ReturnOnlyChangedValues": false,
                        "InitializationCompleted": true
                    },
                    "Name": null,
                    "OdataType": null
                },
                "OdataType": null
            }
        ],
        "UniqueBody": null,
        "WebLink": null,
        "Categories": null,
        "ChangeKey": null,
        "CreatedDateTime": null,
        "LastModifiedDateTime": null,
        "AdditionalData": {},
        "BackingStore": {
            "ReturnOnlyChangedValues": false,
            "InitializationCompleted": true
        },
        "Id": null,
        "OdataType": "#microsoft.graph.message"
    },
    "SaveToSentItems": false
    }

I do get the email and it has 3 attachments. One of them is the image. I was expecting this one not the be visible as attachment when shown inline in the body.

The contentBytes don't show it, but there is a base64 string in it. I can open the attachments in the mail in outlook.

Why is the image not showing up in the body?


Solution

  • I figured it out.

    Apparently you have to add "ContentId" to the AdditionalData dictionary of the attachment.

    So for an attachment that is used to show an image inline the addtionaldata dictionary should look like this:

    "AdditionalData": {
                "ContentBytes": "the-base64-bytes-string",
                "ContentId": "the-id-used-in-the-img-src"
            }
    

    Then oulook will be able to show the image.