telnet with ehlo script to send email in Windows & mutt specifying remote Smtp relay server

I recall someone ever mention we could send an email (think it's via Linux) using telnet smtp_relay_server_IP 25' followed by EHLO & several commands.

Can someone write me a Windows batch or VB script (ideally not PowerShell but if no choice, we'll use PowerShell) to send an email using this method (see below).  Need to attach a file to send out. If it's not possible to attach, read in a text file into the mail's body & send out

A sample is shown below:


 I login directly to the Postfix server & test it:
c:\> telnet localhost 25
> 220 smtp.yy.zz ESMTP
> helo me
> 250 servernm.yy.zz
> MAIL FROM: <fromsms1@yy.zz>
> 250 Ok
> RCPT TO: <mail@yy.zz>
> 550 <mail@yy.zz>:
> ...Ok
> recipient table


Q2:
What's the syntax to specify in Linux to use Mutt to attach a file & send out via a remote Smtp relay server?
sunhuxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunhuxAuthor Commented:
Thanks for adding more domains.
My bigger priority is to get a script in Windows 2008 R2 to
be able to send the email: for mutt, I can google around

A working 'telnet' that I've tried is as follows:

HELO
501 Syntax: HELO hostname
HELO smtp.mypostfixdomain.com
250 hostname.mypostfixdomain.com
MAIL FROM:<sender_id@external_domain.com.sg>
250 2.1.0 Ok
HELO smtp.mypostfixdomain.com
250 hostname.mypostfixdomain.com
MAIL FROM:<sender_id@external_domain.com.sg>
250 2.1.0 Ok
RCPT TO:<recipient_id@mypostfixdomain.com>
250 2.1.5 Ok

RCPT TO:<root@mypostfixdomain.com>
250 2.1.5 Ok
DATA
354 End data with <CR><LF>.<CR><LF>
testg lin1
line 2
line 3
.
250 2.0.0 Ok: queued as BAF8A200034
0
sunhuxAuthor Commented:
http://exchangeserverpro.com/how-to-send-email-via-telnet/

Basically I need to automate the steps given in above link in
a script so that I can use a scheduler to send email on a regular
basis (weekly or monthly)
0
sunhuxAuthor Commented:
http://caspian.dotconf.net/menu/Software/SendEmail/#download
I've also downloaded the above email agent for Windows which
does not require installation but after issuing the command below,
it just 'hangs' there for more than 5 minutes & came out with message
below:

C:\sendemail\> SendEmail.exe -f g-cutility@singtel.com -t mymail@gmail.com -u "testg" -m "this is test" -a "TODO.txt" -s 172.20.1.92:25

Jul 14 19:45:46 MyServer SendEmail[3416]: ERROR => Received:   421 4.7.0 mail
relay2.a-cloud.gov.au closing connection

When I did it in my laptop, pointing to gmail SMTP server, it sends
out within 2 secs.

From MyServer, I could do "telnet 172.20.1.92 25" & got an Smtp
message as both MyServer & 172.20.1.92 sits in the same subnet
(no firewall between them).

When I issue "telnet 172.20.1.92 25" followed by those helo ....
commands, it send the email out;  that's why I wanted a script
as sendemail doesn't work on my Win 2008 x64 servers (tried 2 servers)
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

sunhuxAuthor Commented:
> When I did it in my laptop, pointing to gmail SMTP server, it sends
> out within 2 secs

I used mxtoolbox.com to obtain gmail's smtp servers' IP for the above.
My laptop is running 64bit Windows 7 : tried sendemail on my
other laptop with Win XP 32 bit & it works too.

The above sendemail only works, if the To & From (ie -t and -f) are both
gmail addresses :

C:\sendemail> sendemail.exe -u "testg" -m "atest" -a "TODO.txt" -t "GPO <1stemail@gmail.com>" -f "GPO2 <2ndemail@gmail.com>" -s 173.194.77.26:25

Jul 14 20:07:20 my-pc sendemail.exe[7148]: Email was sent successfully!
0
Steve KnightIT ConsultancyCommented:
Have you tried either blat or using vbscript yet... I generally use the latter these days even if run from batch file. Can easily attach file or use it for body like you say.

http://scripts.dragon-it.co.uk/links/email-from-batch

The page of mine above gives some examples of blat and attachment if send mail.vbs. can elaborate when not on phone...
Steve
0
sunhuxAuthor Commented:
Quite a few years back, a monitoring colleague passed me blat:
at that time, both blat & sendemail works in that Windows 2003 server
but somehow I find sendemail friendlier.

2 clarifications:

Q1:
set smtpserver=smtpipaddress
Is double quotes needed to enclose the IP addr?
So if my smtp relay server is 172.23.3.85, the line is just
set smtpserver=172.23.3.85   ?

Q2:
%dir%blat d:\blat\body.txt -subject %subject% -to %toaddress% -hostname %hostname%
  Is hostname the current server which I'm issuing blat from?
  Is it mandatory to specify this parameter?
0
Steve KnightIT ConsultancyCommented:
No quotes are needed whether IP or DNS name for the server.  The hostname bit should be ok without, think it was just used for the "HELO" greeting for the smtp conversation.

You can soon do without all the generalising bloat too of course.  You need to run the install side once on a machine then the rest, i.e.

@echo off
if not exist "%temp%\blatinstalled.txt" (
  blat -install 10.10.10.10 from@domain.com
  echo %date% %time% > "%temp%\blatinstalled.txt"
)
blat d:\blat\body.txt -subject "My subject" -to "to@domain.com" -attach "c:\somefile.txt"
0
sunhuxAuthor Commented:
Think the script in the link you've given is a Windows batch
(.bat) script because if I tried to run it with
"cscript //nologo scriptname.vbs", it gave syntax error on line 1.

So I updated the script below with my SMTP server's IP, hostname,
to recipient (ie my email addr) & the sender email addr (which
is a whitelisted/verified working sender address : I tested it
working using the  'telnet ... helo ... steps  given above):
the  script sendblat.bat completed with a "Send complete"
message but I never received the email.

I've checked in the Spam/Junk mailboxes : not there too.
Something's amiss


@echo off
REM * Uses blat.exe and body.txt as body of message:
REM * Creates a log file in d:\ftp\blatlog.txt

set dir=D:\blat\
set logfile=%dir%blatlogfile.txt
set hostname=servername
set subject="Backup to BACKUP1 has failed"
set toaddress=server.problem@domainname.com
set receipt=NO
set attach=D:\lotus\scripts\lastlog.txt

if exist d:\blat\blatinstalled.txt goto endif
  set smtpserver=smtpipaddress
  set fromaddress=server.problem@domainname.com
  %dir%blat -install %smtpserver% %fromaddress%
  echo %date% %time% > d:\blat\blatinstalled.txt
:endif

if "%receipt%"=="YES" set extras=-r

echo Sending files....
echo %date% - %time% > %logfile%
%dir%blat d:\blat\body.txt -subject %subject% -to %toaddress% -hostname %hostname% -log %logfile% %extras% -attach %attach%
echo Send complete.
0
Steve KnightIT ConsultancyCommented:
Is that literally what you are running, are you running it from d:\blat with a body.txt file in there for the message body too for example, to be sure and re-run the install delete the blatlogfile.txt file to let it re-run or manually:

blat -install 10.10.10.10 from@domain.com
0
sunhuxAuthor Commented:
I'm running from c:\blat  & that txt file is created in c:\blat with all
the blat executables/dll in that c:\blat folder.

Same issue too if I run the blat.exe at command line without
any script : ie the emails never arrive but from that very same
server, issuing 'telnet ... helo ... data ..." to the same SMTP gateway,
the email arrives
0
sunhuxAuthor Commented:
& I'm always using the local administrator at the command
prompt all these while when testing
0
Steve KnightIT ConsultancyCommented:
Can you see from log on your SMTP server whether it gets a connection? Maybe it is rejecting it based on invalid from address or something?

I would expect an error from blat if it couldn't talk to the server so sounds like possibly accepted then blocked or ignored?

Have you tried vbs method, I use that for 99% these days
0
sunhuxAuthor Commented:
> tried vbs method
VB script?  Care to share a sample VB script that does the sequence of telnet ... helo ... DATA ... ?

Think there's an error in Blat logfile:

C:\blat\blat c:\blat\body.txt -subject "tst" -to myemail@xxx.com.au -hostname myserver -log c:\blat\bl.log -attach todo.txt -f myWhitelist_id@myWhitelistdomain.com -server 172.23.3.85:25
Blat v3.2.1 (build : May 28 2014 00:35:28)
32-bit Windows, Full, Unicode


C:\blat>type bl.log
2014.07.17 18:53:40 (Thu)------------Start of Session-----------------
Blat v3.2.1 (build : May 28 2014 00:35:28)
32-bit Windows, Full, Unicode
Sending c:\blat\body.txt to myemail@xxx.com.au
Subject: tst
Login name is myWhitelist_id@myWhitelistdomain.com
Attached binary file: todo.txt
Error: Wait a bit (possible timeout).
*** Error ***  Message not accepted by server
Error: Wait a bit (possible timeout).
2014.07.17 18:54:41 (Thu)-------------End of Session------------------

C:\blat>

On the SMTP relay server, I ran a continuous command below
& don't see any connection (nor any attempted connection)
from the source IP:
# netstat -ano 1 | grep -i "IP_of_Server_issuing_Blat"


In the sendmail relay server, there are lots of sdap.log.yyyymmdd
files : have a hard time tracing it
0
Steve KnightIT ConsultancyCommented:
The link I posted earlier has a VBScript example.  The commands it sends to SMTP is covered by the CDONTS method and you don't need to use the commands to SMTP directly, though telnetting to SMTP is very useful to know as test procedure!

i.e.


Const MailServer = "12.34.56.78" ' Mail Server to use for SMTP
Const MailServerPort = "25" ' SMTP Port used at Mail server (25 is default)
Const MailFrom = "logs@somedomain.com" 

if wscript.arguments.count <2 then
  wscript.echo "Please add subject and To address on the command line:"
  wscript.echo "cscript //nologo sendmail.vbs ""Subject line"" ""to@domain.com"""
else
  Subject=WScript.Arguments.Item(0)
  MailTo=WScript.Arguments.Item(1)
end if


' subject = taken from command line
' MailTo = taken from command line

body = "Please add the message to add in body here"

'Send mail to administrator
SendMail MailFrom , MailTo , WScript.Arguments.Item(0) , body, MailServer , MailServerPort 

Sub SendMail(Sender, Recipient, Subject, Message,Server,Port)
  Set objEmail = CreateObject("CDO.Message")
  objEmail.From = Sender
  objEmail.To = Recipient
  objEmail.Subject = Subject
  objEmail.Textbody = Message
  objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = Server
  objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = Port
  objEmail.Configuration.Fields.Update()
  objEmail.Send
End Sub

Open in new window


To attach a file you just use objEmail.AttachFile "c:\somedir\somefile.doc" or whatever.

Steve
0
sunhuxAuthor Commented:
Thanks Steve.

You mind inserting the exact objEmail.AttachFile line into the
VB script's codes as I'm rather handicapped with VB.

If I were to make an attempt, I would insert it between lines 31 & 32:

  objEmail.Configuration.Fields.Update()
  objEmail.AttachFile "c:\somedir\somefile.doc"
  objEmail.Send

I'll get to test it in 9 hours' time as I'm 12 hours apart from US time.
0
Steve KnightIT ConsultancyCommented:
Correct, literally that. and you can attach more as needed.

On phone so not copy/pasting all the code just add that one line.

Steve
0
sunhuxAuthor Commented:
Issued the command below but the test emails never arrives.

C:\blat> cscript //nologo sendmail.vbs "from sftp2 via smtp1" "sunhux@gmail.com"

From both our smtp1 & smtp2 Linux servers, I could use mutt or mailx commands
to send emails out & the sysadmin of the two SMTP linux servers told me a number
of our tenants/customers are currently sending emails out via these 2 SMTP servers
(ie confirming they are relay servers).  I must have missed something
0
sunhuxAuthor Commented:
Just saw an email from one of our customer's IT support that used
our SMTP relay server who had been struggling to send email out
& our network/firewall team had verified there's no firewall blocks
/traffic drops.

The customer finally got it working & shared with us that it was solved
by tweaking something in his applications as follow.  Is there anything
similar we can tweak in blat or the VB script?

Mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
Mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "hsvsmtp.mgmt.auscloud.gov.au"
Mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
Mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") =0
Mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
0
sunhuxAuthor Commented:
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") =0
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

I added in the 2  lines above just before the 'update' & when I run it on my
laptop (using the previously tested working SMTP gateway of gmail), got the
error below:

D:\share\sendmail.vbs(34, 3) (null): The message could not be sent to the SMTP s
erver. The transport error code was 0x800ccc69. The server response was 550 5.7.
1 more information. fr10si19817887oeb.18 - gsmtp
0
Steve KnightIT ConsultancyCommented:
Did you get anywhere with this, have been away.

Steve
0
skullnobrainsCommented:
Jul 14 19:45:46 MyServer SendEmail[3416]: ERROR => Received:   421 4.7.0 mail
relay2.a-cloud.gov.au closing connection

When I did it in my laptop, pointing to gmail SMTP server, it sends
out within 2 secs.

you're being greylisted by the server. there is nothing you can do about that except pick another one or implement some kind of retry

----

as far as gmail goes you'd need to be authenticated if you expect to send a mail through their smtp to a non gmail user

----

i'd suggest you stick with your executable but find a server that you are allowed to send mail to. usually, your ISP's is a good choice, or actually do authenticated smtp. but then it won't make much of a difference with blat or a vb script.

---

telnet is a bad choice to script with because some implementations will close the connection when they reach EOF on input, and others will hang forever after sending the mail... anyway reading and writing from the same executable in order to send commands at the proper time from a batch script is a pita in a windows batch if at all feasible before your brains dribble out of your eyes.

on linux if you want to script it, use netcat or hose (from netpipes). there is little use if you just want to send alert mails, but i can provide working scripts if you want

----

for the attachments in linux, you can rather easily write a script that handles attachments by creating a boundary and proper headers, but i'd suggest using biabam which is a bash script that does that nicely and should be available in your favorite distribution
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sunhuxAuthor Commented:
Skullnobrains is right: I need to whitelist the servers using
a sendmail interface (via browser) to whitelist them but
this a rather tedious process: I have to key in each IP
address one by one & I can't specify an entire subnet
or network (eg: 172.22.0.0).

As I have 600+ servers or IPs to key in, does Skullnobrains
have any solution to do this sendmail whitelisting faster?

> as far as gmail goes you'd need to be authenticated if you expect
> to send a mail through their smtp to a non gmail user
Yes, Skullnobrains is right again on the above
0
skullnobrainsCommented:
As I have 600+ servers or IPs to key in, does Skullnobrains
have any solution to do this sendmail whitelisting faster?

i'd suggest you either use a "friendly" smtp server, or configure one of your boxes as a mail server. this is rather trivial on linux and will handle failover natively. just make sure the external ip it uses has a proper PTR and the PTR maps back to the IP, and things should go smoothly

it is most likely feasible to automate the whitelisting process but this is most likely forbidden and most likely something the remote server will notice. if the remote server is yours you should be able to whitelist whatever you want
0
skullnobrainsCommented:
As I have 600+ servers or IPs to key in, does Skullnobrains
have any solution to do this sendmail whitelisting faster?

i'd suggest you either use a "friendly" smtp server, or configure one of your boxes as a mail server. this is rather trivial on linux and will handle failover natively. just make sure the external ip it uses has a proper PTR and the PTR maps back to the IP, and things should go smoothly

it is most likely feasible to automate the whitelisting process but this is most likely forbidden and most likely something the remote server will notice. if the remote server is yours you should be able to whitelist whatever you want
0
sunhuxAuthor Commented:
It's a controlled production environment & I can't install an
SMTP at liberty.  I'll still need to use the Linux sendmail SMTP
server.  

Is there any script or ways to whitelist the entire subnet?
Otherwise, I'll just key in one by one
0
skullnobrainsCommented:
99% of all any linux boxes are built in with an smtp server. the sendmail command is most likely a part of postfix in your case.

i still think you should use an smtp server you can control, and i hardly believe your env does not contain one, but if you're asking a way to whitelist a subnet on a specific external server, i'd need to know which one before i can answer. "a sendmail interface via browser" does not ring a bell so it's difficult to understand what you are struggling with.
0
gheistCommented:
telnet will not wait for server return code, and *ALL* modern mailservers add .1s delay to banner and your telnet script chokes just like any spam.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.