如何测试邮件是普通邮件还是回复?

How to test if the email is a regular email or reply?

我正在编写一个脚本,它会根据日期自动将电子邮件保存到指定的文件夹。所以基本上,今天的所有电子邮件都将保存在标记为 05/20/2015 的文件夹中。

我目前遇到的问题是所有回复消息都被保存为文件,而不是 .msg

我的问题是,有没有办法测试电子邮件是回复邮件还是普通邮件。我想我本来可以做这样的事情。 If Item.Reply Then (code here)。这是这样做的正确方法吗,或者我是否必须着手测试电子邮件是否以不同的方式回复?

这是我正在使用的代码。这是我的意思的一个例子。乔给我发了一封电子邮件。该电子邮件以 .msg 格式保存到我指定的文件夹中。我回应乔。乔回应。理论上,Joe 返回的响应应保存为 .msg,但消息被保存为 .file。那是我不理解的部分,我不知道为什么会这样。

    Option Explicit
'// Save the message as a native .msg
Public Sub SaveMesg(Item As Outlook.MailItem)
 Dim fso As FileSystemObject
 Dim olNS As Outlook.NameSpace
 Dim SavePath As String
 Dim TimeDate As Date
 Dim SaveName As String
 Dim Enviro As String
 Dim NewFolder As String
 Dim EmailSubject As String

'// enviro gets the user account part of the path
'// so you can use the same code on different computers
Set olNS = Application.GetNamespace("MAPI")

ReplaceCharsForFileName SaveName, "_"

'// Use My Documents for older Windows.
NewFolder = "C:\ITDocs\" & Format(Now, "YYYY-MM-DD") & "\"

'// Test if directory or file exists
If FileOrDirExists(NewFolder) Then
    MsgBox NewFolder & " exists!"
Else
    MkDir NewFolder
End If

EmailSubject = FileName(Item.Subject)

'// Determine if there is subject
If Item.Subject <> vbNullString Then
    EmailSubject = Item.Subject
Else
    EmailSubject = "No Subject"
End If

'// Determine if the email is a response or not
'// If Item.Reply <> vbNullString Then
   '// EmailSubject = Item.Subject
 '//End If


'// Get Email subject & set name to be saved as
TimeDate = Item.ReceivedTime
SaveName = Format(TimeDate, "YYYYMMDD", vbUseSystemDayOfWeek, _
vbUseSystem) & Format(TimeDate, "-HHNNSS", _
vbUseSystemDayOfWeek, vbUseSystem) & "-" & EmailSubject & SaveName & ".msg"
Set fso = CreateObject("Scripting.FileSystemObject")



'// Save .msg File
SavePath = "C:\IT Documents\" & NewFolder & "\"
Debug.Print NewFolder & SaveName
Item.SaveAs NewFolder & SaveName, olMSG

End Sub

'// This function removes invalid and other characters from file names
Private Sub ReplaceCharsForFileName(SaveName As String, _
 sChr As String _
 )
 SaveName = Replace(SaveName, "/", sChr)
 SaveName = Replace(SaveName, "\", sChr)
 SaveName = Replace(SaveName, ":", sChr)
 SaveName = Replace(SaveName, "?", sChr)
 SaveName = Replace(SaveName, Chr(34), sChr)
 SaveName = Replace(SaveName, "<", sChr)
 SaveName = Replace(SaveName, ">", sChr)
 SaveName = Replace(SaveName, "|", sChr)
 SaveName = Replace(SaveName, "&", sChr)
 SaveName = Replace(SaveName, "%", sChr)
 SaveName = Replace(SaveName, "*", sChr)
 SaveName = Replace(SaveName, " ", sChr)
 SaveName = Replace(SaveName, "{", sChr)
 SaveName = Replace(SaveName, "[", sChr)
 SaveName = Replace(SaveName, "]", sChr)
 SaveName = Replace(SaveName, "}", sChr)
 SaveName = Replace(SaveName, "!", sChr)

End Sub

'// Good practice suggests that it is wise to check before taking certain               actions
'// This function checks if File or Dir Exists
Function FileOrDirExists(PathName As String) As Boolean
Dim iTemp As Integer

 '// Ignore errors to allow for error evaluation
On Error Resume Next
iTemp = GetAttr(PathName)

 '// Check if error exists and set response appropriately
Select Case Err.Number
Case Is = 0
    FileOrDirExists = True
Case Else
    FileOrDirExists = False
End Select

 '// Resume error checking
On Error GoTo 0
End Function

Function FileName(strText As String) As String
 Dim strStripChars As String
 Dim intLen As Integer
 Dim i As Integer
 strStripChars = "/\[]:=," & Chr(34)
    intLen = Len(strStripChars)
 strText = Trim(strText)
    For i = 1 To intLen
 strText = Replace(strText, Mid(strStripChars, i, 1), "")

 Next
 FileName = strText
End Function

PR_MESSAGE_FLAGS 低级 属性 包含标志的位掩码,指示消息的来源和当前状态。您对以下标志感兴趣:

  • MSGFLAG_READ

邮件已标记为已读。客户端可以在消息首次保存之前通过调用消息的 IMAPIProp::SetProps 方法来设置此标志。如果设置了 MSGFLAG_ASSOCIATED 标志,则忽略此标志。

  • MSGFLAG_UNSENT

邮件仍在撰写中。它已保存,但尚未发送。通常,此标志在消息发送后被清除。

您可以使用 PropertyAccessor 对象来获取低级 属性 值。 DASL 名称是 http://schemas.microsoft.com/mapi/proptag/0x0E070003.

使用任何低级别 属性 查看器(例如 MFCMAPI 或 OutlookSpy)来探索此类属性及其值。

你对"regular email"的定义是什么?您的意思是您需要区分消息是由当前用户发送还是收到

收到的邮件中填充了 MailItem.ReceivedByName。发送的消息没有。

您注意到当您遇到回复或转发的消息时会出现问题。两者的共同点是主题的前缀,例如 "RE: something" 或 "FW: something else"。注意前缀中的冒号。在调用 SaveAs 之前,我没有看到您做任何工作来从名称中删除那个冒号。所以你会给 SaveAs 一个非常奇怪的路径,里面有多个冒号。

我不知道 SaveAs 对此有何反应,但我会从这里开始。