针对 Outlook Interop MailItem 的自定义属性

发布于 2024-10-21 00:09:48 字数 1453 浏览 2 评论 0原文

我将自定义属性添加到 Microsoft.Office.Interop.Outlook.MailItem 对象,如下所示:

Public Const SharePointSiteUrl As String = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/SharePointSiteUrl"
Public Const SharePointSiteFolder As String = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/SharePointSiteFolder"
Public Const SharePointSiteUsername As String = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/SharePointSiteUsername"
Public Const SharePointSitePassword As String = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/SharePointSitePassword"

...

email.PropertyAccessor.SetProperty(OutlookHelper.SharePointSiteUrl, sharepointSite)
email.PropertyAccessor.SetProperty(OutlookHelper.SharePointSiteFolder, sharepointFolder)
email.PropertyAccessor.SetProperty(OutlookHelper.SharePointSiteUsername, sharepointUserName)
email.PropertyAccessor.SetProperty(OutlookHelper.SharePointSitePassword, sharepointPassword)

执行上述四个调用后,我测试是否可以使用 PropertyAccessor.GetProperty 访问属性,它们就在那里。

然后,我使用 .SaveAs 将电子邮件保存到目录中。当我稍后打开邮件项目并尝试访问属性时,出现以下错误:

email.PropertyAccessor.GetProperty(SharePointSiteFolder)

The property "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/SharePointSiteFolder" is unknown or cannot be found.

I am adding custom properties to a Microsoft.Office.Interop.Outlook.MailItem object like so:

Public Const SharePointSiteUrl As String = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/SharePointSiteUrl"
Public Const SharePointSiteFolder As String = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/SharePointSiteFolder"
Public Const SharePointSiteUsername As String = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/SharePointSiteUsername"
Public Const SharePointSitePassword As String = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/SharePointSitePassword"

...

email.PropertyAccessor.SetProperty(OutlookHelper.SharePointSiteUrl, sharepointSite)
email.PropertyAccessor.SetProperty(OutlookHelper.SharePointSiteFolder, sharepointFolder)
email.PropertyAccessor.SetProperty(OutlookHelper.SharePointSiteUsername, sharepointUserName)
email.PropertyAccessor.SetProperty(OutlookHelper.SharePointSitePassword, sharepointPassword)

After the four calls above have been executed, I test if I can access the properies using PropertyAccessor.GetProperty, and they are there.

I then save the email to a directory using .SaveAs. When I open the mail item later and try and access the properties I get the following error:

email.PropertyAccessor.GetProperty(SharePointSiteFolder)

The property "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/SharePointSiteFolder" is unknown or cannot be found.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缪败 2024-10-28 00:09:48

您可以使用 ItemProperties 代替?

Option Explicit On
Option Strict On

Imports Microsoft.Office.Interop.Outlook

Public Class Form1

    Private Shared ReadOnly TestFile As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Test.msg")

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SetProperties()
        GetProperties()
        Me.Close()
    End Sub
    Private Shared Sub GetProperties()
        Dim app = New Microsoft.Office.Interop.Outlook.Application()
        Dim email = DirectCast(app.CreateItemFromTemplate(TestFile), Microsoft.Office.Interop.Outlook.MailItem)
        ''//Write out the properties
        Trace.WriteLine(OutlookHelper.GetStringProperty(email, OutlookHelper.SharePointSiteUrl))
        Trace.WriteLine(OutlookHelper.GetStringProperty(email, OutlookHelper.SharePointSiteFolder))
        Trace.WriteLine(OutlookHelper.GetStringProperty(email, OutlookHelper.SharePointSiteUsername))
        Trace.WriteLine(OutlookHelper.GetStringProperty(email, OutlookHelper.SharePointSitePassword))
    End Sub
    Private Shared Sub SetProperties()
        Dim app As New Microsoft.Office.Interop.Outlook.Application()
        Dim email = DirectCast(app.CreateItem(OlItemType.olMailItem), MailItem)

        ''//The values that we want to set
        Dim sharepointSite = "it was the best of times"
        Dim sharepointFolder = "it was the worst of times"
        Dim sharepointUserName = "it was the age of wisdom"
        Dim sharepointPassword = "it was the age of foolishness"

        ''//Set the properties
        OutlookHelper.SetStringProperty(email, OutlookHelper.SharePointSiteUrl, sharepointSite)
        OutlookHelper.SetStringProperty(email, OutlookHelper.SharePointSiteFolder, sharepointFolder)
        OutlookHelper.SetStringProperty(email, OutlookHelper.SharePointSiteUsername, sharepointUserName)
        OutlookHelper.SetStringProperty(email, OutlookHelper.SharePointSitePassword, sharepointPassword)

        ''//Save the file
        email.SaveAs(TestFile)
    End Sub

End Class
Public Class OutlookHelper
    Public Const SharePointSiteUrl As String = "SharePointSiteUrl"
    Public Const SharePointSiteFolder As String = "SharePointSiteFolder"
    Public Const SharePointSiteUsername As String = "SharePointSiteUsername"
    Public Const SharePointSitePassword As String = "SharePointSitePassword"

    Public Shared Function GetStringProperty(ByVal mailItem As MailItem, ByVal key As String) As String
        ''//Validate input
        If mailItem Is Nothing Then Throw New ArgumentNullException("mailItem")
        If String.IsNullOrEmpty(key) Then Throw New ArgumentNullException("key")
        ''//Try to get the property
        Dim O = mailItem.ItemProperties(key)
        ''//See if we got something and that if it has a value
        If (O IsNot Nothing) AndAlso (O.Value IsNot Nothing) Then
            ''//Return the value as a string
            Return O.Value.ToString()
        Else
            ''//Property wasn't found or it was empty
            Return Nothing
        End If
    End Function
    Public Shared Sub SetStringProperty(ByVal mailItem As MailItem, ByVal key As String, ByVal value As String)
        ''//Check if the property already exists
        Dim O = mailItem.ItemProperties(key)
        ''//If not
        If (O Is Nothing) Then
            ''//Create it
            mailItem.ItemProperties.Add(key, OlUserPropertyType.olText)
        End If
        ''//Set the property
        mailItem.ItemProperties(key).Value = value
    End Sub
End Class

Can you use ItemProperties instead?

Option Explicit On
Option Strict On

Imports Microsoft.Office.Interop.Outlook

Public Class Form1

    Private Shared ReadOnly TestFile As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Test.msg")

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SetProperties()
        GetProperties()
        Me.Close()
    End Sub
    Private Shared Sub GetProperties()
        Dim app = New Microsoft.Office.Interop.Outlook.Application()
        Dim email = DirectCast(app.CreateItemFromTemplate(TestFile), Microsoft.Office.Interop.Outlook.MailItem)
        ''//Write out the properties
        Trace.WriteLine(OutlookHelper.GetStringProperty(email, OutlookHelper.SharePointSiteUrl))
        Trace.WriteLine(OutlookHelper.GetStringProperty(email, OutlookHelper.SharePointSiteFolder))
        Trace.WriteLine(OutlookHelper.GetStringProperty(email, OutlookHelper.SharePointSiteUsername))
        Trace.WriteLine(OutlookHelper.GetStringProperty(email, OutlookHelper.SharePointSitePassword))
    End Sub
    Private Shared Sub SetProperties()
        Dim app As New Microsoft.Office.Interop.Outlook.Application()
        Dim email = DirectCast(app.CreateItem(OlItemType.olMailItem), MailItem)

        ''//The values that we want to set
        Dim sharepointSite = "it was the best of times"
        Dim sharepointFolder = "it was the worst of times"
        Dim sharepointUserName = "it was the age of wisdom"
        Dim sharepointPassword = "it was the age of foolishness"

        ''//Set the properties
        OutlookHelper.SetStringProperty(email, OutlookHelper.SharePointSiteUrl, sharepointSite)
        OutlookHelper.SetStringProperty(email, OutlookHelper.SharePointSiteFolder, sharepointFolder)
        OutlookHelper.SetStringProperty(email, OutlookHelper.SharePointSiteUsername, sharepointUserName)
        OutlookHelper.SetStringProperty(email, OutlookHelper.SharePointSitePassword, sharepointPassword)

        ''//Save the file
        email.SaveAs(TestFile)
    End Sub

End Class
Public Class OutlookHelper
    Public Const SharePointSiteUrl As String = "SharePointSiteUrl"
    Public Const SharePointSiteFolder As String = "SharePointSiteFolder"
    Public Const SharePointSiteUsername As String = "SharePointSiteUsername"
    Public Const SharePointSitePassword As String = "SharePointSitePassword"

    Public Shared Function GetStringProperty(ByVal mailItem As MailItem, ByVal key As String) As String
        ''//Validate input
        If mailItem Is Nothing Then Throw New ArgumentNullException("mailItem")
        If String.IsNullOrEmpty(key) Then Throw New ArgumentNullException("key")
        ''//Try to get the property
        Dim O = mailItem.ItemProperties(key)
        ''//See if we got something and that if it has a value
        If (O IsNot Nothing) AndAlso (O.Value IsNot Nothing) Then
            ''//Return the value as a string
            Return O.Value.ToString()
        Else
            ''//Property wasn't found or it was empty
            Return Nothing
        End If
    End Function
    Public Shared Sub SetStringProperty(ByVal mailItem As MailItem, ByVal key As String, ByVal value As String)
        ''//Check if the property already exists
        Dim O = mailItem.ItemProperties(key)
        ''//If not
        If (O Is Nothing) Then
            ''//Create it
            mailItem.ItemProperties.Add(key, OlUserPropertyType.olText)
        End If
        ''//Set the property
        mailItem.ItemProperties(key).Value = value
    End Sub
End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文