在 Outlook 收件箱中获取第一封未读邮件并在 VBScript 中获取相关属性

Get first unread email in Outlook inbox and fetch related properties in VBScript

这是我从一位前团队成员那里继承来的。目前它用于从未读的电子邮件中获取附件,重命名附件并将它们存储在指定的文件夹中(在自动化工具的帮助下):

Set FSO = CreateObject("Scripting.FileSystemObject")
dim mail,olMSGUnicode, subject, olHTML, saveFolder, flag, subject1, companyCode, emailAddressTo
dim dd, mm, yy, hh, mm, ss, datevalue, dtsnow
subject = WScript.Arguments.Item(0)
saveFolder = WScript.Arguments.Item(1)

'Store DateTimeStamp once.
dtsnow = Now()

'Individual date components
dd = Right("00" & Day(dtsnow), 2)
mm = Right("00" & Month(dtsnow), 2)
yy = Year(dtsnow)

'Build the date string in the format yyyy-mm-dd
'datevalue = dd & "-" & mm & "-" & yy
 datevalue = dd & mm & yy

olMSGUnicode = 9
Set app = CreateObject("Outlook.Application")
Set ns = app.GetNamespace("MAPI")
'Temp subfolder in Outlook Inbox
Set mf = ns.GetDefaultFolder(6)
set objAtt = Outlook.Attachment

Set c = mf.Items

For Each mail In c
    'Read unread email only
    If mail.unread Then 
        'Count number of mail items that have attachments
        intCount = mail.Attachments.Count
        If intCount > 0 Then
            For i = 1 To intCount
                'Save to savefolder directory
                fileName = mail.Attachments.Item(i).DisplayName
                newFileName = datevalue & "_" & fileName
                mail.Attachments.Item(i).SaveAsFile saveFolder & _
                    newFileName
            Next 
        End If
    'Mark email as read
    mail.Unread = False
    End If
Next

WScript.StdOut.WriteLine(newFileName)
                
On Error goto 0

如何实现以下目标?

  1. 获取每封电子邮件的发件人电子邮件地址或 Exchange 用户
  2. 有多种WriteLine方法。此输出然后由自动化工具捕获以进行进一步处理
  3. 我不想一次遍历所有未读电子邮件,而是只想获取第一封未读电子邮件。此代码将用于捕获在给定一天内间歇性发送到邮箱的多个请求

对于混乱的代码或不正确的术语使用,我深表歉意,老实说,我对 VBScript 不是很熟悉。

任何指导将不胜感激。

要获取发件人电子邮件地址,请使用 mail.SenderEmailAddress

不确定“多个 WriteLine 方法”,您可以将 writeline 方法放入循环中

为了与您的代码保持一致,如果您只想阅读一封电子邮件而不是一次阅读所有电子邮件:

bFoundUread = false
For Each mail In c
    'Read unread email only
    If bFoundUread = false Then
        If mail.unread Then 
            bFoundUread = true
            'Count number of mail items that have attachments
            intCount = mail.Attachments.Count
            If intCount > 0 Then
                For i = 1 To intCount
                    'Save to savefolder directory
                    fileName = mail.Attachments.Item(i).DisplayName
                    newFileName = datevalue & "_" & fileName
                    mail.Attachments.Item(i).SaveAsFile saveFolder & _
                        newFileName
                Next 
            End If
        'Mark email as read
        mail.Unread = False
        End If
    End If
Next