开发者

How do I send an email from a DOS batch command?

开发者 https://www.devze.com 2023-01-08 14:40 出处:网络
I have a batch file in DOS that does some checking and I need to fire off an email when its d开发者_高级运维one. I\'ve found a few solutions on the interwebz but most of them are 3rd party or just sim

I have a batch file in DOS that does some checking and I need to fire off an email when its d开发者_高级运维one. I've found a few solutions on the interwebz but most of them are 3rd party or just simply open up a new message in Outlook. I need the command to send an email in its entirety without any human interaction.

We use MS Exchange here, if that matters.

Thanks!


Assuming:

  1. Your Exchange server accepts emails via SMTP on port 25.
  2. You want to send a simple text only email without attachments.
  3. It's possible to drive telnet (or a similar telnet client) from a batch file.

You could just send a simple email via Telnet. This link shows an example of how to do it: http://www.yuki-onna.co.uk/email/smtp.html

If assumption 2 or 3 is wrong, you could write a command line SMTP client for sending simples emails fairly easily in many languages and then call it from your batch file.


Try

http://sourceforge.net/projects/blat/

it's an open source mailer from SourceForge with a lot of options.


Try http://caspian.dotconf.net/menu/Software/SendEmail/.

It's a third-party utility, but you can easily call it from a batch file. I do. I have the same need that you do: send e-mail at the end of a batch file with no human interaction.


If your host OS has PowerShell, you can accomplish this from CMD.EXE as a one-liner (effectively) without also incurring script-execution privilege blocking by simply doing:

powershell -command $S=New-Object Net.Mail.SmtpClient('your.smtp.server',port);$S.Send('addr-from','addr-to','subject','body')

Concatenating the lines with ; and executing as a single PS command dodges checking/setting script execution. There is, of course, a limitation to the length of a single command string in CMD.EXE so this exact technique puts some limitations on the number of recipients, size of subject and body, etc. but there are ways around this, covered elsewhere, I'm sure.

Naturally, my usage of that is much more robust, being wrapped in a .BAT that turns it into a utility (perfect for low-friction scripts on locked-down Windows hosts) with parameter checking, error handling, logging, etc. Go nuts.

If your SMTP server requires authentication, you can add the additional necessary configuration of the SmtpClient as needed, which will of course further constrain length.

EDIT: Insert this before the $S.Send to add authentication:

$S.EnableSsl=$true;$S.Credentials=New-Object System.Net.NetworkCredential('usr', 'pass');


powershell -command $S=New-Object Net.Mail.SmtpClient('your.smtp.server',port);$S.Send('addr-from','addr-to','subject','body')`

this is working for me

but I have to send an email with attachment to multiple recepients

0

精彩评论

暂无评论...
验证码 换一张
取 消