TUTOS.EU

Envoyer un mail en powershell

Envoyer un mail via une passerelle smtps

Ceci est un exemple "à l'arrache" car des lignes commentées ne servent pas, sauf pour au cas où, mais cela fonctionne

Clear-Host

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

#Variables Mail
$fromaddress = "expediteur@mondomaine.com"
$toaddress = "cible@mondomaine.com"
$Subject = "Test" 
$body = "Test"
$attachment = "D:\monfichier.xlsx" 
$smtpserver = "smtps.mondomaine.com" 

#Envoi Mail
#$message = new-object System.Net.Mail.MailMessage
#$message.From = $fromaddress 
#$message.To.Add($toaddress)
#$message.IsBodyHtml = $True 
#$message.Subject = $Subject 
#$attach = new-object Net.Mail.Attachment($attachment) 
#$message.Attachments.Add($attach) 
#$message.body = $body 
#$SMTPClient = new-object Net.Mail.SmtpClient($smtpserver, 587) 
$SMTPClient = New-Object System.Net.Mail.SmtpClient($smtpServer, 587)
$SMTPClient.EnableSsl = $true
#$SMTPClient.Credentials = New-Object System.Net.NetworkCredential('monlogin@mondomaine.com', 'monmotdepasse')
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential('monlogin@mondomaine.com', 'monmotdepasse')

#$SMTPClient.Send($message)
$SMTPClient.Send($fromaddress, $toaddress, $Subject, $body)
Lien vers le fichier : cliquez ici Copier le code

2