代码之家  ›  专栏  ›  技术社区  ›  Jörgen Lundberg

从Windows脚本发送邮件

  •  10
  • Jörgen Lundberg  · 技术社区  · 18 年前

    我想从WindowsServer2003标准版上的脚本发送邮件。我认为服务器的设置基本上是开箱即用的。

    邮件服务器是一个交换服务器,当您在内部网络上时,可以使用普通的旧SMTP。我用Perl从我的机器上完成了这项工作,但不幸的是,Perl在服务器上不可用。

    有没有一种简单的方法可以从.bat文件或任何其他不需要安装一些附加软件的方法来实现这一点?

    Edit:
    谢谢你的快速回复。“blat”这个东西可能工作得很好,但是使用wscript,我不需要使用单独的二进制文件。

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

    只需将脚本保存到一个文件中,比如sendmail.vbs,然后从命令提示符调用它,如下所示:
    wscript sendmail.vbs

    7 回复  |  直到 14 年前
        1
  •  10
  •   PhiLho    18 年前

    可以使用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
    

    更新:在那里找到更多信息: VBScript To Send Email Using CDO 默认情况下,它似乎使用了Outlook[Express],所以它在我的计算机上不起作用,但您可以使用一个给定的SMTP服务器,这对我来说很好。

        2
  •  6
  •   Jacek Szymański    18 年前

    我不知道删除.bat文件旁边的二进制文件是否算是安装软件,但如果不是,您可以使用 blat 这样做。

        3
  •  4
  •   Nathan Hartley    14 年前

    如果服务器安装了PowerShell v2(我知道这个问题有多旧),则Cmdlet发送邮件消息将在一行中执行此操作。

    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
  •   Re0sless    18 年前

    如果安装了Outlook/Exchange,则应该可以使用cdonts,只需创建mail.vbs文件并在类似的批处理文件中调用它(有趣的是,它们位于同一目录中)。

    wscript mail.vbs
    

    对于vbscript代码,请签出

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

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

    忘记这两个链接所说的ASP,它应该可以作为一个独立的脚本在不使用IIS的情况下正常工作。

        5
  •  0
  •   Moshe    18 年前

    我认为您必须安装一些可以从wscript调用的ActiveX或其他组件,例如: http://www.activexperts.com/ActivEmail/ 还有: http://www.emailarchitect.net/webapp/SMTPCOM/developers/scripting.asp

    否则,您将不得不独自在wscript中编写整个SMTP逻辑(如果可能,不确定)。

        6
  •  0
  •   vacpro    18 年前

    将CDONTS与Windows脚本宿主(wscript)一起使用

        7
  •  0
  •   DssTrainer    15 年前

    是否有不引用外部架构URL的发送方式? http://schemas.microsoft.com/cdo/configuration/

    这是非常无用的,因为不能假定所有的盒子都有外部互联网接入,可以在本地交换台内部发送邮件。有没有办法在本地保存这些URL的信息?