2008-09-30 133 views
9

我想從Windows Server 2003標準版上的腳本發送郵件。我認爲服務器設置幾乎是開箱即用的。從Windows腳本發送郵件

郵件服務器是Exchange的郵件服務器,當您在內部網絡上時,您可以使用普通的舊SMTP。我從我的機器上用Perl完成了它,但不幸的是,Perl在服務器上不可用。

有沒有一種簡單的方法可以通過.bat文件或其他任何不需要安裝某些附加軟件的方式來完成此操作?

Edit:
感謝您的快速回復。 「喋喋不休」的東西可能會正常工作,但與wscript我不必使用一個單獨的二進制文件。

我第一次編輯和選擇答案時沒有看到PhiLho的帖子。我不需要在這裏複製代碼。

腳本只是保存到一個文件,說sendmail.vbs,然後在命令提示符下調用它像這樣:
wscript sendmail.vbs

回答

9

有可能與WScript的,使用CDO:

Dim objMail 

Set objMail = CreateObject("CDO.Message") 

objMail.From = "Me <[email protected]>" 
objMail.To = "You <[email protected]>" 
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服務器,這對我來說工作得很好。

+0

收到'服務器拒絕發件人地址...必須首先發出STARTTLS命令。' – 2014-06-30 20:51:13

6

我不知道是否將與.bat文件一起放置的二進制文件視爲安裝軟件,但如果不是,則可以使用blat來執行此操作。

1

如果你已經安裝了Outlook/Exchange,您應該能夠使用CDONTS,只需創建一個mail.vbs文件,像這樣把它在一個批處理文件(有趣的是,他們在同一目錄)

wscript mail.vbs 

的VBScript代碼檢查

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

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

忘記一個事實,他們的2升油墨談論ASP,它應該作爲一個獨立的腳本與iis一起工作。

+0

CDONTS已棄用,由CDOSYS替代。 http://support.microsoft.com/default.aspx/kb/810702 – 2009-03-19 13:34:22

0

使用CDONTS與Windows腳本宿主(WScript的)

0

有沒有一種方法可以在不引用外部架構URL的情況下發送。 http://schemas.microsoft.com/cdo/configuration/

這是非常無用的,因爲它不能被認爲是所有的盒子都有外部互聯網訪問在本地交換機內部發送郵件。有沒有辦法在本地保存這些網址的信息?

4

如果服務器發生了(我意識到這個問題有多大),安裝了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>]