Sometimes it might be needed to get the Web Application outgoing emails settings programatically.
The below code snippet gives all the settings including Outbound SMTP server, From address, Reply-to address and Character set information.
GET:
The below code snippet gives all the settings including Outbound SMTP server, From address, Reply-to address and Character set information.
using (SPSite site = new SPSite("http://siteurl")) { SPWebApplication webApp = site.WebApplication; string outboundSMTPServer = webApp.OutboundMailServiceInstance.Parent.Name; string fromAddress = webApp.OutboundMailSenderAddress; string replyToAddress = webApp.OutboundMailReplyToAddress; int characterSet = webApp.OutboundMailCodePage; }Web application outgoing email settings can also be updated programatically as below.
using (SPSite site = new SPSite("http://siteurl")) { string outboundSMTPServer = "Outbound SMTP server"; string fromAddress = "From address"; string replyToAddress = "Reply-to address"; int characterSet = 65001; //Character set code SPWebApplication webApp = site.WebApplication; webApp.UpdateMailSettings(outboundSMTPServer,fromAddress,replyToAddress,characterSet); }Similarly we can get & set the Global outgoing email settings as below.
GET:
var globalAdmin = SPAdministrationWebApplication.Local; var outboundSMTPServer = globalAdmin.OutboundMailServiceInstance.Parent.Name; var fromAddress = globalAdmin.OutboundMailSenderAddress; var replyToAddress = globalAdmin.OutboundMailReplyToAddress; var characterSet = globalAdmin.OutboundMailCodePage;SET:
string outboundSMTPServer = "Outbound SMTP server"; string fromAddress = "From address"; string replyToAddress = "Reply-to address"; int characterSet = 65001; //Character set code var globalAdmin = SPAdministrationWebApplication.Local; globalAdmin.UpdateMailSettings(outboundSMTPServer, fromAddress, replyToAddress, characterSet);