首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用以编程方式创建的规则移动邮件

使用以编程方式创建的规则移动邮件
EN

Stack Overflow用户
提问于 2019-04-26 20:30:01
回答 1查看 525关注 0票数 0

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

代码语言:javascript
复制
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”文件夹。

当我手动创建它时,它可以正常工作。

EN

回答 1

Stack Overflow用户

发布于 2019-06-13 04:01:32

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

代码语言:javascript
复制
rule.Conditions.From.Enabled = true;

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

代码语言:javascript
复制
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

此外,我建议您检查一下示例代码,它可以正确地用于其他代码:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55867695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档