从Windows脚本发送邮件。

时间:2021-08-07 18:13:44

I would like to send mail from a script on a Windows Server 2003 Standard Edition. I think the server setup is pretty much out of the box.

我想从Windows Server 2003标准版的脚本中发送邮件。我认为服务器的设置基本上是现成的。

The mail server is an Exchange one, and when you're on the internal network you can use plain old SMTP. I have done it from my machine with Perl, but unfortunately Perl is not available on the server.

邮件服务器是一个交换器,当你在内部网络上时,你可以使用简单的旧的SMTP。我已经用Perl完成了它,但不幸的是,Perl在服务器上是不可用的。

Is there an easy way of doing this from a .bat-file or any other way that doesn't require installing some additional software?

是否有一种简单的方法可以从.bat文件或其他不需要安装额外软件的方式来实现这一点?

Edit:
Thanks for the quick replies. The "blat" thingie would probably work fine but with wscript I don't have to use a separate binary.

编辑:谢谢你的快速回复。“blat”的东西可能会很好,但是使用wscript,我不需要使用单独的二进制文件。

I didn't see PhiLho's post the first time I edited and selected an answer. No need for me to duplicate the code here.

我第一次编辑并选择一个答案时,没有看到PhiLho的帖子。我不需要复制这里的代码。

Just save the script to a file, say sendmail.vbs, and then call it from the command prompt like so:
wscript sendmail.vbs

将脚本保存到一个文件中,sendmail说。vbs,然后从命令提示符调用它:wscript sendmail.vbs。

7 个解决方案

#1


9  

It is possible with Wscript, using CDO:

使用Wscript,可以使用CDO:

Dim objMail

Set objMail = CreateObject("CDO.Message")

objMail.From = "Me <Me@Server.com>"
objMail.To = "You <You@AnotherServer.com>"
objMail.Subject = "That's a mail"
objMail.Textbody = "Hello World"
objMail.AddAttachment "C:\someFile.ext"

---8<----- You don't need this part if you have an active Outlook [Express] account -----
' Use an SMTP server
objMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

' Name or IP of Remote SMTP Server
objMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
    "smtp.server.com"

' Server port (typically 25)
objMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

objMail.Configuration.Fields.Update
----- End of SMTP usage ----->8---

objMail.Send

Set objMail=Nothing
Wscript.Quit

Update: found more info there: VBScript To Send Email Using CDO By default it seems it uses Outlook [Express], so it didn't worked on my computer but you can use a given SMTP server, which worked fine for me.

更新:发现更多信息:VBScript默认使用CDO发送电子邮件,它似乎使用Outlook [Express],所以它没有在我的电脑上运行,但你可以使用一个给定的SMTP服务器,这对我来说很正常。

#2


6  

I don't know if dropping a binary alongside the .bat file counts as installing software, but, if not, you can use blat to do this.

我不知道是否将一个二进制文件和.bat文件放在一起作为安装软件,但是,如果没有,您可以使用blat来完成这个任务。

#3


4  

If the server happened (I realize how old this question is) to have Powershell v2 installed, the CmdLet Send-MailMessage would do this in one line.

如果服务器发生了(我意识到这个问题有多老),要安装Powershell v2,那么CmdLet Send-MailMessage就会在一行中执行这个操作。

Send-MailMessage [-To] <string[]> [-Subject] <string> -From <string> [[-Body] <string>] [[-SmtpServer] <string>] [-Attachments <string[]>] [-Bcc <string[]>] [-BodyAsHtml] [-Cc <string[]>] [-Credential <PSCredential>] [-DeliveryNotficationOption {None | OnSuccess | OnFailure | Delay | Never}] [-Encoding <Encoding>] [-Priority {Normal | Low | High}] [-UseSsl] [<CommonParameters>]

#4


1  

If you have outlook/exchange installed you should be able to use CDONTs, just create a mail.vbs file and call it in a batch file like so (amusing they are in the same dir)

如果你已经安装了outlook/exchange,你应该可以使用CDONTs,只需创建一个邮件。vbs文件,并在批处理文件中调用它(有趣的是它们在同一个目录中)

wscript mail.vbs

for the VBScript code check out

对于VBScript代码检查。

http://support.microsoft.com/kb/197920

http://support.microsoft.com/kb/197920

http://www.w3schools.com/asp/asp_send_email.asp

http://www.w3schools.com/asp/asp_send_email.asp

forget the fact they the two links speak about ASP, it should work fine as a stand alone script with out iis.

忘记了这两个链接关于ASP的事实,它应该作为一个独立的脚本和iis一起工作。

#5


0  

I think that you'll have to install some ActiveX or other component what could be invoked from WScript, such as: http://www.activexperts.com/ActivEmail/ and: http://www.emailarchitect.net/webapp/SMTPCOM/developers/scripting.asp

我认为您必须安装一些ActiveX或其他组件,这些组件可以从WScript中调用,例如:http://www.activexperts.com/ActivEmail/和:http://www.emailarchitect.net/webapp/smtpcom/developers/script.asp。

Otherwise, you'll have to write the entire SMTP logic (if possible, not sure) in WScript all on your own.

否则,您将不得不在自己的脚本中编写整个SMTP逻辑(如果可能的话,不确定)。

#6


0  

Use CDONTS with Windows Scripting Host (WScript)

使用CDONTS与Windows脚本主机(WScript)

#7


0  

Is there a way you send without referencing the outside schema urls. http://schemas.microsoft.com/cdo/configuration/

是否有一种方法可以在不引用外部模式url的情况下发送。http://schemas.microsoft.com/cdo/configuration/

That is highly useless as it can't be assumed all boxes will have outside internet access to send mail internally on the local exchange. Is there a way to save the info from those urls locally?

这是非常无用的,因为不能假定所有的盒子都可以在互联网上通过本地交换发送邮件。是否有一种方法可以在本地保存这些url的信息?

#1


9  

It is possible with Wscript, using CDO:

使用Wscript,可以使用CDO:

Dim objMail

Set objMail = CreateObject("CDO.Message")

objMail.From = "Me <Me@Server.com>"
objMail.To = "You <You@AnotherServer.com>"
objMail.Subject = "That's a mail"
objMail.Textbody = "Hello World"
objMail.AddAttachment "C:\someFile.ext"

---8<----- You don't need this part if you have an active Outlook [Express] account -----
' Use an SMTP server
objMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

' Name or IP of Remote SMTP Server
objMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
    "smtp.server.com"

' Server port (typically 25)
objMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

objMail.Configuration.Fields.Update
----- End of SMTP usage ----->8---

objMail.Send

Set objMail=Nothing
Wscript.Quit

Update: found more info there: VBScript To Send Email Using CDO By default it seems it uses Outlook [Express], so it didn't worked on my computer but you can use a given SMTP server, which worked fine for me.

更新:发现更多信息:VBScript默认使用CDO发送电子邮件,它似乎使用Outlook [Express],所以它没有在我的电脑上运行,但你可以使用一个给定的SMTP服务器,这对我来说很正常。

#2


6  

I don't know if dropping a binary alongside the .bat file counts as installing software, but, if not, you can use blat to do this.

我不知道是否将一个二进制文件和.bat文件放在一起作为安装软件,但是,如果没有,您可以使用blat来完成这个任务。

#3


4  

If the server happened (I realize how old this question is) to have Powershell v2 installed, the CmdLet Send-MailMessage would do this in one line.

如果服务器发生了(我意识到这个问题有多老),要安装Powershell v2,那么CmdLet Send-MailMessage就会在一行中执行这个操作。

Send-MailMessage [-To] <string[]> [-Subject] <string> -From <string> [[-Body] <string>] [[-SmtpServer] <string>] [-Attachments <string[]>] [-Bcc <string[]>] [-BodyAsHtml] [-Cc <string[]>] [-Credential <PSCredential>] [-DeliveryNotficationOption {None | OnSuccess | OnFailure | Delay | Never}] [-Encoding <Encoding>] [-Priority {Normal | Low | High}] [-UseSsl] [<CommonParameters>]

#4


1  

If you have outlook/exchange installed you should be able to use CDONTs, just create a mail.vbs file and call it in a batch file like so (amusing they are in the same dir)

如果你已经安装了outlook/exchange,你应该可以使用CDONTs,只需创建一个邮件。vbs文件,并在批处理文件中调用它(有趣的是它们在同一个目录中)

wscript mail.vbs

for the VBScript code check out

对于VBScript代码检查。

http://support.microsoft.com/kb/197920

http://support.microsoft.com/kb/197920

http://www.w3schools.com/asp/asp_send_email.asp

http://www.w3schools.com/asp/asp_send_email.asp

forget the fact they the two links speak about ASP, it should work fine as a stand alone script with out iis.

忘记了这两个链接关于ASP的事实,它应该作为一个独立的脚本和iis一起工作。

#5


0  

I think that you'll have to install some ActiveX or other component what could be invoked from WScript, such as: http://www.activexperts.com/ActivEmail/ and: http://www.emailarchitect.net/webapp/SMTPCOM/developers/scripting.asp

我认为您必须安装一些ActiveX或其他组件,这些组件可以从WScript中调用,例如:http://www.activexperts.com/ActivEmail/和:http://www.emailarchitect.net/webapp/smtpcom/developers/script.asp。

Otherwise, you'll have to write the entire SMTP logic (if possible, not sure) in WScript all on your own.

否则,您将不得不在自己的脚本中编写整个SMTP逻辑(如果可能的话,不确定)。

#6


0  

Use CDONTS with Windows Scripting Host (WScript)

使用CDONTS与Windows脚本主机(WScript)

#7


0  

Is there a way you send without referencing the outside schema urls. http://schemas.microsoft.com/cdo/configuration/

是否有一种方法可以在不引用外部模式url的情况下发送。http://schemas.microsoft.com/cdo/configuration/

That is highly useless as it can't be assumed all boxes will have outside internet access to send mail internally on the local exchange. Is there a way to save the info from those urls locally?

这是非常无用的,因为不能假定所有的盒子都可以在互联网上通过本地交换发送邮件。是否有一种方法可以在本地保存这些url的信息?