Cool method for scripting attaching a file and emailing using Powershell. The script also displays how to zip up a destination using Out-zip.
$sender = sender@host.com
$recipient = recipient@host.com
$server = mail.host.com
$targetFolder = c:\MyFolder
$file = c:\MyZipFile.zipif ( [System.IO.File]::Exists($file) )
{
remove-item -force $file
}gi $targetFolder | out-zip $file $_
$subject = “Sending a File ” + [System.DateTime]::Now
$body = “I’m sending a file!”
$msg = new-object System.Net.Mail.MailMessage $sender, $recipient, $subject, $body
$attachment = new-object System.Net.Mail.Attachment $file
$msg.Attachments.Add($attachment)
$client = new-object System.Net.Mail.SmtpClient $server
$client.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$client.Send($msg)


















0 Responses to “PowerShell Script: Zip up a folder and email it”