Programmatically Using the Outgoing SMTP Server from Central Admin

Had a need today to programmatically get the outgoing SMTP server that is setup in the Central Administrator. Luckily, I found a sample of how to do this with WSS V3 from the folks at Covelle Corner. Worked like a champ for me!

They use the following code (obvious credit to Covelle Corner for this):

///
/// Returns SharePoint smtp server
///
///
private string GetSmtpServer()
{
SPWebApplicationCollection spWebApplicationCollection = SPWebService.ContentService.WebApplications;
SPOutboundMailServiceInstance smtpServer = new SPOutboundMailServiceInstance();

if (spWebApplicationCollection != null)
{
foreach (SPWebApplication spWebApplication in spWebApplicationCollection) {
smtpServer = spWebApplication.OutboundMailServiceInstance;
return smtpServer.Server.Address;
}
}
return string.Empty;
}