Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a simple email from a Windows batch file?

I'm running Windows 2003 Service Pack 2. I have a batch file that runs on demand. I want to have an email sent every time the batch file runs. The email is simple, just a sentence indicating that the batch file ran; it is the same every time.

I've tried a couple of things to get this done. I thought of telnet, but I can't figure out how to redirect a set of commands into telnet; Windows batch files don't have a Unix-style "here document," and calling "telnet <scriptfile" where scriptfile contains the commands to send an email didn't work. I also found a couple of solutions on the internet using CDO.Message, but I've never used that before and I kept getting error messages that I don't understand.

How can I send a simple email from a Windows batch file?

like image 825
user448810 Avatar asked Jan 27 '12 19:01

user448810


People also ask

Can you send an email from a batch file?

To send an email using a batch file and PowerShell, we first need to create the PowerShell script to send an email. Once the script is built and saved, we can then call the PowerShell script from the batch file, a process is very similar to the VBscript method.

Can you send an email from CMD?

In Windows there is no way to natively send mail from the Command Prompt, but because PowerShell allows you to use the underlying . Net Framework, you can easily create and send an e-mail from the command line.

How do I send a batch file through Outlook?

Save the attachment to the cloud, a server, or an FTP site that you can access. This can include OneDrive or a secure network share server such as SharePoint. You can send a link to the attachment on the server or FTP site so that the users can click the link to access the file and save it to the computer.


1 Answers

Max is on he right track with the suggestion to use Windows Scripting for a way to do it without installing any additional executables on the machine. His code will work if you have the IIS SMTP service setup to forward outbound email using the "smart host" setting, or the machine also happens to be running Microsoft Exchange. Otherwise if this is not configured, you will find your emails just piling up in the message queue folder (\inetpub\mailroot\queue). So, unless you can configure this service, you also want to be able to specify the email server you want to use to send the message with. To do that, you can do something like this in your windows script file:

Set objMail = CreateObject("CDO.Message") Set objConf = CreateObject("CDO.Configuration") Set objFlds = objConf.Fields objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'cdoSendUsingPort objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.your-site-url.com" 'your smtp server domain or IP address goes here objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 'default port for email 'uncomment next three lines if you need to use SMTP Authorization 'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your-username" 'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your-password" 'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'cdoBasic objFlds.Update objMail.Configuration = objConf objMail.FromName = "Your Name" objMail.From = "[email protected]" objMail.To = "[email protected]" objMail.Subject = "Email Subject Text" objMail.TextBody = "The message of the email..." objMail.Send Set objFlds = Nothing Set objConf = Nothing Set objMail = Nothing 
like image 85
dmarietta Avatar answered Sep 19 '22 16:09

dmarietta