Friday, November 8, 2024
HomePowershellUtilizing Ship-MgUserMail because the Ship-MailMessage substitute

Utilizing Ship-MgUserMail because the Ship-MailMessage substitute


I used the Ship-MailMessage cmdlet rather a lot prior to now for testing Obtain Connectors in Trade or for emailing stories in scheduled PowerShell scripts. Whenever you attempt to use the Ship-MailMessage cmdlet, it has been exhibiting you this message for fairly some time now:

WARNING: The command ‘Ship-MailMessage’ is out of date. This cmdlet doesn’t assure safe connections to SMTP servers. Whereas there isn’t any speedy substitute obtainable in PowerShell, we suggest you don’t use Ship-MailMessage at the moment. See https://aka.ms/SendMailMessage for extra data.

On this weblog submit, I’ll present you the brand new method of sending emails utilizing Ship-MgUserMail.

Word: This works for Trade On-line, not for native Trade installations or some other service working SMTP.

Preparations

(Self-Signed) Certificates

For authenticating Microsoft Graph, you should use a self-signed certificates which you’ll be able to create working the next strains in PowerShell. The certificates file will likely be in c:temp and its title will likely be just like the $certname variable, on this case, “SendEmail.cer”. The strains beneath will import the certificates within the Certificates Retailer of the consumer working the script, if you happen to use it for Scheduled Duties… You must run it because the consumer specified within the Scheduled Job after all.

Word: For testing/private use, it’s okay to make use of a Self-Signed certificates however I strongly advise utilizing a company certificates.

$certname = "SendEmail"
$cert = New-SelfSignedCertificate -Topic "CN=$certname" -CertStoreLocation "Cert:CurrentUserMy" -KeyExportPolicy Exportable -KeySpec Signature -KeyLength 2048 -KeyAlgorithm RSA -HashAlgorithm SHA256
Export-Certificates -Cert $cert -FilePath "C:Temp$certname.cer"

Registering an App for authentication

To be able to connect with Microsoft Graph, you need to authenticate first after all. In case you register an app, you should use the IDs from that in your scripts. Steps are:

  • Go to App Registrations
  • Choose New Registration
  • Enter ‘Graph Electronic mail’ (For instance, select your personal most popular one) as Identify and choose Register
  • Make observe of the Utility (shopper) ID and the Listing (tenant) ID
  • Choose Certificates and secrets and techniques
  • Choose Certificates
  • Choose Add certificates
  • Choose the folder icon on the Choose a file line and browse to your certificates from the earlier step
  • Enter “SendEmail” (For instance) because the description and choose Add
  • Make observe of the Thumbprint by right-clicking the Thumbprint line and deciding on Copy
  • Choose API permissions
  • Choose Add a permission
  • Choose Microsoft Graph
  • Choose Utility permissions
  • Enter “mail.ship” within the search field, choose “Mail.Ship” and choose Add Permission
  • Choose Grant admin consent for… and choose Sure

Sending an e-mail utilizing Ship-MgUserMail

Now that the authentication half is completed, you can begin utilizing it for connecting to 365. For This it’s essential must Microsoft.Graph module put in in your system, you are able to do this by working:

Set up-Module Microsoft.Graph
Import-Module Microsoft.Graph

Now that the module is put in, you’ll be able to ship an e-mail with an attachment (c:tempoutput.csv on this instance) by working: (Change the XXXX, From, To, Topic, Physique and attachment variable to your personal values)

Choose-MgProfile -Identify "Beta"
Join-MgGraph -ClientId XXXX -TenantId XXXX -CertificateThumbprint XXXX -ContextScope CurrentUser

$from = "admin@Mpowershellisfun.com"
$to = "hurt@powershellisfun.com"
$topic = "PowerShell is enjoyable :)"
$sort = "html"
$physique = "See hooked up file"
$attachment = "c:tempoutput.csv"
$FileName = (Get-Merchandise -Path $Attachment).title
$base64string = [Convert]::ToBase64String([IO.File]::ReadAllBytes($Attachment))
  
$recipients = @()
$recipients += @{
    emailAddress = @{
        handle = $To
    }
}

$message = @{
    topic      = $topic
    toRecipients = $recipients
    Attachments  = @{
        title         = $FileName
        contenttype="textual content/plain"
        contentbytes = $base64string
    }
    physique         = @{
        contentType = $sort
        content material     = $physique
    }
}
 
Ship-MgUserMail -UserId $from -Message $message

Extra details about Ship-MgUserMail may be discovered right here

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments