Outlook MailItem:如何区分邮件是传入还是传出?
我正在用 C# 编写 VSTO Outlook 插件,我需要区分给定的 MailItem 是传入还是传出(或者两者都不是,例如当它是草稿时)。
有什么万无一失的方法可以做到这一点吗?我现在拥有的最佳解决方案是获取收件人、抄送和密送的列表,从活动帐户加载电子邮件地址,并检查这两个列表是否相交,但这对我来说似乎相当脆弱,我希望有更好的解决方案。
用例:我想获取电子邮件的相关日期,可以是 ReceivedTime 或 SentOn,但要知道应该使用哪一个,我需要知道邮件是否已发送或接收。
谢谢你的想法:)
I am writing VSTO Outlook addin in C#, and I need to distinguish, whether given MailItem is incoming or outgoing (or neither, when it is for example a draft).
Is there some foolproof way to do this? Best solution I have now would be getting a list of recipients, cc's, and bcc's, loading email adresses from active accounts, and checking if those two lists intersect, but this seems quite fragile to me, and I hope that there is a better solution.
Use case: I'd like to get a relevant date for an email, which could be either ReceivedTime, or SentOn, but to know which one I should use, I beed to know whether a mail was sent or received.
Thank you for ideas :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
来到这个页面是因为我在 VBA 中遇到了同样的问题。检查父文件夹很麻烦,因为一条消息可能保存在多个文件夹深处(因此您必须迭代多个文件夹),或者用户可能已更改该文件夹。一个极端的例子:已删除邮件文件夹包含传入和传出邮件项目。
我选择了与上面另一个人(Adi Kini)类似的解决方案,我在其中检查 ReceivedByName(我认为他选择了 ReceivedEntryID)。对于已发送的消息,无论其当前位于何处,ReceivedByName 属性始终为 Null ("")。此方法可以找到已拖入收件箱的已发送项目!。这似乎是一种相当可靠的检查方法。
奇怪的是,像检查邮件是传入还是传出这样看似简单的事情却会让我们犯错!
Came to this page because I was having same issue in VBA. Checking the parent folders is cumbersome, as a message can be held either several folders deep (and therefore you have to iterate up several folders) or the user may have changed the folder. An extreme example: the deleted items folder contains both incoming and outgoing mail items.
I have chosen a similar solution to another person (Adi Kini) above where I check the ReceivedByName (I think he chose ReceivedEntryID). The ReceivedByName property is always Null ("") for a sent message, wherever it currently lays. This method can find a sent item that has been dragged to the inbox!. It seems a fairly reliable method of checking.
It seems odd that such an apparently straightforward thing as checking whether mail is incoming or outgoing can trip us up!
我带着同样的问题来到这里。由于我将明确建议用户将邮件移动到我的用例中的某个文件夹,因此检查 Parent 不会有帮助...
关于 MailItem.Sent:您确定 MailItem.Sent 以这种方式工作吗?我刚刚编写了一个简单的代码来运行我的收件箱和已发送项目,并且几乎所有这些已发送都是 true。我认为这实际上只是表明邮件是否已发送(=不是草稿)...
I came here with the same problem. Since I will explicitly suggest that user moves the mail to some folder in my usecase, checking Parent would not help...
Regarding MailItem.Sent: are you sure that MailItem.Sent works this way? I just wrote a simple code to run through both my Inbox and SentItems and for almost all of them Sent is true. I assume this is really just an indication whether the mail has been sent (= is not draft)...
我通过在发送电子邮件后添加新的 UserProperty 解决了这个问题。因此,当我需要检查电子邮件是否已发送时,我会检查此属性。即使电子邮件已移出“已发送”文件夹,此操作仍然有效。当然,这仅适用于新创建的电子邮件,但您可以在首次启动时将此属性添加到“已发送”文件夹中的所有电子邮件。唯一的错误是默认打印 UserProperties,但这可以覆盖。
I resolved this problem by adding a new UserProperty after e-mail was sent. So when I need to check if e-mail was sent I check this property. This works even if e-mail was moved out of Sent folder. Of course, this works only for newly created e-mails, but you may add this property to all e-mails in Sent folder during first start. The only bug is that UserProperties are printed by default, but this can be overridden.
这就是我检查邮件类型的方法,即使邮件被移动到任何文件夹,它也能工作。该解决方案使用 Outlook 2007 中提供的 PROPERTY ACCESSOR。下面是代码
This is how I check mail type and it works even if mail is moved to any folder. This solution uses PROPERTY ACCESSOR which is available in outlook 2007. Below is the code
MailItem.sent 对于传入也是如此。
检查 MailItem.ReceivedByEntryID 怎么样。但我确信这会失败(对于收件箱中的邮件,ReceivedByEntryID 将为空),如果您说从 Outlook Express 或其他电子邮件程序导入,则
迭代通过邮件帐户/senderemail 可能会有所帮助,如您所说,但它并不是万无一失的(就像如果您重命名电子邮件地址)
MailItem.sent is true for incoming too.
How about checking MailItem.ReceivedByEntryID. But i am sure this will fail (ReceivedByEntryID will be null for mails in inbox) if you say import from outlook express or maybe some other email program
Iterating thru mail accounts/senderemail may help as you said, but its not fool proof (like if you rename the email address)
查看 MailItem 的 .Parent 属性 - 检查文件夹属性以确定它是否是收件箱、发件箱、草稿、已发送项目等。
Take a look at the MailItem's .Parent property - examine the folder properties to determine if it is the inbox, outbox, drafts, sent items, etc.
您可以检查它是否位于
Outlook.OlDefaultFolders.olFolderInbox
或olFolderOutbox
内,然后您应该可以使用其他方法来检查它是否位于这些文件夹中!You can check if it's inside the
Outlook.OlDefaultFolders.olFolderInbox
orolFolderOutbox
, then there should be other methods you can use to check if it's inside either of these folders!对于通过 SMTP 地址进行简单的发送/接收控制,我建议采用地址检查方法。
可以这样完成:
For a simple sended/received control by SMTP address, i suggest an address-check approach.
It can be done in this way:
我反对 SenderName 与 CurrentUser,以区分收件箱或已发送文件夹中的电子邮件。
I contradict SenderName vs CurrentUser, to distinguish between emails in inbox or sent folder.
您尝试过 MailItem.Sent 属性吗?
传入时为 true,传出时为 false。
Did you try MailItem.Sent property?
Its true for incoming, and false for outgoing.