Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Tiens Adrien, petite fonction pour déclarer un envoi de mail:
  2. -----------------------------------------------------------------------------------------------
  3.  
  4. Function Envoi-Email {
  5.     Param (
  6.         [Parameter(`
  7.             Mandatory=$true)]
  8.         [String]$EmailTo,
  9.         [Parameter(`
  10.             Mandatory=$true)]
  11.         [String]$Subject,
  12.         [Parameter(`
  13.             Mandatory=$true)]
  14.         [String]$Body,
  15.         [Parameter(`
  16.             Mandatory=$true)]
  17.         [String]$EmailFrom="myself@gmail.com",  #This gives a default value to the $EmailFrom command
  18.         [Parameter(`
  19.             mandatory=$false)]
  20.         [String]$attachment,
  21.         [Parameter(`
  22.             mandatory=$true)]
  23.         [String]$Password
  24.     )
  25.  
  26.         $SMTPServer = "smtp.gmail.com"
  27.         $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
  28.         if ($attachment -ne $null) {
  29.             $SMTPattachment = New-Object System.Net.Mail.Attachment($attachment)
  30.             $SMTPMessage.Attachments.Add($SMTPattachment)
  31.         }
  32.         $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
  33.         $SMTPClient.EnableSsl = $true
  34.         $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($EmailFrom.Split("@")[0], $Password);
  35.         $SMTPClient.Send($SMTPMessage)
  36.         Remove-Variable -Name SMTPClient
  37.         Remove-Variable -Name Password
  38.  
  39. }
  40.  
  41. -----------------------------------------------------------------------------------------------
  42. Ensuite pour utiliser ta fonction:
  43. -----------------------------------------------------------------------------------------------
  44.  
  45. Envoi-Email -EmailTo "Myself@gmail.com" -Body "Test Body" -Subject "Test Subject" -attachment "C:\cdf.pdf" -password "Passowrd"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement