Hi I am sending email using Microsoft Graph API message. All is going well except that when the file attachment is excel (xlsx) the attached email file shows an error upon opening up the file.
The error is "Excel cannot open the file "Myfile.xlsx" because the file format or file extension is not valid. Verify that file has not been corrupted and that the file extension matches the format of the file".
The thing to note here, I do not gett issue the the word document and I am able to open it without any issue. Also I don't think there is an issue of permission since I have old project which uses some other tool for the email and when I received the attached excel file using that email I do not see an issue.
Code:
private static MessageAttachmentsCollectionPage GetAttachments(List<HttpPostedFileBase> fileUploader)
{
var attachmentPage = new MessageAttachmentsCollectionPage();
if (fileUploader != null)
{
foreach (var file in fileUploader)
{
var fileAttachment = new FileAttachment();
fileAttachment.Name = file.FileName;
//For xlsx content type is application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
fileAttachment.ContentType = file.ContentType;
fileAttachment.ContentBytes = ConvertToBytes(file);
attachmentPage.Add(fileAttachment);
}
}
return attachmentPage;
}
private static byte[] ConvertToBytes(HttpPostedFileBase item)
{
byte[] data;
var bytes = new byte[item.InputStream.Length];
item.InputStream.Position = 0;
item.InputStream.Read(bytes, 0, bytes.Length);
data = bytes;
return data;
}