locked
Help with attaching files from a remote server RRS feed

  • Question

  • Hi everyone,

    I am new to Powershell and I need help with some scripting.

    I have the following script that sends an email and attaches a .txt file to the email (see below).  I can do it from my C drive.  However, I need to attach a file from a server.  Can anyone help me with the code?

    Many thanks and best regards,

    Boris

    $Date= Get-Date

    $EmailFrom = "user1@abc.com"
    $EmailToAddresses = @(“user1@abc.com”,"user2@abc.com","user3@abc.com","user4@abc.com")
    $Subject = "Backup Test"
    $Body = "Scheduled Test successful" 
    $File = "C:\path\folder\abc_Log.txt"

    $Msg = New-Object System.Net.Mail.MailMessage $EmailFrom, $EmailTo, $Subject, $Body
    $Attachment =  New-Object System.Net.Mail.Attachment $File
    $Msg.Attachments.Add($Attachment)

    $SMTPServer = "smtp.gmail.com"
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("User Name", "User Password");
    foreach ($EmailTo in $EmailToAddresses)
    {
    $SMTPClient.Send($Msg)
    }

    Wednesday, April 10, 2019 6:36 PM

All replies

  • Setup a share on the server to the file, and make sure the user running the script has permissions. Otherwise, you would need to run the script on the server.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Wednesday, April 10, 2019 6:52 PM
  • Hi, thank you.  Can you explain in more detail what you mean by "set up a share on the server"?
    • Edited by Blokshin Wednesday, April 10, 2019 6:59 PM
    Wednesday, April 10, 2019 6:58 PM
  • Also, the script is saved on the server.  But it does not attach the file.   Thank you again
    • Edited by Blokshin Wednesday, April 10, 2019 7:01 PM
    Wednesday, April 10, 2019 7:00 PM
  • You should be using "Send-MailMessage".

    To attach a file just specify the file name.

    Send-MailMessage -To … -Boby … -Attachments \\server\share\file.txt

    That is all you have to do.

    help Send-MailMessage -online


    \_(ツ)_/

    Wednesday, April 10, 2019 8:58 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee

    Just do it.

    Thursday, April 25, 2019 9:08 AM