通过 Outlook 从命令行发送电子邮件而无需单击发送 [英] Sending email from Command-line via outlook without having to click send
问题描述
我需要通过命令行发送电子邮件,无需任何人工交互以实现自动化.
I need to send email via command-line without any human interactions for automation.
我知道我们可以使用 mailto 命令,但这会撰写电子邮件、主题、正文和所有内容,但除非我点击发送,否则它不会发送.
I know we can use mailto command but that would compose email, subject,body and everything but it wouldn't send it unless I click send.
我在网上看到我们可以使用 blat,但我不能使用 Outlook 以外的任何东西.
I read online we can use blat but I cannot use anything other than outlook.
这是我发现的封闭帖子 SOF 帖子链接.
This is closed post I have found Link to SOF post.
仅供参考:我正在研究一些发送电子邮件的 telnet 命令,但也没有成功.telnet 命令发送电子邮件>
just for your information: I am looking into some telnet commands to send email haven't gotten success in that yet either. telnet commands to send email
推荐答案
选项 1
你没有说太多关于你的环境,但假设你有它,你可以使用 PowerShell 脚本;一个例子是这里.其本质是:
$smtp = New-Object Net.Mail.SmtpClient("ho-ex2010-caht1.exchangeserverpro.net")
$smtp.Send("reports@exchangeserverpro.net","administrator@exchangeserverpro.net","Test Email","This is a test")
然后您可以按照 this 从命令行启动脚本例子:
powershell.exe -noexit c:scripts est.ps1
请注意,Windows 7 和 Windows Server 2008R2 上默认安装的 PowerShell 2.0 包括一个更简单的 Send-MailMessage
命令,让事情变得更简单.
Note that PowerShell 2.0, which is installed by default on Windows 7 and Windows Server 2008R2, includes a simpler Send-MailMessage
command, making things easier.
选项 2
如果您准备使用第三方软件,一个选项类似于 这个 SendEmail 命令-线工具.不过,这取决于您的目标环境;如果您要将批处理文件部署到多台机器,显然每次都需要包含(但不是正式安装).
Option 2
If you're prepared to use third-party software, an option is something like this SendEmail command-line tool. It depends on your target environment, though; if you're deploying your batch file to multiple machines, that will obviously require inclusion (but not formal installation) each time.
选项 3
您可以直接从 VBA 脚本驱动 Outlook,而后者又可以从批处理文件中触发;这将允许您使用 Outlook 本身发送电子邮件,它看起来最接近您想要的内容.这有两个部分;首先,找出发送电子邮件所需的 VBA 脚本.网上有很多这方面的示例,包括来自 Microsoft 此处.其本质是:
Sub SendMessage(DisplayMsg As Boolean, Optional AttachmentPath)
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
Set objOutlookRecip = .Recipients.Add("Nancy Davolio")
objOutlookRecip.Type = olTo
' Set the Subject, Body, and Importance of the message.
.Subject = "This is an Automation test with Microsoft Outlook"
.Body = "This is the body of the message." &vbCrLf & vbCrLf
.Importance = olImportanceHigh 'High importance
If Not IsMissing(AttachmentPath) Then
Set objOutlookAttach = .Attachments.Add(AttachmentPath)
End If
For Each ObjOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next
.Save
.Send
End With
Set objOutlook = Nothing
End Sub
然后,按照这个答案/autorun 参数从命令行启动 Outlooka>(根据需要更改路径/宏名称):
Then, launch Outlook from the command line with the /autorun
parameter, as per this answer (alter path/macroname as necessary):
C:Program FilesMicrosoft OfficeOffice11Outlook.exe" /autorun macroname
选项 4
您可以使用与选项 3 相同的方法,但将 Outlook VBA 移动到 PowerShell 脚本(您将从命令行运行).示例 此处一>.这可能是最整洁的解决方案,IMO.
Option 4
You could use the same approach as option 3, but move the Outlook VBA into a PowerShell script (which you would run from a command line). Example here. This is probably the tidiest solution, IMO.
这篇关于通过 Outlook 从命令行发送电子邮件而无需单击发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!