Finding the SMTP server address through code in SharePoint 2010

If anyone knows a better way then let me know.

string smtp = string.Empty;

SPFarm.Local.Servers.ForEach(s => s.ServiceInstances.ForEach(i =>
{
if (i is SPOutboundMailServiceInstance)
{
smtp = s.Address;
}
}));

return smtp;

I use a ForEach extension which I've included below

public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)

{

foreach (T item in enumerable)

{

action(item);

}

}

You may also like: