Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PowerShell sending email fails with .rar attachment

    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.

    Code:
    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   
    }
     }
     }

  2. #2
    Administrator
    Join Date
    Mar 2001
    Location
    St Louis, Missouri, USA
    Posts
    20,554
    Thanks
    2
    Thanked 614 Times in 550 Posts
    Can you get more information about the actual error?

    Is the attachment too big?

    Does your email security scan the content of compressed attachments and block based on that?

    Joe

  3. #3
    New Lounger
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by JoeP517 View Post
    Can you get more information about the actual error?

    Is the attachment too big?

    Does your email security scan the content of compressed attachments and block based on that?

    Joe
    Hi,

    Highly thankful for response.

    Yes the attachment is 1MB plus but below 25MB restricted by gmail for above 25MB

    I tried disabling the firewall, anti virus from other machine Home, Office the same error.

    Error: Email Send failed: Exception calling "Send" with "1" argument(s): "Failure sending mail."

    error.png

    Hope to have favourable reply.
    Last edited by John_2014; 2014-01-31 at 13:51.

  4. #4
    5 Star Lounger
    Join Date
    Jan 2010
    Location
    Los Angeles, CA
    Posts
    797
    Thanks
    3
    Thanked 28 Times in 26 Posts
    I tried your code and sending a winrar file worked for me. I tried inserting various errors into the code to try to simulate the error message you got but was unsuccessful. I suggest temporarily removing the code that reads the csv file and hard code sending a single rar file and get that to work first (that's what I did with your code). In fact, get the temporary code working with a hard-coded PDF file name and then switch that name for a hard-coded rar file name. (Successful debugging usually involves removing any superfluous code and dealing with only the bare minimum amount of code to accomplish the task. Once that code is working you can start to add complexity one piece at a time until it breaks, then you'll know where to look for what went wrong)

    If that still doesn't work, try setting a mime type for the attachment. To do this you will have to first create an Attachment object. Example:

    $attachment = New-Object System.Net.Mail.Attachment("c:\path\to\your\rarfil e.rar")
    $attachment.ContentType.MediaType = "application/x-rar-compressed"
    $message.attachments.add($att)

    I don't think you really have to do this because the default mime type is "application/octet-stream" which is acceptable for winrar, but it is worth a shot.

  5. #5
    New Lounger
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello,

    Thanks for advise i will try and resolve it with your suggestions.
    Thanks for supporting.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •