Outlook邮件规则无法正常工作

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (55)

我试图在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”文件夹。

我怎么解决这个问题?

提问于
用户回答回答于

通常,动作/调用的顺序看起来很好。但收件人应该通过地址簿解决。我建议从分解多个点的代码行开始。因此,将释放所有底层COM对象。此外,它可以导致权限结果,因为每次调用相同的属性时,您可能会获得一个新的对象实例。例如:

rule.Conditions.From.Enabled = true;

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

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

此外,我建议检查适用于其他人的示例代码:

所属标签

可能回答问题的人

  • 天使的炫翼

    17 粉丝531 提问36 回答
  • 富有想象力的人

    3 粉丝0 提问29 回答
  • o o

    4 粉丝494 提问27 回答
  • 学生

    3 粉丝476 提问27 回答

扫码关注云+社区

领取腾讯云代金券