Hello people ,
I am fairly new to this language and has gotten good help from you guys. I have written a script (with some help) to email several files in a folder. This works great. But since this script is to be used on a bad internet connection I would like to maybe output the results from the script to a logfile so I can check this on a regular basis to see if all the mails have been sendt. Hope someone can help me :-) My code so far is:
#Connection Details
$username=”jfgs@gmail.com”
$password=”xxxxxxxx”
$smtpServer = “smtp.gmail.com”
$msg = new-object Net.Mail.MailMessage
#Change port number for SSL to 587
$smtp = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
#Uncomment Next line for SSL
$smtp.EnableSsl = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential( $username, $password )
#From Address
$msg.From = "CMSUPPORT@NFO.no"
#To Address, Copy the below line for multiple recipients
$msg.To.Add(“Chris@NFO.no”)
#Message Body
$msg.Body=”Please See Attached Database Files”
#Message Subject
$msg.Subject = “DATABASE”
write-host "SENDING FILES"
#your file location
$files=Get-ChildItem “C:\CM\Send\”
Foreach($file in $files)
{
Write-Host "Attaching File :- " $file
$attachment = New-Object System.Net.Mail.Attachment –ArgumentList $file.FullName
$msg.Attachments.Add($attachment)
$smtp.Send($msg)
$msg.Attachments.Clear()
}
$smtp.Send($msg)
$attachment.Dispose();
$msg.Dispose();
write-host "Mail Sent Successfully"Thank you :-)