some particular parameters required while sending an email

This topic contains 25 replies, has 3 voices, and was last updated by  Deep 4 months ago.

  • Author
    Posts
  • #75316

    Ankit Parmar
    Participant

    I am trying to make script in which I want that a email will shoot to the recipient if it will get some .xml files in particular folder or if not then at the end of the day it will shoot an email to recipient with the message that "no file found". The whole script i am pasting below but i am not getting an idea how to adjust the second path in script that is if "files not found"

    $EmailFrom = ""
    $EmailTo = ""
    $EmailSubject = ""
    $Emailbody = ""

    $SMTPServer = "xx.basware.com"

    $emailattachment = "C:\Program Files (x86)\TIE Kinetix B.V\TIE Digipoort WUS Service\error\*.xml"

    function send_email {
    $mailmessage = New-Object system.net.mail.mailmessage
    $mailmessage.from = ($emailfrom)
    $mailmessage.To.add($emailto)
    $mailmessage.Subject = $emailsubject
    $mailmessage.Body = $emailbody

    $attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain')
    $mailmessage.Attachments.Add($attachment)

    #$mailmessage.IsBodyHTML = $true
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
    $SMTPClient.Send($mailmessage)
    }

  • #75320

    Simon B
    Participant

    you could try

    If (test-path $emailattatchment)
    {
    $emailsubject = "Today's XML File"
    $attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain')
    $mailmessage.Attachments.Add($attachment)
    }
    Else
    {
    $emailsubject = "No File Found"
    }

    • #75326

      Ankit Parmar
      Participant

      Hi Simon,

      Thanks buddy for your reply but can you please adjust your content in my script as i am not able to fit the content in my script and getting error.

      Thanks
      Ankit

  • #75400

    Simon B
    Participant

    think this should work

    $EmailFrom = ""
    $EmailTo = ""
    $EmailSubject = ""
    $Emailbody = ""

    $SMTPServer = "xx.basware.com"

    $emailattachment = "C:\Program Files (x86)\TIE Kinetix B.V\TIE Digipoort WUS Service\error\*.xml"

    function send_email {
    $mailmessage = New-Object system.net.mail.mailmessage
    $mailmessage.from = ($emailfrom)
    $mailmessage.To.add($emailto)
    $mailmessage.Subject = $emailsubject
    $mailmessage.Body = $emailbody

    If (test-path $emailattatchment)
    {
    $emailsubject = "Today's XML File"
    $attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain')
    $mailmessage.Attachments.Add($attachment)
    }
    Else
    {
    $emailsubject = "No File Found"
    }
    #$mailmessage.IsBodyHTML = $true
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
    $SMTPClient.Send($mailmessage)
    }

    • #75530

      Ankit Parmar
      Participant

      Hi Simon,

      Thanks for your reply but I have tried it and it didn't work although it is not giving error on execution but neither giving any result. Do you have any idea why it is happening.

      Thanks
      Ankit

  • #75538

    Deep
    Participant

    Hi Ankit,

    Please check below script if it works for you.

    $EmailFrom = ""
    $EmailTo = ""
    $EmailSubject = ""
    $Emailbody = ""

    $SMTPServer = "xx.basware.com"
    function send_email {
    $mailmessage = New-Object system.net.mail.mailmessage
    $mailmessage.from = ($emailfrom)
    $mailmessage.To.add($emailto)
    $mailmessage.Subject = $emailsubject
    $mailmessage.Body = $emailbody

    #Checks if the file exists and returns True or false accordingly
    $emailattachment = Test-Path -Path "C:\Program Files (x86)\TIE Kinetix B.V\TIE Digipoort WUS Service\error\*.xml"

    If ($emailattachment -eq "True")
    {
    $emailsubject = "Today's XML File"
    $attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain')
    $mailmessage.Attachments.Add($attachment)
    }
    Else
    {
    $emailsubject = "No File Found"
    }
    #$mailmessage.IsBodyHTML = $true
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
    $SMTPClient.Send($mailmessage)
    }

    • #75541

      Ankit Parmar
      Participant

      Hi,

      Thanks for replying but issue remains the same. It is executing but neither sending any email nor giving any error on console. Just don't understand where I am doing wrong.

      Thanks
      Ankit

  • #75542

    Deep
    Participant

    how are you calling the function (function send_email) ?

  • #75544

    Deep
    Participant

    what I've did is just created two different functions for sending email and those are called accordingly on the if loop trigger, I understand this can be done with the a single function by passing parameter to the function dynamically, if this work you can go ahead and create a single function for the sending emails.

    $EmailFrom = ""
    $EmailTo = ""
    $EmailSubject = ""
    $Emailbody = ""
    $SMTPServer = "xx.basware.com"
    $emailattachment = "C:\Program Files (x86)\TIE Kinetix B.V\TIE Digipoort WUS Service\error\*.xml"

    function Send-email_File {
    $mailmessage = New-Object system.net.mail.mailmessage
    $mailmessage.from = ($emailfrom)
    $mailmessage.To.add($emailto)
    $mailmessage.Subject = "Today's XML File details – Found"
    $mailmessage.Body = "Attached are the files which are in the Queue "
    $attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain')
    $mailmessage.Attachments.Add($attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
    $SMTPClient.Send($mailmessage)
    }

    function Send-email_NOfile {
    $mailmessage = New-Object system.net.mail.mailmessage
    $mailmessage.from = ($emailfrom)
    $mailmessage.To.add($emailto)
    $mailmessage.Subject = "Today's XML File details- Not Found"
    $mailmessage.Body = "No file is in the Queue"
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
    $SMTPClient.Send($mailmessage)
    }

    #Checks if the file exists and returns True or false accordingly
    $emailattachment = Test-Path -Path "C:\Program Files (x86)\TIE Kinetix B.V\TIE Digipoort WUS Service\error\*.xml"

    If ($emailattachment -eq "True")
    {
    Send-email_File
    }
    Else
    {
    Send-email_NOfile
    }

    • #75736

      Ankit Parmar
      Participant

      Hey..

      thanks a ton for your help and reply. Finally script is running and I got emails in my mailbox but the problem is attachments are missing in the email and giving some error which I pasted below. Whereas files are available in the folder which i have mentioned in attachment path in script. Second part is running fine i.e. when I remove those files from the folder then script is running without giving any error and got the right email with the right content i.e. "No file is in the Queue"

      ————————————————————————————

      Error
      ———————————————————-

      New-object : Cannot convert argument "0", with value: "True", for "Attachment" to type "System.IO.Stream": "Cannot convert
      value "True" to type "System.IO.Stream". Error: "Invalid cast from 'System.Boolean' to 'System.IO.Stream'.""
      At C:\Users\ankitpar\Downloads\Untitled8.ps1:12 char:15
      + ... ttachment = New-object System.Net.Mail.Attachment($emailattachment, ' ...
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
      + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

      Exception calling "Add" with "1" argument(s): "Value cannot be null.
      Parameter name: item"
      At C:\Users\ankitpar\Downloads\Untitled8.ps1:13 char:1
      + $mailmessage.Attachments.Add($attachment)
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : ArgumentNullException

      Thanks
      Ankit

  • #75737

    Deep
    Participant

    You can try by changing the variable name to $files as below.

    6. $files = "C:\Program Files (x86)\TIE Kinetix B.V\TIE Digipoort WUS Service\error\*.xml"

    14. $attachment = New-Object System.Net.Mail.Attachment($file, 'text/plain')

    • #75749

      Ankit Parmar
      Participant

      Nopes.. still issue remain the same.

  • #75772

    Deep
    Participant

    You can try below Script.

    Note:- I have used Sendgrid for sending out emails, this is working fine for me.

    function Send-email_File{
    Process{
    $File = "C:\Program Files (x86)\TIE Kinetix B.V\TIE Digipoort WUS Service\error\*.xml"
    $sub="Today's XML File details – Found"
    $body="Attached are the files which are in the Queue"
    $To=""
    $pass = ConvertTo-SecureString "" -AsPlainText -Force
    $smtpServer = ""
    $user = ""
    $credential = New-Object System.Management.Automation.PsCredential($user, $pass)

    Send-MailMessage -From "test@mydomain.com" -To $To -Subject $sub -Body $body -Attachments $file -SmtpServer $smtpServer -Credential $credential -UseSsl -Port 587
    }
    }

    function Send-email_NOfile {

    $sub="Today's XML File details – Not Found"
    $body="No file in the Queue"
    $To=""
    $pass = ConvertTo-SecureString "" -AsPlainText -Force
    $smtpServer = ""
    $user = ""
    $credential = New-Object System.Management.Automation.PsCredential($user, $pass)

    Send-MailMessage -From "test@mydomain.com" -To $To -Subject $sub -Body $body -SmtpServer $smtpServer -Credential $credential -UseSsl -Port 587

    }

    #Checks if the file exists and returns True or false accordingly
    $emailattachment = Test-Path -Path "C:\Program Files (x86)\TIE Kinetix B.V\TIE Digipoort WUS Service\error\*.xml"

    If ($emailattachment -eq "True")
    {
    Send-email_File
    }
    Else
    {
    Send-email_NOfile
    }

    If you have SMTP server in place please refer below SMTP script and change the script accordingly

    function Send-email{
    Process{
    $File = "C:\*txt"
    $sub=""
    $body=""
    $To=""
    $smtpServer = ""
    Send-MailMessage -From "test@mydomain.com" -To $To -Subject $sub -Body $body -Attachments $file -SmtpServer $smtpServer
    }
    }
    Send-email

    hope this would help you in some way. 🙂

    • #75871

      Ankit Parmar
      Participant

      Good morning,

      Thanks for your extensive help but few questions here –

      In below mentioned script lines, in first point do I need to put my local LAN user id in parameter or email id? In second point do I need to put my LAN password or email password?

      1. $user = ""
      2. $credential = New-Object System.Management.Automation.PsCredential($user, $pass)

      Thanks
      Ankit

  • #75872

    Deep
    Participant

    As I mentioned I checked sending out email via sendgrind (https://sendgrid.com/)

    $user = ""

    But if you have SMTP server in place you can use below script, just replace the function with the required changes.

    function Send-email{
    Process{
    $File = "C:\*txt"
    $sub=""
    $body=""
    $To=""
    $smtpServer = ""
    Send-MailMessage -From "test@mydomain.com" -To $To -Subject $sub -Body $body -Attachments $file -SmtpServer $smtpServer
    }
    }
    Send-email

    • #75875

      Ankit Parmar
      Participant

      Well as you suggested I have applied the second function in my script as we have smtp server. The script is given below and again it is running fine without giving any error but the issue is again the attachment is missing in email and the second function is working and I got the email "No file in the Queue" whereas xml files are placed ini folder. Any insight would be appreciated. Thanks in advance

      function Send-email_File{
      Process{
      $File = "C:\Users\ankitpar\Desktop\Digipoort\*.xml"
      $sub="Today's XML File details – Found"
      $body="Attached are the files which are in the Queue"
      $To="ankit.parmar@basware.com"
      $smtpServer = "mail.basware.com"
      Send-MailMessage -From "ankit.parmar@basware.com" -To $To -Subject $sub -Body $body -Attachments $File -SmtpServer $smtpServer
      }
      }

      function Send-email_NOfile {
      Process{
      $File = "C:\Users\ankitpar\Desktop\Digipoort\*.xml"
      $sub="Today's XML File details – Not Found"
      $body="No file in the Queue"
      $To="ankit.parmar@basware.com"
      $smtpServer = "mail.basware.com"
      Send-MailMessage -From "ankit.parmar@basware.com" -To $To -Subject $sub -Body $body -SmtpServer $smtpServer
      }
      }

      If ($File -eq "True")
      {
      Send-email_File
      }
      Else
      {
      Send-email_NOfile
      }

      Thanks
      Ankit

    • #75881

      Ankit Parmar
      Participant

      I have tried it in other way. Please find the below script but this time getting error of "Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response
      was: 5.7.1 Client was not authenticated" . Any suggestions .. Thanks
      ———————————————————————————

      $File = Test-Path C:\Users\ankitpar\Desktop\Digipoort\*.xml
      $fpath="C:\Users\ankitpar\Desktop\Digipoort\*.xml"
      $secpasswd = ConvertTo-SecureString “xxxx” -AsPlainText -Force
      $mycreds = New-Object System.Management.Automation.PSCredential (“abc@domain.com”, $secpasswd)
      $smtpServer = "xyz.domain.com"
      $To="abc@domain.com"

      function Send-email_File{
      Process{

      $sub="Today's XML File details – Found"
      $body="Attached are the files which are in the Queue"

      Send-MailMessage -From "abc@domain.com" -To $To -Subject $sub -Body $body -Attachments $fpath -SmtpServer $smtpServer -Credential $mycreds -UseSsl -Port "587"

      }
      }

      function Send-email_NOfile {
      Process{

      $sub="Today's XML File details – Not Found"
      $body="No file in the Queue"

      Send-MailMessage -From "abc@domain.com" -To $To -Subject $sub -Body $body -UseSsl -SmtpServer $smtpServer -Credential $mycreds -Port "587"
      }
      }

      If ($File)
      {
      Send-email_File
      }
      Else
      {
      Send-email_NOfile
      }

    • #75898

      Ankit Parmar
      Participant

      Deep,

      Finally the issue is solved. the issue was the file name was new.txt.txt and i am mentioning in script only new.txt. Actually I caught this later when I have checked the "File name extensions" tab in "View" and after that problem was solved.

      Thanks a lot for your time and efforts.

  • #75884

    Simon B
    Participant

    try this

    function Send-email_File{
    Process{
    $Files = get-ChildItem "C:\Users\ankitpar\Desktop\Digipoort\*.xml"
    $attachments = @()

    Foreach($file in $files){
    $filename =$file.FullName
    $attachments += $filename
    }

    $sub="Today's XML File details – Found"
    $body="Attached are the files which are in the Queue"
    $To="ankit.parmar@basware.com"
    $smtpServer = "mail.basware.com"
    Send-MailMessage -From "ankit.parmar@basware.com" -To $To -Subject $sub -Body $body -Attachments $attachments -SmtpServer $smtpServer
    }
    }

    • #75886

      Ankit Parmar
      Participant

      Thanks Simon for replying but by execution of script which you have provided I didn't get any email in my inbox though it's executed without any error.

  • #75887

    Simon B
    Participant

    what happens when you run :-

    function Send-email_File{
    Process{
    $Files = get-ChildItem "C:\Users\ankitpar\Desktop\Digipoort\*.xml"
    $attachments = @()

    Foreach($file in $files){
    $filename =$file.FullName
    $attachments += $filename
    Write-host "Adding file " $attachments # for testing only
    }

    $sub="Today's XML File details – Found"
    $body="Attached are the files which are in the Queue"
    $To="ankit.parmar@basware.com"
    $smtpServer = "mail.basware.com"
    Send-MailMessage -From "ankit.parmar@basware.com" -To $To -Subject $sub -Body $body -Attachments $attachments -SmtpServer $smtpServer
    }
    }
    function Send-email_NOfile {
    Process{

    $sub="Today's XML File details – Not Found"
    $body="No file in the Queue"
    $To="ankit.parmar@basware.com"
    $smtpServer = "mail.basware.com"
    Send-MailMessage -From "ankit.parmar@basware.com" -To $To -Subject $sub -Body $body -SmtpServer $smtpServer
    }
    }

    $path = Test-Path -path "C:\Users\ankitpar\Desktop\Digipoort\*.xml"

    If ($path -eq "True")
    {
    Send-email_File
    }
    Else
    {
    Send-email_NOfile
    }

    • #75890

      Ankit Parmar
      Participant

      This time got an email but attachment again missing. Got an email with this line -> "No file in the Queue"

  • #75892

    Simon B
    Participant

    were there files in the path "C:\Users\ankitpar\Desktop\Digipoort\*.xml" ? if so try just calling the Send-email_File to check that that works with sending all of the attachments in that Directory

    also on your If ($path -eq "True") I think it should be If ($path -eq $True)

    • #75895

      Ankit Parmar
      Participant

      Simon,

      Finally the issue is solved. the issue was the file name was new.txt.txt and i am mentioning in script only new.txt. Actually I caught this later when I have checked the "File name extensions" tab in "View" and after that problem was solved.

      Thanks a lot for your time.

  • #75899

    Simon B
    Participant

    Glad it is working now

  • #75902

    Deep
    Participant

    Cool 🙂

You must be logged in to reply to this topic.