8

One of the applications that I have to do an enhancement to soon has a number of emails that are sent from the application. To the point of verification, I need to see the delivery failure notifications. I've done some preliminary searching on how to do this, but, haven't managed to find anything that I could consider useful.

The email client that would be used would be Outlook 2007 or 2010 on an exchange server.

3 Answers 3

5

Finally figured it out, with the exception of reportitems such as Undeliverables. Most of the current code is from snippets that I put together to make it work.

public void readOutlookTest()
        {
            Microsoft.Office.Interop.Outlook.Application app = null;
            Microsoft.Office.Interop.Outlook._NameSpace ns = null;
            Microsoft.Office.Interop.Outlook.MailItem item = null;
            Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
            Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;
            Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application();
            app = application;
            ns = application.Session;

            try
            {
                ns.Logon(null, null, false, false);
                inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                subFolder = inboxFolder.Folders["folder name that you want to look in"];
                Console.WriteLine("Folder Name: {0}, EntryId: {1}", subFolder.Name, subFolder.EntryID);
                Console.WriteLine("Num Items: {0}", subFolder.Items.Count.ToString());
                for (int i = 1; i <= subFolder.Items.Count; i++)
                {
                        if (subFolder.Items[i] is ReportItem)
                        {
                            try
                            {
                                Console.WriteLine("Item: {0} is a Report Item.  I don't know how to deal with this yet.", i.ToString());
                            }
                            catch
                            {
                                Console.WriteLine("problems");
                            }
                        }
                        else if (subFolder.Items[i] is MailItem)
                        {
                            item = (MailItem)subFolder.Items[i];
                            item = subFolder.Items[i];
                            Console.WriteLine("Item: {0}", i.ToString());
                            Assert.IsTrue(item.SenderName == "display name of sender");
                            Assert.IsTrue(item.Subject.Contains("test"));
                            Assert.IsTrue(item.Body.Contains("text you need to look for"));
                        }
                        else
                        {
                            Console.WriteLine("item " + i + " is not a mail or report item.  have no use for it");
                        }
                    }
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                ns = null;
                app = null;
                inboxFolder = null;
            }
        }
2
  • Can we use the same code using Java? I am badly stuck. Please help I need to check in outlook if mail is received or not and then parse and click on the provided link to accept or reject the request. Please help Commented May 21, 2013 at 9:55
  • @user2341727 My experience has been that reading/writing to Exchange using c# is exactly 4100 times faster than using Java. I'm a Java stack guy through and through, but when you need to talk to Exchange, using c# just "works". You could always write a small c# app or service and call that with your java code, though.
    – corsiKa
    Commented May 22, 2013 at 15:22
2

I think this is related to this question which suggests a SMTP pretend server:

Testing email generation

but I suspect this would need to be extended to 'know' about certain emails and be able to respond to [email protected] in the correct way.

1
  • That's correct. I would need to be able to read the message in the inbox. Commented Jul 8, 2011 at 12:47
2

You could use a test email account and access it using an IMAP/POP client?

What I do in my Django apps is I set the email backend to a file based one for testing. Emails are then not sent to the addresses but are stored in individual log files. I can then test the content of these files.

There are two similar but different scenarios here. One is testing email delivery (this is dependent on your provider) and the other is testing the content of email being sent from your application.

1
  • Yet to be able to get non-outlook clients to work. As for the log file, it is a great idea, however, our business sponsors also need to be able to see it work. Currently, for any email addresses that it has rules to always send with, we're using the same local, but different domain. It is a really good idea though. Commented Jul 8, 2011 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.