Mailozaurr icon indicating copy to clipboard operation
Mailozaurr copied to clipboard

Adding attachment >3 MB not possible if no client secret is used

Open scriptkiddy666 opened this issue 1 year ago • 6 comments

Hi, maybe it's a bug, but I'm unable to send attachments which are bigger than 3 MB via a managed identity or a normal app registration with certificate based authentication. After creating a client secret for the same app registration it worked. (But I don't want to use a client secret.) Is it possible, that the function which handles the upload somehow doesn't use the already existing session? Because the upload doesn't work, but the email is send without problems (and without an attachment of course).

image

I used the following commands: Connect-MgGraph -Identity or Connect-MgGraph -ClientId $ClientId -TenantId $TenantId -CertificateThumbprint $Thumbprint

Send-EmailMessage -From 'XXX' -To 'XXX' -HTML "XXX" -Subject 'XXX' -Graph -ReplyTo "XXX" -MgGraphRequest -Attachment "C:\temp\XXX.pdf" -Verbose

(By using the parameter -Credential and the function ConvertTo-GraphCredential for generating the credential object and removing the parameter -MgGraphRequest it worked.)

Thanks, Markus

scriptkiddy666 avatar Jul 29 '24 14:07 scriptkiddy666

Try using PS 7, it should provide better error reporting. Also do you have proper rights to upload files? As ReadWrite Emails? Send is not enough.

PrzemyslawKlys avatar Jul 29 '24 15:07 PrzemyslawKlys

Same with PS7 (do I have to "reinstall" the module for PS7?): image

Yes, both, my managed identity and the app registration have Mail.Send and Mail.ReadWrite. (That should be fine, because it worked with the client secret for the same app registration.)

scriptkiddy666 avatar Jul 29 '24 15:07 scriptkiddy666

You don't have to reinstall module.

Just to clarify:

  • Does it work for 4MB plus with Connect-MgGraph and MgGraphRequest with ClientSecret?
  • It doesn't work with Connect-MgGraph and MgGraphRequest with Certificate?

PrzemyslawKlys avatar Jul 29 '24 15:07 PrzemyslawKlys

And I assume in all cases the mailbox does exists? The one you're sending it with?

PrzemyslawKlys avatar Jul 29 '24 15:07 PrzemyslawKlys

You don't have to reinstall module.

Just to clarify:

  • Does it work for 4MB plus with Connect-MgGraph and MgGraphRequest with ClientSecret? Yes. I just tried it again (also without executing Connect-MgGraph before) by just adding the -Credential parameter with the ClientSecret. Works like a charm. (The parameter -MgGraphRequeset was not added.)
  • It doesn't work with Connect-MgGraph and MgGraphRequest with Certificate? Yes. I used Connect-MgGraph with my certificate before executing Send-EmailMessage. (-Credential parameter of Send-EmailMessage removed and -MgGraphRequest added instead.)

And I assume in all cases the mailbox does exists? The one you're sending it with?

Correct. (It's the always the same mailbox and I also tried it with a different one, but the error is the same.)

scriptkiddy666 avatar Jul 30 '24 06:07 scriptkiddy666

I'm now using the following workaround without the need for a client secret:

$null = Connect-AzAccount -Identity
[System.Object]$AccessToken = (Get-AzAccessToken -ResourceTypeName 'MSGraph' -AsSecureString -WarningAction 'SilentlyContinue').Token
[System.Object]$Credentials = ConvertTo-GraphCredential -MsalToken ([System.Net.NetworkCredential]::new('', $AccessToken).Password)
[System.String[]]$Attachments = @(XXX)

Send-EmailMessage -From 'XXX' -To 'XXX' -HTML "XXX" -Subject 'XXX' -Graph -ReplyTo "XXX" -Credential $Credentials -Attachment $Attachments -DoNotSaveToSentItems

scriptkiddy666 avatar Aug 09 '24 11:08 scriptkiddy666

Thank you for this thread. I was struggling today with the same error as yours - Cannot index into a null array. - and it seems that it is not me being dumb but actually a bug. Version of Mailozaurr that I am using is 1.0.4 (code is executed from Azure Automations Account). Mail is being sent just fine with small attachments but with larger (~15MB) it throws an error. This workaround works great.

Koniasz avatar Mar 03 '25 22:03 Koniasz

This is pretty weird, as I can't see anything that would make sense for this, especially what we're using MgGraph, it has to be some bug on the Microsoft.Graph.Authentication

PrzemyslawKlys avatar Mar 05 '25 09:03 PrzemyslawKlys