I need to send an email through outlook that already has all the details filled in (eg. subject, body, etc), except it has to be done through some sort of script. This can be any script that works on Windows 10.
Essentially, all I need the script to do is 'click' send on the email. Is there anyway to do this, or is this impossible. The preferable script types are .bat
, .cmd
, and .vbs
. I don't want to use any 3rd party software.
This is what I currently have in my batch file, again I just need the script to send the email:
cd C:\Program Files (x86)\Microsoft Office\root\Office16 & start outlook.exe /c ipm.note /m "[email protected]?subject=subject here"
I will be filling out the body of the email by another script, which I have managed to do. If you have any ideas about alternative options, still reply, it might be useful.
Here is an image of what I need to be sent by a script file:
Thanks in advance.
Powershell is a scripting environment for Microsoft Windows. Scripts are saved as plain text files with the .ps1
extension. You can create a file in Windows Powershell ISE or a text editor. Here is the sample script which can be used to get the job done:
#create COM object named Outlook
$Outlook = New-Object -ComObject Outlook.Application
#create Outlook MailItem named Mail using CreateItem() method
$Mail = $Outlook.CreateItem(0)
#add properties as desired
$Mail.To = "[email protected]"
$Mail.Subject = "subject"
$Mail.HTMLBody = "testing"
#send message
$Mail.Send()
#quit and cleanup
$Outlook.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook) | Out-Null
But in the real world the code can be more complex:
Add-Type -assembly "Microsoft.Office.Interop.Outlook"
add-type -assembly "System.Runtime.Interopservices"
try
{
$outlook = [Runtime.Interopservices.Marshal]::GetActiveObject('Outlook.Application')
$outlookWasAlreadyRunning = $true
}
catch
{
try
{
$Outlook = New-Object -comobject Outlook.Application
$outlookWasAlreadyRunning = $false
}
catch
{
write-host "You must exit Outlook first."
exit
}
}
$namespace = $Outlook.GetNameSpace("MAPI")
See Outlook Email Automation with PowerShell for more information.
Note, you could run a powershell script from a batch file or command prompt (cmd.exe
):
powershell -noexit -File "C:\my_path\MYSCRIPT.ps1"
This is a method which Microsoft does when we right click on a ps1 script and click on "Run with PowerShell" :
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & 'C:\Users\USERNAME\Desktop\MYSCRIPT.ps1'"