使用Outlook插件实现自动保存邮件为TestTrack记录

348 篇文章 0 订阅
7 篇文章 0 订阅
使用Outlook插件实现自动保存邮件为TestTrack记录
标签: 正则表达式stringapplicationnullpythonexception
1726人阅读 评论(1) 收藏 举报
本文章已收录于:
分类:

背景:

天津的同事需要给位于北京的TestTrack里面报Bugs,但是没有权限,只能发邮件给北京同事,由bj同事手动添加,为此我们需要开发一个工具来自动依据邮件生成TT记录


方案:

Outlook Add-in + Python脚本

Add-in实现自动截获邮件,保存MailItem.Body为txt文本,由python编写的脚本将生成的txt借助TestTrack SDK提供的api保存到TestTrack。


实现:

Add-in

1.  在ThisAddIn_Startup中使用NewMailEx,不要使用NewMail。

- NewMailEx,当有新邮件到来时会触发,他检索的位置包括刚刚收到的邮件,而NewMail,新邮件到来时也会触发,但是仅能在inbox和子subfolder下检索

见MSDN解释:

NewMailEx: 
Occurs when one or more new items are received in the Inbox. This event passes a list of entry IDs of all the items received in the Inbox since the last time the event was fired. 

  1. private void ThisAddIn_Startup(object sender, System.EventArgs e)  
  2. {  
  3.     this.Application.NewMailEx += new Outlook.ApplicationEvents_11_NewMailExEventHandler(Application_NewMailEx);  
  4. }  
  5.   
  6. void Application_NewMailEx(string EntryIDCollection)  
  7. {  
  8.     //Todo: handle coming mail item  
  9. }  
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            this.Application.NewMailEx += new Outlook.ApplicationEvents_11_NewMailExEventHandler(Application_NewMailEx);
        }

        void Application_NewMailEx(string EntryIDCollection)
        {
            //Todo: handle coming mail item
        }
2. 使用Application.Session.GetItemFromID(id)来获取MailItem
  1. var item = Application.Session.GetItemFromID(id) as Outlook.MailItem;  
var item = Application.Session.GetItemFromID(id) as Outlook.MailItem;

3. 使用正则表达式来筛选邮件

- 使用正则表达式检查MailItem.Subject 是否match “[XXX]:”

  1. string regStr = @"^[.\S]:";  
string regStr = @"^[.\S]:";

- 使用SenderAddress来检查发件人

4. 获取邮件Body,使用字符串处理方式处理邮件内容

- 可以直接使用MailItem.SaveAs()直接保存邮件标题、发件人、发件地址、正文等内容

- 可以使用MailItem.Body直接读取邮件正文,自己编写保存方法

  1. public static void SaveAsTXT(string fileName, string content, FileMode mode)  
  2. {  
  3.     if (string.IsNullOrEmpty(fileName) == false || mode == null)  
  4.     {  
  5.         throw new ArgumentNullException("SaveAsTXT");  
  6.     }  
  7.   
  8.     FileStream ofs = null;  
  9.     StreamWriter sw = null;  
  10.   
  11.     try  
  12.     {  
  13.         ofs = new FileStream(fileName, mode);  
  14.         sw = new StreamWriter(ofs);  
  15.         sw.WriteLine(content);  
  16.     }  
  17.     catch (Exception ex)  
  18.     {  
  19.         string msg = string.Format("fail to save as txt, detail is {0}", ex.ToString());  
  20.         sw.WriteLine();  
  21.     }  
  22.     finally  
  23.     {  
  24.         if (sw != null)  
  25.         {  
  26.             sw.Close();  
  27.         }  
  28.         if (ofs != null)  
  29.         {  
  30.             ofs.Close();  
  31.         }  
  32.     }  
  33. }  
        public static void SaveAsTXT(string fileName, string content, FileMode mode)
        {
            if (string.IsNullOrEmpty(fileName) == false || mode == null)
            {
                throw new ArgumentNullException("SaveAsTXT");
            }

            FileStream ofs = null;
            StreamWriter sw = null;

            try
            {
                ofs = new FileStream(fileName, mode);
                sw = new StreamWriter(ofs);
                sw.WriteLine(content);
            }
            catch (Exception ex)
            {
                string msg = string.Format("fail to save as txt, detail is {0}", ex.ToString());
                sw.WriteLine();
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }
                if (ofs != null)
                {
                    ofs.Close();
                }
            }
        }

Pythoe

// 明天再写。。。




0
0
 
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值