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

前端 未结 6 1432
醉酒成梦
醉酒成梦 2020-12-07 21:35

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 s

相关标签:
6条回答
  • 2020-12-07 21:51
    $emailSmtpServerPort = "587"
    $emailSmtpUser = "username"
    $emailSmtpPass = 'password'
    $emailMessage = New-Object System.Net.Mail.MailMessage
    $emailMessage.From = "[From email address]"
    $emailMessage.To.Add( "[Send to email address]" )
    $emailMessage.Subject = "Testing e-mail"
    $emailMessage.IsBodyHtml = $true
    $emailMessage.Body = @"
    <p>Here is a message that is <strong>HTML formatted</strong>.</p>
    <p>From your friendly neighborhood IT guy</p>
    "@
    $SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
    $SMTPClient.Send( $emailMessage )
    
    0 讨论(0)
  • 2020-12-07 21:57

    I've used Blat ( http://www.blat.net/ ) for many years. It's a simple command line utility that can send email from command line. It's free and opensource.

    You can use command like "Blat myfile.txt -to fee@fi.com -server smtp.domain.com -port 6000"

    Here is some other software you can try to send email from command line (I've never used them):
    http://caspian.dotconf.net/menu/Software/SendEmail/
    http://www.petri.co.il/sendmail.htm
    http://www.petri.co.il/software/mailsend105.zip
    http://retired.beyondlogic.org/solutions/cmdlinemail/cmdlinemail.htm

    Here ( http://www.petri.co.il/send_mail_from_script.htm ) you can find other various way of sending email from a VBS script, plus link to some of the mentioned software

    The following VBScript code is taken from that page

    Set objEmail = CreateObject("CDO.Message")
    objEmail.From = "me@mydomain.com"
    objEmail.To = "you@yourdomain.com"
    objEmail.Subject = "Server is down!"
    objEmail.Textbody = "Server100 is no longer accessible over the network."
    objEmail.Send
    

    Save the file as something.vbs

    Set Msg = CreateObject("CDO.Message")
    
    With Msg
    
     .To = "you@yourdomain.com"
     .From = "me@mydomain.com"
     .Subject = "Hello"
     .TextBody = "Just wanted to say hi."
     .Send
    
    End With
    

    Save the file as something2.vbs

    I think these VBS scripts use the windows default mail server, if present. I've not tested these scripts...

    0 讨论(0)
  • 2020-12-07 22:00

    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 = "your@address.com"
    objMail.To = "destination@address.com"
    objMail.Subject = "Email Subject Text"
    objMail.TextBody = "The message of the email..."
    objMail.Send
    Set objFlds = Nothing
    Set objConf = Nothing
    Set objMail = Nothing
    
    0 讨论(0)
  • 2020-12-07 22:01

    If you can't follow Max's suggestion of installing Blat (or any other utility) on your server, then perhaps your server already has software installed that can send emails.

    I know that both Oracle and SqlServer have the capability to send email. You might have to work with your DBA to get that feature enabled and/or get the privilege to use it. Of course I can see how that might present its own set of problems and red tape. Assuming you can access the feature, it is fairly simple to have a batch file login to a database and send mail.

    A batch file can easily run a VBScript via CSCRIPT. A quick google search finds many links showing how to send email with VBScript. The first one I happened to look at was http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/enterprise/mail/. It looks straight forward.

    0 讨论(0)
  • 2020-12-07 22:03

    It works for me, by using double quotes around variables.

    I am using batch script to call powershell Send-MailMessage

    Batch Script:send_email.bat

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  -command 'E:\path\send_email.ps1
    

    Pwershell Script send_email.ps1

    Send-MailMessage -From "noreply@$env:computername" -To '<target_email@example.com>' -Subject 'Blah Blah' -SmtpServer  'smtp.domain.com'  -Attachments 'E:\path\file.log' -BODY "Blah Blah on Host: $env:computername "
    
    0 讨论(0)
  • 2020-12-07 22:04

    If PowerShell is available, the Send-MailMessage commandlet is a single one-line command that could easily be called from a batch file to handle email notifications. Below is a sample of the line you would include in your batch file to call the PowerShell script (the %xVariable% is a variable you might want to pass from your batch file to the PowerShell script):

    --[BATCH FILE]--

    :: ...your code here...
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  -windowstyle hidden -command C:\MyScripts\EmailScript.ps1 %xVariable%
    

    Below is an example of what you might include in your PowerShell script (you must include the PARAM line as the first non-remark line in your script if you included passing the %xVariable% from your batch file:

    --[POWERSHELL SCRIPT]--

    Param([String]$xVariable)
    # ...your code here...
    $smtp = "smtp.[emaildomain].com"
    $to = "[Send to email address]"
    $from = "[From email address]" 
    $subject = "[Subject]" 
    $body = "[Text you want to include----the <br> is a line feed: <br> <br>]"    
    $body += "[This could be a second line of text]" + "<br> "
    
    $attachment="[file name if you would like to include an attachment]"
    send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Attachment $attachment -Priority high  
    
    0 讨论(0)
提交回复
热议问题