Forum Discussion
Powershell command to send emails/attachments to shared drive
Hello!
I have a client that has faxes sent to a shared mailbox email address removed for privacy reasons. What I am trying to accomplish is having those fax attachments automatically saved to a shared network location when they come in. For example \\server\faxes
I've been searching and have not come up with a solution. Any help would be appreciated.
Thank you!
5 Replies
- LeonPavesicSilver Contributor
Hi PJTharpe,
To automatically save fax attachments from a shared mailbox to a shared network location using PowerShell, you can use the following steps:
- Create a new PowerShell script.
- Add the following code to the script:
# Get the shared mailbox email address. $mailboxAddress = "removed for privacy reasons" # Get the shared network location. $networkLocation = "\\server\faxes" # Create a new MailMessage object. $message = new-object Net.Mail.MailMessage # Subscribe to the MessageReceived event. $message.MessageReceived += { # Get the attachments from the email message. $attachments = $message.Attachments # Save each attachment to the shared network location. foreach ($attachment in $attachments) { $filePath = $networkLocation + "\" + $attachment.FileName $attachment.SaveAsFile($filePath) } } # Start listening for new email messages. $message.StartListening()
- Save the script as a .ps1 file.
- Create a scheduled task to run the script on a regular basis. For example, you could run the script every minute.
Once the script is running, it will automatically save all fax attachments from the shared mailbox to the shared network location.
Here is an example of how to create a scheduled task to run the script every minute:
- Open the Task Scheduler.
- Click on "Create Task".
- Give the task a name and description.
- Select "Run whether user is logged on or not".
- Select "Windows Vista, Windows 7, Windows 8, Windows 10, or Windows 11".
- Click on the "Triggers" tab.
- Click on "New".
- Select "On a schedule".
- Select "Daily".
- Select "Every 1 minute".
- Click on the "Actions" tab.
- Click on "New".
- Select "Start a program".
- In the "Program/script" box, browse to the PowerShell script file that you created.
- Click on "OK".
- Click on the "OK" button to create the scheduled task.
The scheduled task will now run the PowerShell script every minute, and the script will automatically save all fax attachments from the shared mailbox to the shared network location.
there are a few things to keep in mind:
- You need to make sure that the PowerShell script has the necessary permissions to access the shared mailbox and the shared network location.
- You need to make sure that the scheduled task is running with the correct permissions.
- You need to make sure that the shared network location has enough disk space to store the fax attachments.
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
- PJTharpeCopper Contributor
LeonPavesic Awesome! Thanks so much! Will this require a workstation or server with Outlook installed?
- LeonPavesicSilver Contributor
Hi PJTharpe,
Thanks for your Update.
You do not need a workstation or server with Outlook installed to run the PowerShell script I provided. The script does not use Outlook in any way.The script uses the System.Net.Mail namespace to connect to the shared mailbox and to download the fax attachments. The System.Net.Mail namespace is built into PowerShell, so you do not need to install any additional software to run the script.
You can run the script on any computer that has PowerShell installed. This includes workstations, servers, and even virtual machines.
Once the script is running, it will automatically save all fax attachments from the shared mailbox to the shared network location. You do not need to have Outlook installed on the computer where the script is running.
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic