简体   繁体   English

使用C#提取Outlook传递失败电子邮件主题和收件人

[英]Extract Outlook delivery failure email subjects and recepients using C#

I used to send thousands of mails over outlook and many since many recipient leave th org, used to get delivery failure mail. 我曾经在Outlook上发送过数千封邮件,而且由于许多收件人离开了组织,所以收到了许多邮件,用于获取传递失败的邮件。

In order to correct my list I want to extract subject and recipient to filter my master list. 为了更正我的列表,我想提取主题和收件人以过滤我的主列表。

The issue is that my .net program is able to fetch mail subject it's not able to fetch the recipient. 问题是我的.net程序能够获取邮件主题,而无法获取收件人。 Since Recipient is not available in To/CC/BCC list but in mail body, I have to read mail body and will subsequently fetch the email-id. 由于收件人在“收件人/抄送/密件抄送”列表中不可用,但在邮件正文中不可用,因此我必须阅读邮件正文,随后将获取电子邮件ID。

Using below code however this is not reading the CC list from the mail body. 但是,使用下面的代码不会从邮件正文中读取抄送列表。

    Microsoft.Office.Interop.Outlook.Application app = null;
    Microsoft.Office.Interop.Outlook._NameSpace ns = null;
    Microsoft.Office.Interop.Outlook.ReportItem item = null;
    Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
    Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;


        try
        {
            app = new Microsoft.Office.Interop.Outlook.Application();
            ns = app.GetNamespace("IPM.Note");
            ns.Logon (null, null, false, false);
            inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
            subFolder = inboxFolder.Folders["TestMail"]; //folder.Folders[1]; also works
            //MessageBox.Show("Folder Name: " + subFolder.Name, subFolder.EntryID);
            //MessageBox.Show("Num Items:  " + subFolder.Items.Count.ToString());

            for(int i=1;i<=subFolder.Items.Count;i++)
              {                   
                item = (Microsoft.Office.Interop.Outlook.ReportItem)subFolder.Items[i];                

               //var item = subFolder.Items[i];
                Console.WriteLine("Item: {0}", i.ToString());
                Console.WriteLine("Subject: {0}", item.Subject);
                //Console.WriteLine("Receipient: {0}", item); 
                Console.WriteLine("Categories: {0}", item.Categories);
                Console.WriteLine("Body: {0}", item.Body);
                Console.WriteLine("Body: {0}", item.BillingInformation);
                listBox1.Items.Add(item.Subject);                       

              }
        }
        catch(System.Runtime.InteropServices.COMException ex) 
        {
            Console.WriteLine(ex.ToString());
        }

The subject is appearing something in gibberish and unreadable format. 主题出现乱码和不可读的格式。

Also the item.To/CC/bcc is not exposed. 而且item.To/CC/bcc不公开。

Any help/suggestion pls... 任何帮助/建议...

I have used the following method before to extract email addresses from a body of an delivery failure email message: 在从传递失败电子邮件的正文中提取电子邮件地址之前,我已经使用以下方法:

private IList<string> FindEmailAddresses(string body)
{
    IList<string> emailAddresses = new List<string>;
    string emailMatch = @"\b([A-Z0-9._%-]+)@([A-Z0-9.-]+\.[A-Z]{2,6})\b";

    Regex mailReg = new Regex(emailMatch,
        RegexOptions.IgnoreCase | RegexOptions.Multiline);
    MatchCollection matches = mailReg.Matches(body);

    for (int index = 0; index < matches.Count; index++)
    {
        emailAddresses.Add(matches[index].ToString());
    }

    return emailAddresses;
}

A couple of notes: 一些注意事项:

The regex used is fairly simple and will catch the more simply formatted emails but can miss more complex formatted addresses. 使用的正则表达式非常简单,可以捕获格式更简单的电子邮件,但会丢失格式更复杂的地址。

I usually deal with the MailItem objects of the delivery failure emails. 我通常处理传递失败电子邮件的MailItem对象。 I haven't worked with ReportItem. 我尚未使用ReportItem。

我认为您可以将邮件服务器设置为向您发送失败的电子邮件地址列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM