通过outlook从命令行发送电子邮件,而无需单击发送 [英] Sending email from Command-line via outlook without having to click send

查看:1230
本文介绍了通过outlook从命令行发送电子邮件,而无需单击发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过命令行发送电子邮件,而不需要任何人工交互来自动化。



我知道我们可以使用mailto命令,除非我点击发送,否则它不会发送。



我在网上看到我们可以使用blat,但是我不能使用outlook之外的任何东西。



我昨天甚至没有对Google进行全天研究,但没有解决方案。



这是封闭的帖子我发现链接到SOF post



任何帮助将会有所帮助。



只是fyi:我正在调查一些telnet命令发送电子邮件还没有获得成功。
telnet命令发送电子邮件

解决方案

选项1

您没有说明您的环境,你有它可用你可以使用PowerShell脚本;一个示例是此处。其实质是:

  $ smtp = New-Object Net.Mail.SmtpClient(ho-ex2010-caht1.exchangeserverpro .net)
$ smtp.Send(reports@exchangeserverpro.net,administrator@exchangeserverpro.net,测试电子邮件,这是一个测试)

然后,您可以按照此示例

  powershell.exe -noexit c:\ scripts\test.ps1 

请注意,PowerShell 2.0默认安装在Windows 7和Windows Server 2008R2包含更简单的 Send-MailMessage 选项2

如果您准备使用第三方软件,是某些行
此SendEmail命令行工具。这取决于你的目标环境;



选项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

设置objOutlook = CreateObject(Outlook.Application)
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

使用objOutlookMsg
设置objOutlookRecip = .Recipients.Add(Nancy Davolio)
objOutlookRecip.Type = olTo
设置消息的主题,主体和重要性。
.Subject =这是使用Microsoft Outlook的自动化测试
.Body =这是消息的正文。 & vbCrLf& vbCrLf
.Importance = olImportanceHigh'高重要性

如果不是IsMissing(AttachmentPath)则
设置objOutlookAttach = .Attachments.Add(AttachmentPath)
结束如果

对于每个ObjOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next

.Save
.Send
End With
Set objOutlook = Nothing
End Sub

然后,从命令行启动Outlook, c $ c> / autorun 参数,根据此答案(必要时更改路径/宏指令):

  C:\Program Files\ Microsoft Office \Office11\Outlook.exe/ autorun macroname 

选项4

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


I need to send email via command-line without any human interactions for automation.

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.

I read online we can use blat but I cannot use anything other than outlook.

I've even did Google research whole day yesterday but no solution yet.

This is closed post I have found Link to SOF post.

Any help will be helpful.

just fyi: I am looking into some telnet commands to send email haven't gotten success in that yet either. telnet commands to send email

解决方案

Option 1
You didn't say much about your environment, but assuming you have it available you could use a PowerShell script; one example is here. The essence of this is:

$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")

You could then launch the script from the command line as per this example:

powershell.exe -noexit c:\scripts\test.ps1

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.

Option 2
If you're prepared to use third-party software, is something line 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.

Option 3
You could drive Outlook directly from a VBA script, which in turn you would trigger from a batch file; this would let you send an email using Outlook itself, which looks to be closest to what you're wanting. There are two parts to this; first, figure out the VBA scripting required to send an email. There are lots of examples for this online, including from Microsoft here. Essence of this is:

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

Then, launch Outlook from the command line with the /autorun parameter, as per this answer (alter path/macroname as necessary):

C:\Program Files\Microsoft Office\Office11\Outlook.exe" /autorun macroname

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆