0

I have written a small script that are using PSFTP to connect to a FTP server, download some files and conclude by sending an email with a summary of downloaded files.

I get a problem with the send-mailmessage part, which fails with this error: Send-MailMessage : The remote certificate is invalid according to the validation procedure.

When running the send-mailmessage alone, it works flawlessly, it is only when using it combined with PSFTP, it fails. I am guessing, that it is the due to having made a session with the ftpserver, that somehow interferes, but that session should be ended when we come to the send mail. I can't figure this out, can anybody help?

My script:


    #--------- FTP server variables here ------
    $User = "XXX"
    $Pass = "XXX"
    $EncryptedPass = ConvertTo-SecureString -String $Pass -asPlainText -Force
    $Credentials = New-Object System.Management.Automation.PSCredential($User,$EncryptedPass)
    $Server = "XXX"

    #-------- Mail credentials here -----------
    $User_ = "XXX"
    $Pass_ = "XXX"
    $EncryptedPass_ = ConvertTo-SecureString -String $Pass_ -asPlainText -Force
    $Mailcredentials = New-Object System.Management.Automation.PSCredential($User_,$EncryptedPass_)
    #$mailcredentials =
    $Subject ="'bowkerdownload'"
    $To = "XXX"
    $From = "XXX"
    $body = (get-content -path g:\resultout.txt) -join "`n"
     $SMTP =  "smtp.office365.com" 

    #---------File variables here --------------
    $month =  (Get-Date).AddMonths(-8).ToString('MMM') 
    $year = (Get-date).addyears(-1).ToString('yyyy') 
    $filesequential="_*_"   
    $filesize = "lc"  
    $filetype = ".txt" 
    $samlevar=($month + ''+$year + ''+$filesequential + ''+$filesize + ''+$filetype)
    $filename = $samlevar.tostring()


#------ The script---------

Set-FTPConnection -Credentials $Credentials -Server $server -Session defaultftpsession -UsePassive 
$Session = Get-FTPConnection -Session defaultftpsession  

Get-FTPChildItem -Path /Download -filter "$filename"  |
Get-FTPItem -session $Session -LocalPath g:\ -Overwrite | 


Convertfrom-string -PropertyNames Kode, Status, Action, fil |
Select-Object -Property Status, action, Fil |
Out-file g:\resultout.txt;


Send-MailMessage -To $To -Subject $Subject -Body $Body -SmtpServer $SMTP -From $From -Credential $Mailcredentials -port 587 -UseSsl``
Jan_sh
  • 1
  • What happens if you omit `-UseSsl` ? Maybe, [this answer](https://stackoverflow.com/a/26335731/9898643) can help? – Theo Dec 11 '19 at 12:47
  • Worth a try, but did not work, smtp requires ssl. And thank you for the link - while not exactly covering this problem, the explanation sheds some light on the problem cause. – Jan_sh Dec 11 '19 at 21:04

0 Answers0