次の方法で共有


Enumerating fields for CDOSYS, System.Web.Mail and System.Net.Mail

There are times when you may want to enumerate the fields set on a message while debugging issues with CDOSYS, System.Web.Mail, or System.Net.Mail.  Below is sample code which will enumerate these fields.  As you can see, there are some considerable differenences between how enumeration is done.  

CDOSYS:
-------

    int iCount = 0;

    for (iCount = 0; iCount < oMsg.Fields.Count; iCount++)
    {
        if (oMsg.Fields[iCount] != null)
        {
            if (oMsg.Fields[iCount].Value != null)
                Console.WriteLine(iCount.ToString() + "   " + oMsg.Fields[iCount].Name + ":  " + oMsg.Fields[iCount].Value.ToString());
            else
                Console.WriteLine(iCount.ToString() + "   " + oMsg.Fields[iCount].Name + ":  " + "<null>");
        }
        else
        {
            Console.WriteLine(iCount.ToString() + "   " + "Null Field");
        }
    } 

 

System.Web.Mail:
----------------

    using System.Collections;

    . . .

    IDictionaryEnumerator e = (IDictionaryEnumerator)oMsg.Fields.Keys.GetEnumerator();
    while (e.MoveNext())
    {

        if (e.Value != null)
            Console.WriteLine(e.Key + ":  " + e.Value.ToString());
        else
            Console.WriteLine(e.Key + ":  " + "<null>");
    }

System.Net.Mail
----------------

    string[] sKeys = oMsg.Headers.AllKeys;

    foreach (string sKey in sKeys)
    {
        if (oMsg.Headers[sKey] != null)
            Console.WriteLine(sKey + ":  " + oMsg.Headers[sKey]);
        else
            Console.WriteLine(sKey + ":  " + "<null>");
    }

Comments