[英]C# Get last reply in Outlook conversation
How to get the last reply in an Outlook conversation? 如何在Outlook对话中获得最后的答复? For example, the following code:
例如,以下代码:
Outlook.MailItem item = Items.GetLast();
MessageBox.Show(item.Body);
will display the entire email, not only the last reply. 将显示整个电子邮件,而不仅仅是最后一个回复。
The following example shows how to get and display mail items in a conversation. 以下示例显示如何在对话中获取和显示邮件项目。
void DemoConversation()
{
object selectedItem = Application.ActiveExplorer().Selection[1];
// For this example, you will work only with
//MailItem. Other item types such as
//MeetingItem and PostItem can participate
//in Conversation.
if (selectedItem is Outlook.MailItem)
{
// Cast selectedItem to MailItem.
Outlook.MailItem mailItem = selectedItem as Outlook.MailItem;
// Determine store of mailItem.
Outlook.Folder folder = mailItem.Parent as Outlook.Folder;
Outlook.Store store = folder.Store;
if (store.IsConversationEnabled == true)
{
// Obtain a Conversation object.
Outlook.Conversation conv = mailItem.GetConversation();
// Check for null Conversation.
if (conv != null)
{
// Obtain Table that contains rows
// for each item in Conversation.
Outlook.Table table = conv.GetTable();
Debug.WriteLine("Conversation Items Count: " + table.GetRowCount().ToString());
Debug.WriteLine("Conversation Items from Table:");
while (!table.EndOfTable)
{
Outlook.Row nextRow = table.GetNextRow();
Debug.WriteLine(nextRow["Subject"]
+ " Modified: "
+ nextRow["LastModificationTime"]);
}
Debug.WriteLine("Conversation Items from Root:");
// Obtain root items and enumerate Conversation.
Outlook.SimpleItems simpleItems = conv.GetRootItems();
foreach (object item in simpleItems)
{
// In this example, enumerate only MailItem type.
// Other types such as PostItem or MeetingItem
// can appear in Conversation.
if (item is Outlook.MailItem)
{
Outlook.MailItem mail = item as Outlook.MailItem;
Outlook.Folder inFolder = mail.Parent as Outlook.Folder;
string msg = mail.Subject
+ " in folder " + inFolder.Name;
Debug.WriteLine(msg);
}
// Call EnumerateConversation
// to access child nodes of root items.
EnumerateConversation(item, conv);
}
}
}
}
}
void EnumerateConversation(object item, Outlook.Conversation conversation)
{
Outlook.SimpleItems items = conversation.GetChildren(item);
if (items.Count > 0)
{
foreach (object myItem in items)
{
// In this example, enumerate only MailItem type.
// Other types such as PostItem or MeetingItem
// can appear in Conversation.
if (myItem is Outlook.MailItem)
{
Outlook.MailItem mailItem = myItem as Outlook.MailItem;
Outlook.Folder inFolder = mailItem.Parent as Outlook.Folder;
string msg = mailItem.Subject
+ " in folder " + inFolder.Name;
Debug.WriteLine(msg);
}
// Continue recursion.
EnumerateConversation(myItem, conversation);
}
}
}
In the sample code example, we get a selected MailItem
object and then determine the store of the MailItem
object by using the Store
property of the Folder
object. 在示例代码示例中,我们获取了一个选定的
MailItem
对象,然后使用Folder
对象的Store
属性确定MailItem
对象的Store
。 DemoConversation then checks whether the IsConversationEnabled
property is true; 然后,
IsConversationEnabled
检查IsConversationEnabled
属性是否为true。 if it is true, the code example gets Conversation
object by using the GetConversation
method. 如果为true,则代码示例使用
GetConversation
方法获取Conversation
对象。 If the Conversation
object is not a null reference, the example gets the associated Table
object that contains each item in the conversation by using the GetTable
method. 如果“
Conversation
对象不是空引用,则该示例使用GetTable
方法获取包含对话中每个项目的关联Table
对象。 The example then enumerates each item in the Table
and calls EnumerateConversation
on each item to access the child nodes of each item. 然后,该示例枚举
Table
每个项目,并在每个项目上调用EnumerateConversation
以访问每个项目的子节点。 EnumerateConversation
takes a Conversation
object and gets the child nodes by using the GetChildren(Object)
method. EnumerateConversation
接受一个Conversation
对象,并使用GetChildren(Object)
方法获取子节点。 EnumerateConversation
is called recursively until there are no more child nodes. 递归调用
EnumerateConversation
直到没有更多的子节点为止。 Each conversation item is then displayed to the user. 然后,每个对话项都显示给用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.