Hi @Nandan Hegde ,
Thanks for reaching out.
Firstly, as per this Azure document, we recommend sending an email from a runbook with SendGrid.
However, if you still want to go with Send-MailMessage cmdlet approach then I dont have SMTP server handy to test but you may try to resolve the issue by replacing
$Emailcred= Get-AzureKeyVaultSecret -vaultName KVname -name SecretName
with
$KeyVault = Get-AzKeyVaultSecret -VaultName KVname -Name SecretName
$KeyVaultSecretString = (Get-AzKeyVaultSecret -VaultName KVname -Name SecretName).SecretValueText
$User = $KeyVault.Name
$PWord = ConvertTo-SecureString -String $KeyVaultSecretString -AsPlainText -Force
$Emailcred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
The reason for it is, the type of
$Emailcred= Get-AzureKeyVaultSecret -vaultName KVname -name SecretName
is PSKeyVaultSecret. For illustration, please check below screenshot.
Whereas, Send-MailMessage cmdlets Credential parameter accepts only PSCredential type value.
So above code that I have provided would fetch your KeyVault secret name and secret value and then store it in PSCredential object that matches to the Send-MailMessage cmdlets Credential parameters accepted type so it would resolve your issue.