How to update SMTP test server settings based on individual site’s settings

   —   
This article describes how to dynamically update SMTP test server settings in the Site Manager / Administration / System / E-mail section, based on SMTP server settings for individual web sites, as defined in Settings / System / E-mails section.
Kentico CMS allows you to set different SMTP server settings for individual web sites (or globally) in the Site Manager / Settings / <web site> / System / E-mails section. However, if you want to test sending e-mails in the Site Manager / Administration / System / E-mail section, there is only one pre-filled SMTP server and corresponding credentials.

 By default, you cannot switch to another SMTP server setting, as defined for particular web sites. Currently, you have to update these credentials manually, and these changes are not saved when you re-open the E-mail section again. The respective control simply uses CMSContext.CurrentSite and its credentials for the initial SMTP server settings.

Imagine that you want to be able to choose which web site’s settings should be pre-filled, as shown on the following screenshot:


Let’s see how this functionality can be achieved.  Here are the two files which need to be customized:

~\CMSModules\System\System_Email.aspx
~\CMSModules\System\System_Email.aspx.cs

In markup, we can define the required controls and their layout looks like this:
<table style="border: 1px solid black;"> <tr> <td> <asp:Label runat="server" ID="lSites" Text="Choose web site to load SMTP settings from:"></asp:Label> </td> <td> <asp:DropDownList runat="server" ID="ddlSites" AutoPostBack="True" onselectedindexchanged="ddlSites_SelectedIndexChanged"> </asp:DropDownList> </td> </tr> </table>

In the code-behind, the following changes must be done:

1. Add these references:
using CMS.SiteProvider;
using System.Data;

2. In the Page_Load method, update “if (!RequestHelper.IsPostBack())“ logic like this:
if (!RequestHelper.IsPostBack()) { //Get number of sites DataSet ds = CMS.SiteProvider.SiteInfoProvider.GetAllSites(); int iSitesCount = ds.Tables[0].Rows.Count; //If zero, hide controls if (ValidationHelper.GetInteger(iSitesCount, 0) == 0) { lSites.Visible = false; ddlSites.Visible = false; } else { //Prefill drop-down list ddlSites.DataSource = ds.Tables[0]; ddlSites.DataTextField = "SiteDisplayName"; ddlSites.DataValueField = "SiteName"; ddlSites.DataBind(); } // Fill SMTP fields with the default server data if (CMSContext.CurrentSite != null) { string siteName = CMSContext.CurrentSiteName; txtServer.Text = EmailHelper.Settings.ServerName(siteName); txtUserName.Text = EmailHelper.Settings.ServerUserName(siteName); txtPassword.Text = EmailHelper.Settings.ServerPassword(siteName); txtPassword.Attributes.Add("value", txtPassword.Text); chkSSL.Checked = EmailHelper.Settings.ServerUseSSL(siteName); } }

3. Add the following method right into the CMSModules_System_System_Email class:
// Update SMTP server settings by selected site’s settings protected void ddlSites_SelectedIndexChanged(object sender, EventArgs e) { string siteName = ddlSites.SelectedValue; txtServer.Text = EmailHelper.Settings.ServerName(siteName); txtUserName.Text = EmailHelper.Settings.ServerUserName(siteName); txtPassword.Text = EmailHelper.Settings.ServerPassword(siteName); txtPassword.Attributes.Add("value", txtPassword.Text); chkSSL.Checked = EmailHelper.Settings.ServerUseSSL(siteName); }

Please note – this is just sample code (although fully functional) and you may want to update it as per individual needs.
-rm-


See also: SMTP server configuration

Applies to: Kentico CMS 7.x
Share this article on   LinkedIn