DOS Command Line - Send Email

Has anyone tried sending email using the DOS Command Line using a .BAT file? I’m testing with commandlineSMTP.com but running into issues with selecting “wildcard” filenames. I know command prompts use the * as the wildcard in a filename so that it finds the filename “ABC*” regardless of anything after the * but doesn’t seem to recognize the * in the script.

image

You should give powershell a shot instead, a lot cleaner

4 Likes

Does this require having the Microsoft Outlook Exchange application installed on the server? I was hoping to avoid having to do the install and keep it simple.

What is the error message reporting when you try to do the thing you’re trying to do? Command Line SMTP.                                                                                                                                                 

image

There is no error, it just won’t send the email if the name of the file attachment is not exact. The file name will not be exact since it could possible have Today’s date which changes depending on when the file was created. For example “C:\Report02252019.pdf”

image

it would then appear that the programmer doesn’t like wildcards are part of this switch, so you would have to get a list of the attachments via the batch file via an iterative for loop, store them into a variable, and pass that variable into the commandlineSMTP attachment switch. You could just pipe the file names into a text file (if you didn’t want to do all of this in memory) via a dir listing with whatever filters you want. Then, something like:

e.g.

for /F "tokens=*" %%A in (\\server\shared\file.txt) do call :startprocess %%A
:startprocess
set valueinfile=%1

call program.exe /switch %valueinfile%

But, Powershell is the right solution to this issue.

1 Like

Yes, I did receive the same ‘iterative function/for loop’ answer from their support but was a little bit more than what I was expecting so I might just head the Powershell route. I’ll probably just use a = Get-ChildItem “C:\Reports\” in the directory with Powershell. Can Powershell do the wildcard * for dynamic file names?

Yep you can use wildcards. Here’s an example I use to pick up filenames for making Epicor attachments / create the DMT file

Nancy

1 Like

For multiple attachment, use the /attachment: switch multiple times.
Like:

Commandlinesmtp.exe /smtpserver:smtp.verizon.net /to:me@example.net /from:billgates@ms.com /subject:"testing"  /message:"CommandLineSMTP" /username:bgates@ms.com /password:"Ih8j0bs" /port:465 /usessl:yes /attachment:"C:\temp\b1.txt" /attachment:"C:\temp\b2.txt"

So in a batch file you’d have to first build a variable that contained all the matching files found.

setlocal enabledelayedexpansion
set al=

FOR /r %%f IN (b*.txt) DO set al=!al! /attachment:%%f 
echo %al%

then add %al% in place of the/attachment` switch and file name

Commandlinesmtp.exe /smtpserver:smtp. ....   /usessl:yes %al%

But really … Move to PowerShell

EDIT

I cant believe I basically repeated @aidacra 's post :man_facepalming:

1 Like