batch script to send email

纵饮孤独 提交于 2019-12-06 00:28:52

问题


How to write a Windows batch script to send a mail? Give an example


回答1:


You will need to use a third party tool such as blat (http://www.blat.net/). Then in your batch file, you would have a line like the following:

blat -to foo@bar.com -f bar@foo.com -subject "Email Subject" -body "Email body" -server mysmtphost




回答2:


You can enable the installed SMTP Server of Windows. And then run a power shell script:

$subject = $args[0]

# Create from/to addresses
$from = New-Object system.net.mail.MailAddress "remy@supertext.ch"
$to = New-Object system.net.mail.MailAddress "remy.blaettler@gmail.com"

# Create Message
$message = new-object system.net.mail.MailMessage $from, $to
$message.Subject = $subject
$message.Body = @"
Warning message from the Supertext Server
"@

# Set SMTP Server and create SMTP Client
$server = "209.162.190.6"
$client = new-object system.net.mail.smtpclient $server

# SO do it
"Sending an e-mail message to {0} by using SMTP host {1} port {2}." -f $to.ToString(), $client.Host, $client.Port
try {
$client.Send($message)
}
catch {
"Exception caught in CreateTestMessage: {0}" -f $Error.ToString()
}



回答3:


I would suggest downloading and installing a command line emailing program. The best freeware one that I can see is:

http://www.beyondlogic.org/solutions/cmdlinemail/cmdlinemail.htm

From there, save the downloaded .exe to a good location on your HDD (possibly in c:/program files/bmail) and add that directory to your PATH ( see http://www.brightrev.com/how-to/windows/35-add-a-utilities-directory-to-your-pc.html?start=1 for instructions ).

From then, you could setup a small batch script such as this:

@echo off
bmail -s smtp.example.com -p 465 -t toemail@example.com -f fromemail@example.com -h -a "INSERT SUBJECT HERE" -b "INSERT MESSAGE TEXT HERE"

Then to send an email to that email address, just run the batch file.

If you want, you could change it to accept parameters so you can customise the message: http://www.robvanderwoude.com/parameters.php .

Important: I'm pretty sure bmail won't work if the SMTP server requires authentication, so you'll need to find one you can use. Maybe set one up on the server without a username and password, and that can only be accessed from localhost?



来源:https://stackoverflow.com/questions/4732919/batch-script-to-send-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!