Sending email from Command-line via outlook without having to click send
Asked Answered
J

5

23

I need to send email via command-line without any human interactions for automation.

I know we can use mailto command but that would compose email, subject,body and everything but it wouldn't send it unless I click send.

I read online we can use blat but I cannot use anything other than outlook.

This is closed post I have found Link to SOF post.

just for your information: I am looking into some telnet commands to send email haven't gotten success in that yet either. telnet commands to send email

Jhvh answered 15/3, 2013 at 13:2 Comment(0)
S
26

Option 1
You didn't say much about your environment, but assuming you have it available you could use a PowerShell script; one example is here. The essence of this is:

$smtp = New-Object Net.Mail.SmtpClient("ho-ex2010-caht1.exchangeserverpro.net")
$smtp.Send("[email protected]","[email protected]","Test Email","This is a test")

You could then launch the script from the command line as per this example:

powershell.exe -noexit c:\scripts\test.ps1

Note that PowerShell 2.0, which is installed by default on Windows 7 and Windows Server 2008R2, includes a simpler Send-MailMessage command, making things easier.

Option 2
If you're prepared to use third-party software, an option is something like this SendEmail command-line tool. It depends on your target environment, though; if you're deploying your batch file to multiple machines, that will obviously require inclusion (but not formal installation) each time.

Option 3
You could drive Outlook directly from a VBA script, which in turn you would trigger from a batch file; this would let you send an email using Outlook itself, which looks to be closest to what you're wanting. There are two parts to this; first, figure out the VBA scripting required to send an email. There are lots of examples for this online, including from Microsoft here (old version). Essence of this is:

Sub SendMessage(DisplayMsg As Boolean, Optional AttachmentPath)
    Dim objOutlook As Outlook.Application
    Dim objOutlookMsg As Outlook.MailItem
    Dim objOutlookRecip As Outlook.Recipient
    Dim objOutlookAttach As Outlook.Attachment
    
    Set objOutlook = CreateObject("Outlook.Application")
    Set objOutlookMsg  = objOutlook.CreateItem(olMailItem)
    
    With objOutlookMsg
        Set objOutlookRecip = .Recipients.Add("Nancy Davolio")
        objOutlookRecip.Type = olTo
        ' Set the Subject, Body, and Importance of the message.
        .Subject = "This is an Automation test with Microsoft Outlook"
        .Body = "This is the body of the message." &vbCrLf & vbCrLf
        .Importance = olImportanceHigh  'High importance
        
        If Not IsMissing(AttachmentPath) Then
            Set objOutlookAttach = .Attachments.Add(AttachmentPath)
        End If
        
        For Each ObjOutlookRecip In .Recipients
            objOutlookRecip.Resolve
        Next
        
        .Save
        .Send
    End With
    Set objOutlook = Nothing
End Sub

Then, launch Outlook from the command line with the /autorun parameter, as per this answer (alter path/macroname as necessary):

C:\Program Files\Microsoft Office\Office11\Outlook.exe" /autorun macroname

Option 4
You could use the same approach as option 3, but move the Outlook VBA into a PowerShell script (which you would run from a command line). Example here. This is probably the tidiest solution, IMO.

Spice answered 15/3, 2013 at 13:18 Comment(11)
sorry, my script is batch based.Jhvh
So you'd just create the PowerShell script, then launch it from your batch file. You'd use parameters, as per this question.Spice
Thanks for great links, but all of those require login to smtp server and I cannot store my user and password to any script that is very insecure.Jhvh
I added a couple of other options there. You may also want to reconsider doing this in a batch file - depends a lot on how/when/why you want to trigger, etc.Spice
Thanks Geoff, I ended up creating PowerShell script sends email using gmail :( I have no other option, and in my batch script I send output to txt file and call Powershell script from there and in my PS script I cat that file in body part of the script. it is work around but I got working temporarily.Jhvh
Geoff, you should add this, link to your Option 1. it sends email via existing outlook setup via powershell but doesn't require smtp server or passs and stuff. andyparkhill.co.uk/2010/08/…Jhvh
@Mowgli: The link you show there is an example of option 3, I think (which would be my solution, if it were me).Spice
@Oidium are you using Bash from WSL2? If so, you could use the PowerShell approach above from my answer, and call the PowerShell script using something like powershell.exe -File "c:\users\myuser\Desktop\mymailscript.ps1". Note that the path is the native windows path, not the /mnt path.Spice
@Geoff: I'm a Mac user whose organization uses Outlook. :-) Although I'm quite comfortable with Windows in general, I am a novice with PowerShell. I'm very familiar with Bash scripting, however, so I was wondering if there's a script approach to using Outlook on macOS.Oidium
@Oidium ok, makes sense now! I'd say you want to ask a fresh question in that context, in that caseSpice
Option 2 link is dead, maybe replace with web archive link? web.archive.org/web/20160823050449/http://caspian.dotconf.net/…Norward
D
5

Using Powershell you can send a Email and integrate this into Bat file script. But you need to have outlook installed for this on your machine. This worked for me on local PC but not on server. On server, it asks me to give permission everytime I use this command, also installing outlook on server might not be preferred for many. This works perfectly when ran on local PC

$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "[email protected]"
$Mail.Subject = "Action"
$Mail.Body ="Pay rise please"
$Mail.Send()
Deaconry answered 24/9, 2021 at 7:17 Comment(0)
F
1

Send SMS/Text Messages from Command Line with VBScript!

If VBA meets the rules for VB Script then it can be called from command line by simply placing it into a text file - in this case there's no need to specifically open Outlook.

I had a need to send automated text messages to myself from the command line, so I used the code below, which is just a compressed version of @Geoff's answer above.

Most mobile phone carriers worldwide provide an email address "version" of your mobile phone number. For example in Canada with Rogers or Chatr Wireless, an email sent to <YourPhoneNumber>@pcs.rogers.com will be immediately delivered to your Rogers/Chatr phone as a text message.

* You may need to "authorize" the first message on your phone, and some carriers may charge an additional fee for theses message although as far as I know, all Canadian carriers provide this little-known service for free. Check your carrier's website for details.

There are further instructions and various compiled lists of worldwide carrier's Email-to-Text addresses available online such as this and this and this.


Code & Instructions

  1. Copy the code below and paste into a new file in your favorite text editor.
  2. Save the file with any name with a .VBS extension, such as TextMyself.vbs.

That's all!
Just double-click the file to send a test message, or else run it from a batch file using START.

Sub SendMessage()
    Const EmailToSMSAddy = "[email protected]"
    Dim objOutlookRecip
    With CreateObject("Outlook.Application").CreateItem(0)
        Set objOutlookRecip = .Recipients.Add(EmailToSMSAddy)
        objOutlookRecip.Type = 1
        .Subject = "The computer needs your attention!"
        .Body = "Go see why Windows Command Line is texting you!"
        .Save
        .Send
    End With
End Sub

Example Batch File Usage:

START x:\mypath\TextMyself.vbs

Of course there are endless possible ways this could be adapted and customized to suit various practical or creative needs.

Fishworm answered 15/11, 2018 at 13:42 Comment(6)
Thank you for your answer. I did the same. It ran without any errors. But I do not get any E-Mail in my mailbox. Also looked into Junk, Spam as well. Rechecked with spelling mistakes or any. Script runs with no issues. But why do I not see any mail ? :( This is disappointing.Deaconry
This was intended not for sending email, but for SMS Messages (text messages), so theoretically you should have a message show up in your "Sent Items", and a text messages arrive at the phone number specified in the script..Fishworm
...However, in theory it should work for sending email as well, but I just tested it and it no longer works for me either. It's been a few years since I used that so I'm not sure what the problem could be, but my guess is it's related to some new security setting in Outlook that can probably be disabled. (oh, and you need to have Outlook installed.) Sorry I don't have time to troubleshoot. Nowadays I send SMS messages similarly through Email-to-SMS, but by calling a PHP script on my webserver with PHP's simple mail() command.Fishworm
Perhaps you need to change an Outlook setting to allow "programmatic access". I think when I wrote that answer it may have been enabled by default.... but that's just a guess.Fishworm
Also note that is not a BAT file… it’s a VBS file (visual basic script, which is similar to Office’s VBA). Here is more info about running VBS. Another possible problem could be your antivirus software preventing access (since this could easily be abused by malware). Good luck & feel free to update the answer if you find a solution! :-)Fishworm
Thank you very much for your detailed reply. Appreciate it. I already also had gone through those outlook allow programmatic access settings as well. But unable to find out still. Also I found out that I could send mail using powershell scripting, but that too runs successfully without errors and returns no email on server (yes I need to install outlook on server for this on server, which I don't want to). On server, it asks everytime for my confirmation. I Will post incase if I find any solution. Thanks a lot for ur timeDeaconry
N
0

Option 1. SMTP

Language-specific libraries: (.NET), or (Python).

Some documentation: SmtpClient class in C#. SmtpClient Class (System.Net.Mail) | Microsoft Learn

Option 1 and 2 in https://mcmap.net/q/271170/-sending-email-from-command-line-via-outlook-without-having-to-click-send . (Language: PowerShell)

See https://mcmap.net/q/219814/-c-mailto-with-attachment and Adding an attachment to email using C# for another example.

Option 2. MAPI

See https://mcmap.net/q/219814/-c-mailto-with-attachment for an example (C#).

https://github.com/mhammond/pywin32/blob/3e9da59af065a35485107a688e588690ffd72cdf/com/win32comext/mapi/demos/mapisend.py has another example (Python, use pywin32 library).

Option 3. Outlook interop

Option 3 and 4 in https://mcmap.net/q/271170/-sending-email-from-command-line-via-outlook-without-having-to-click-send . (Language: VBA)

https://mcmap.net/q/271433/-automate-outlook-web-app-by-using-selenium-python (Python, use pywin32 library)

https://mcmap.net/q/271170/-sending-email-from-command-line-via-outlook-without-having-to-click-send (Language: VBA)

https://mcmap.net/q/271170/-sending-email-from-command-line-via-outlook-without-having-to-click-send (Language: PowerShell)

Clearly documented reading of emails functionality with python win32com outlook (Language: Python)

  • Advantage: don't require any special permission.

  • Disadvantage: requires Windows OS and Outlook Desktop to be installed.

That having said, it may happen that you get the warning message

A program is trying to automatically send Email on your behalf.

Refer to How to avoid Outlook security alert when reading outlook message from C# program for ways to automate this.

Option 4. Exchange Web Services (EWS)

Some documentation can be found in Send email messages by using EWS in Exchange | Microsoft Learn .

See https://mcmap.net/q/271434/-sending-email-through-ms-outlook-and-disabling-the-warning for an example (C#).

Option 5. PnP

Documentation can be found at https://pnp.github.io/cli-microsoft365/cmd/outlook/mail/mail-send/ .

Requires authorization.

Option 6. Use Selenium to automate Outlook webapp

https://mcmap.net/q/271433/-automate-outlook-web-app-by-using-selenium-python

  • Advantage: don't require any special permission.

  • Disadvantage: Highly not recommended, code will be very fragile, because the webapp will be updated frequently.

Option 7. Use Process.Start("mailto:...")

See c# Send Email using Process.Start for an example.

  • Disadvantage: it will only open the dialog. The user still need to click Send button.

For adding an attachment refer to Open default mail client along with a attachment .

Norward answered 19/2 at 8:29 Comment(0)
M
-5

You can use cURL and CRON to run .php files at set times.

Here's an example of what cURL needs to run the .php file:

curl http://localhost/myscript.php

Then setup the CRON job to run the above cURL:

nano -w /var/spool/cron/root
or
crontab -e

Followed by:

01 * * * * /usr/bin/curl http://www.yoursite.com/script.php

For more info about, check out this post: https://www.scalescale.com/tips/nginx/execute-php-scripts-automatically-using-cron-curl/

For more info about cURL: What is cURL in PHP?

For more info about CRON: http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800

Also, if you would like to learn about setting up a CRON job on your hosted server, just inquire with your host provider, and they may have a GUI for setting it up in the c-panel (such as http://godaddy.com, or http://1and1.com/ )

NOTE: Technically I believe you can setup a CRON job to run the .php file directly, but I'm not certain.

Best of luck with the automatic PHP running :-)

Mollusc answered 5/1, 2016 at 19:35 Comment(1)
the guy asked for outlook ... not for some php solution .. cheersSpringtime

© 2022 - 2024 — McMap. All rights reserved.