在Outlook中为特定电子邮件运行jar

时间:2017-02-09 10:25:36

标签: java vba outlook outlook-vba

每次收到特定的电子邮件时,我都要运行一些java代码。 我只需在电子邮件中下载附件,运行jar并回复执行该附件的jar响应。

有可能以某种方式自动化吗?

我已经检查了可以使用outlook规则调用的VBA例程,但我不确定是否可以使用它来执行我的jar文件。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

这是结构,我会让你调整它!

您可以将规则设置为使用作为主程序的SaveToDiskAndReply

将其粘贴到Outlook模块的开头:

Option Explicit

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub RunSleep( _
                    exec As WshExec, _
                    Optional timeSegment As Long = 800 _
                    )
    Do While exec.Status = WshRunning
        Sleep timeSegment
    Loop
End Sub

Private Function RunProgram( _
                            program As String, _
                            Optional command As String = "" _
                            ) As WshExec
    Dim wsh As New WshShell
    Dim exec As WshExec

    Set exec = wsh.exec(program)
    Call exec.StdIn.WriteLine(command)
    Call RunSleep(exec)
    Set RunProgram = exec
End Function

Public Function Run_Jar() As String
    Dim program As WshExec
    Dim value As String
    '''Set the path (jar and log)
    Set program = RunProgram("java -jar ""D:\\Demo.jar"" 8861ccd621")
    DoEvents
    Run_Jar = program.StdOut.ReadAll
 End Function

并将其用作规则发起的脚本:

Public Sub SaveToDiskAndReply(ItM As Outlook.MailItem)
    Dim oAttS As Outlook.Attachments
    Dim objAtt As Outlook.Attachment
    Dim oItM As Outlook.MailItem
    Dim saveFolder As String
    Dim dateFormat As String
    Dim JarReturn As String

    dateFormat = Format(Now, "yyyy-mm-dd")
    saveFolder = "c:\temp\"
    Set oAttS = ItM.Attachments

    '''Save the attachements
    For Each objAtt In oAttS
        objAtt.SaveAsFile saveFolder & objAtt.FileName
    Next objAtt

    '''Run your jar
    JarReturn = Run_Jar

    '''Fill the email
    Set oItM = OutApp.CreateItem(0)


    '''Decomment the next line when you're done testing
    'On Error Resume Next
    With oItM
        .To = ItM.SenderEmailAddress
        .CC = ""
        .BCC = ""
        .Subject = ItM.Subject
        .Body = JarReturn
        For Each objAtt In oAttS
            .Attachments.Add saveFolder & objAtt.FileName
        Next objAtt
        .Send   'or use .Display
    End With
    On Error GoTo 0

    Set oAttS = Nothing
    Set objAtt = Nothing
End Sub