Hello,
This Powershell script sends email reading a csv file and attachment of .pdf files perfectly. Tested working.
Now my requirement is to send files compressed by winrar in .rar as attachments but it fails to send.
Error: Email Send failed: Exception calling “Send” with “1” argument(s): “Failure sending mail.”
I have tried with Send-MailMessage, Rar is not blocked by system email security, then too it fails.
Please tell me is there any other option i should implement, to get successful.
Thanks in advance.
Function Write-Log($LogText){ $LogEntry=@” $((Get-Date).ToString(“dd-MM-yyyy HH:mm:ss”)) > $($LogText) “@ $LogEntry $LogEntry | Add-Content ‘c:sendLogsuccess.txt’ } $rootfolder=’C:send’ $files=Get-ChildItem “$rootfolderCSV*.csv” $def = “username@gmail.com” $from=$def ForEach($file in $files){ Write-Log “Found file $file” $content = Get-Content -Path $file $content = $content -replace ‘”‘,” } $content | ConvertFrom-Csv -delimiter ‘|’ | ForEach-Object{ #Import-CSV $file | ForEach-Object{ $attachment=”$rootfolderrar$($_.shippernbr).rar” $shippernbr = ($_.Shippernbr) $sub = ‘Subject #’ + ‘ ‘ + $shippernbr $Body = ‘Dear Sir, Please find attachment of files in winrar. Thanks.’ Write-Log “Attachment is $($attachment)” $email=$_.email Write-Log “Email address is $($email)” $emailCC=$_.emaillCC Write-Log “Email CC address is $($emailcc)” If($_.Email){ [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { return $true } $SMTPServer = “smtp.gmail.com” $SMTPPort = “587” $Username = $from $Password = “password” $to = $email $cc = $emailcc $subject = $sub $body = $Body $attachment = $attachment $message = New-Object System.Net.Mail.MailMessage $message.subject = $subject $message.body = $body $message.to.add($to) $message.cc.add($cc) $message.from = $username $message.attachments.add($attachment) $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort); $smtp.EnableSSL = $true $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password); $smtp.send($message) if($?){ Write-Log “Email Sent Successfully” Move-Item $file -Destination “$rootfolderArchive” Write-Log “Moved file $($file) to $rootfolderArchive” Move-Item $attachment -Destination “$rootfolderArchive” Write-Log “Moved file $($attachment) to $rootfolderArchive” }else{ Write-Log “Email Send failed: $($error[0])”;break } } }