Hello,
I use the below script for sending email which is very sucessful, but i have 2 problems for which i require help.
1.) If there is no attachment the script sends email which i want to avoid it, if it has the attachment then only send email to recipient.
2.) I Send emails from my gmail account but if its attachment is above 25MB it fails to send the email, restrictions of gmail for above 25MB, do we have any way to sending attachment above 25MB, Please advise.
Text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | Function Write-Log($LogText){
$LogEntry=@"
$((Get-Date).ToString("dd-MM-yyyy HH:mm:ss")) > $($LogText)
"@
$LogEntry
$LogEntry | Add-Content 'c:\send\Log\success.txt'
}
$rootfolder='C:\send'
$files=Get-ChildItem "$rootfolder\CSV\*.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="$rootfolder\rar\$($_.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 "$rootfolder\Archive\"
Write-Log "Moved file $($file) to $rootfolder\Archive\"
Move-Item $attachment -Destination "$rootfolder\Archive\"
Write-Log "Moved file $($attachment) to $rootfolder\Archive\"
}else{
Write-Log "Email Send failed: $($error[0])";break
}
}
}
|