我有一个辅助交换帐户,其中服务器规则处于活动状态,它将收到的每封邮件转发到我的主帐户(在另一台服务器上)。为了避免无意义的转发标头并保留 From 和 To 字段,我将邮件作为附件转发并
我有这个代码的三个问题并且有点卡住了,所以我在这里发布它希望得到一些输入:
- 我想运行附件验证,因此只有实际的邮件类型被解包到收件箱。我找到了该
.Type
属性,但这只是给了我一个数字,我找不到相应的参考。如果发现任何非邮件附件(或没有附件),转发邮件应保存或不删除。 - 项目在收件箱中创建为草稿,而不是收到的邮件项目。我找不到任何方法来更改文档类型。
- 似乎我的代码在我的发件箱中随机创建了空邮件。也许这是由于从磁盘打开消息并且除了移动它之外没有对它做任何事情,但我现在不能确定。如果未打包的邮件有附件,则可以在发件箱中找到带有这些附件的空草稿。
下面我发布了整个代码,主要归功于相关问题的答案中的信息。
Public Sub unpackAttachedMessage(itm As Outlook.MailItem)
Dim olApp As New Outlook.Application
Dim olNameSpace As Outlook.NameSpace
Dim olTargetFolder As Outlook.Folder
Dim objAtt As Outlook.Attachment
' Program Configuration Variables and Constants
Const saveFolder As String = "C:\Temp\Outlook"
Const messageCategory As String = "CategoryName"
' Runtime Variables
Dim i As Integer
Dim attachmentCount As Integer
i = 1
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
' Folder creation does not seem to work.
If Not fso.FolderExists(saveFolder) Then
fso.CreateFolder (saveFolder)
End If
' For each attachment in the message.
For Each objAtt In itm.Attachments
' Save it to disk as a message.
objAtt.SaveAsFile saveFolder & "\" & i & ".msg"
' Retrieve a message from disk.
Dim message As Outlook.MailItem
Set message = Application.CreateItemFromTemplate(saveFolder & "\" & i & ".msg")
' Modify the Message.
' Note that this and potentially other message options need
' to be set BEFORE you move the item to its destination folder.
' Set the Category.
message.Categories = message.Categories & "," & messageCategory
' Mark as unread.
message.UnRead = True
' MsgBox "Class: " & itm.MessageClass & " --- Attached Item Class: " & message.MessageClass
' Doesn't work
'message.MessageClass = olPostItem
' Save changes to the message.
message.Save
' Move the item to Inbox.
Set olNameSpace = olApp.GetNamespace("MAPI")
Set olTargetFolder = olNameSpace.GetDefaultFolder(olFolderInbox)
message.Move olTargetFolder
' objAtt.DisplayName
Set objAtt = Nothing
i = i + 1
Next
attachmentCount = i
End Sub