通过outlook从命令行发送电子邮件,而无需单击"发送"

Mow*_*gli 17 outlook command-line automation

我需要通过命令行发送电子邮件,而无需任何人工交互以实现自动化.

我知道我们可以使用mailto命令,但这会组成电子邮件,主题,正文和所有内容,但除非我点击发送,否则它不会发送它.

我在线阅读我们可以使用blat但我不能使用除outlook之外的任何东西.

这是关闭后我发现链接到SOF帖子.

只是为了您的信息:我正在研究一些telnet命令发送电子邮件还没有成功. telnet用于发送电子邮件的命令

Geo*_*off 22

选项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")
Run Code Online (Sandbox Code Playgroud)

然后,您可以按照以下示例从命令行启动脚本:

powershell.exe -noexit c:\scripts\test.ps1
Run Code Online (Sandbox Code Playgroud)

请注意,默认安装在Windows 7和Windows Server 2008R2上的PowerShell 2.0包含一个更简单的Send-MailMessage命令,使事情变得更容易.

选项2
如果您准备使用第三方软件,则可以使用此SendEmail命令行工具.但这取决于您的目标环境; 如果您要将批处理文件部署到多台计算机上,那么每次都需要包含(但不是正式安装).

选项3
您可以直接从VBA脚本驱动Outlook,而VBA脚本又可以从批处理文件触发; 这将允许您使用Outlook本身发送电子邮件,这看起来最接近您想要的.这有两个部分; 首先,弄清楚发送电子邮件所需的VBA脚本.这里有很多这样的例子,包括微软在这里.其实质是:

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
Run Code Online (Sandbox Code Playgroud)

然后,/autorun根据此答案从命令行启动Outlook,根据需要更改路径/ macroname:

C:\Program Files\Microsoft Office\Office11\Outlook.exe" /autorun macroname
Run Code Online (Sandbox Code Playgroud)

选项4
您可以使用与选项3相同的方法,但将Outlook VBA移动到PowerShell脚本(您可以从命令行运行).这里的例子.这可能是最整洁的解决方案,IMO.


Pri*_*iya 5

使用 Powershell,您可以发送电子邮件并将其集成到 Bat 文件脚本中。但您需要在计算机上安装 Outlook。这对我来说在本地电脑上有效,但在服务器上无效。在服务器上,每次使用此命令时它都会要求我授予权限,而且在服务器上安装 Outlook 对于许多人来说可能不是首选。在本地 PC 上运行时效果完美

$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "xyz@mail.com"
$Mail.Subject = "Action"
$Mail.Body ="Pay rise please"
$Mail.Send()
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

96179 次

最近记录:

6 年,10 月 前