Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
Fix uncaught exception when sending email
Browse files Browse the repository at this point in the history
Only attempt to attach the user's profile picture if they actually have
one.

If the user does not have a profile picture `pictureMemoryStream` is
null and the code throws an exception when attempting to add it as an
attachment.
  • Loading branch information
juanpescador committed Oct 7, 2019
1 parent 03267f8 commit e4078a7
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions MicrosoftGraphAspNetCoreConnectSample/Helpers/GraphService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,21 @@ public static async Task SendEmail(GraphServiceClient graphClient, IHostingEnvir
// Load user's profile picture.
var pictureStream = await GetMyPictureStream(graphClient, httpContext);

// Copy stream to MemoryStream object so that it can be converted to byte array.
var pictureMemoryStream = new MemoryStream();
await pictureStream.CopyToAsync(pictureMemoryStream);

// Convert stream to byte array and add as attachment.
attachments.Add(new FileAttachment
if (pictureStream != null)
{
ODataType = "#microsoft.graph.fileAttachment",
ContentBytes = pictureMemoryStream.ToArray(),
ContentType = "image/png",
Name = "me.png"
});
// Copy stream to MemoryStream object so that it can be converted to byte array.
var pictureMemoryStream = new MemoryStream();
await pictureStream.CopyToAsync(pictureMemoryStream);

// Convert stream to byte array and add as attachment.
attachments.Add(new FileAttachment
{
ODataType = "#microsoft.graph.fileAttachment",
ContentBytes = pictureMemoryStream.ToArray(),
ContentType = "image/png",
Name = "me.png"
});
}
}
catch (Exception e)
{
Expand Down

0 comments on commit e4078a7

Please sign in to comment.