The problem with doing this, is that a mesh can use several materials. The way this is split up is by each mesh having one or more components, that correspond to a specific shading group and hence material.
Assume you have a MObject of type MFn::kMesh
MFnMesh fnMesh(mesh);
MObjectArray shadingGroups;
MObjectArray components;
fnMesh.getConnectedSetsAndMembers(0 /*instance number*/, shadingGroups, components, 1 /*renderableSetsOnly*/ );
This function returns a list of sets of which the mesh is a member. By setting the last parameter to 1, this is limited to renderable sets, i.e. shading groups. The third parameter, returns the associated component (a list of polygons). The first parameter is incase the geometry is instanced.
e.g. shadingGroups = {"lambertSG1", "lambertSG2"}
components = {0:20, 21:40}
So polygons 0 to 20, refer to shading lambertSG1 and polygons 21 to 40 have been assigned to lambertSG2. Now you need to iterate over the mesh and a specific component:
MItMeshPolygon polyIter(dagPathMesh, component, &status);
Finally in reconstructing the mesh, you should note that the polyIter gives you back a series of triangle fans, but that's another story...
N.B. One last note, the components MObjectArray can contain element of either type kMeshPolygonComponent (as above) or kInvalid. Typically this occurs when a mesh has only one material, so there is no need for a componenting.