Outlook邮件规则无法正常工作

时间:2019-04-26 12:30:01

标签: c# outlook vsto outlook-addin

我正在尝试在Outlook中创建自定义邮件规则。该规则将邮件从特定的电子邮件地址移动到特定的文件夹。

我照做:

public partial class Ribbon1{

    private Outlook.Folders allFolders;

    private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
    {
        Outlook.MAPIFolder inbox = ThisAddIn.app.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
        Outlook.MAPIFolder mainFolder = inbox.Parent;
        allFolders = mainFolder.Folders;
    }

    private void button2_Click(object sender, RibbonControlEventArgs e)
    {
        Outlook.Rules rules = null;

        try
        {
            rules = ThisAddIn.app.Session.DefaultStore.GetRules(); //Gets list of outlook rules
        }
        catch
        {
            Debug.WriteLine("Could not obtain rules collection.");
            return;
        }

        string ruleName = "TestRule";

        Outlook.Rule rule = rules.Create(ruleName, Outlook.OlRuleType.olRuleReceive);
        rule.Name = ruleName;

        rule.Conditions.From.Recipients.Add("test12345@hotmail.com");
        rule.Conditions.From.Enabled = true;

        Outlook.MAPIFolder ruleFolder = allFolders["test1"];
        rule.Actions.MoveToFolder.Folder = ruleFolder;
        rule.Actions.MoveToFolder.Enabled = true;

        rule.Enabled = true;

        //Save rules
        try
        {
            rules.Save(true);
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
    }
}

我有一个按钮,当我单击它时,自定义规则将添加到Outlook。我检查了Outlook规则窗口。当我将自己发送到“ test12345@hotmail.com”时,它不会将邮件移至“ test1”文件夹。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

通常,操作/调用的顺序看起来不错。但是收件人应该根据地址簿解决。我建议从分解带有多个点的代码行开始。因此,所有基础COM对象将被释放。而且,由于每次调用相同的属性,您可能会获得一个新的对象实例,因此可能导致权限结果。例如:

rule.Conditions.From.Enabled = true;

Rule类的Conditions属性返回一个RuleConditions集合对象,该对象表示该规则的所有可用规则条件。因此,应立即发布。完成使用后,请使用System.Runtime.InteropServices.Marshal.ReleaseComObject释放Outlook对象。然后在Visual Basic中将变量设置为Nothing(在C#中为null)以释放对该对象的引用。在Systematically Releasing Objects文章中了解有关此内容的更多信息。

Sub CreateRule()  
  Dim colRules As Outlook.Rules 
  Dim oRule As Outlook.Rule  
  Dim colRuleActions As Outlook.RuleActions  
  Dim oMoveRuleAction As Outlook.MoveOrCopyRuleAction  
  Dim oFromCondition As Outlook.ToOrFromRuleCondition  
  Dim oExceptSubject As Outlook.TextRuleCondition  
  Dim oInbox As Outlook.Folder  
  Dim oMoveTarget As Outlook.Folder 

  'Specify target folder for rule move action  
  Set oInbox = Application.Session.GetDefaultFolder(olFolderInbox)  
  'Assume that target folder already exists  
  Set oMoveTarget = oInbox.Folders("Dan")  

  'Get Rules from Session.DefaultStore object 
  Set colRules = Application.Session.DefaultStore.GetRules()  

  'Create the rule by adding a Receive Rule to Rules collection 
  Set oRule = colRules.Create("Dan's rule", olRuleReceive) 

  'Specify the condition in a ToOrFromRuleCondition object  
  'Condition is if the message is sent by "DanWilson"  
  Set oFromCondition = oRule.Conditions.From  
  With oFromCondition  
      .Enabled = True  
      .Recipients.Add ("DanWilson")  
      .Recipients.ResolveAll  
  End With 

  'Specify the action in a MoveOrCopyRuleAction object  
  'Action is to move the message to the target folder  
  Set oMoveRuleAction = oRule.Actions.MoveToFolder  
  With oMoveRuleAction 
    .Enabled = True  
    .Folder = oMoveTarget  
  End With  

  'Specify the exception condition for the subject in a TextRuleCondition object  
  'Exception condition is if the subject contains "fun" or "chat"  
   Set oExceptSubject = oRule.Exceptions.Subject 

   With oExceptSubject  
     .Enabled = True  
     .Text = Array("fun", "chat")  
   End With  

   'Update the server and display progress dialog  
   colRules.Save  
 End Sub

此外,我建议您检查可对其他人正常使用的示例代码: