Query SMTP settings when sending email

You can query the Relativity database for SMTP server settings by using the Relativity API Helpers from an extensibility point. Use this functionality when sending email from agents, custom pages, or event handlers in conjunction with the .NET mail interface as illustrated in the following code sample.

Note: This code sample uses the IInstanceSettingsBundle interface available in RelativityOne 9.6.110.36 and above.

using kCura.Agent;
using Relativity.API;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;

namespace SendRelativityEmail
{
    [System.Runtime.InteropServices.Guid("ad664098-f186-4b4d-9219-728f82a5fa15")]
    [kCura.Agent.CustomAttributes.Name("Send Relativity Email")] 
    public class SendRelativityEmail : AgentBase
    {
        static string smtpPasswordValue;
        static int smtpPortValue;
        static string smtpUserValue;
        static string smtpServerValue;

        public override void Execute()
        {
            SMTPSetting smtpPassword = new SMTPSetting { Section = "kCura.Notification", Name = "SMTPPassword" };
            SMTPSetting smtpPort = new SMTPSetting { Section = "kCura.Notification", Name = "SMTPPort" };
            SMTPSetting smtpServer = new SMTPSetting { Section = "kCura.Notification", Name = "SMTPServer" };
            SMTPSetting smtpUser = new SMTPSetting { Section = "kCura.Notification", Name = "SMTPUserName" };

            List<SMTPSetting> smtpSettings = new List<SMTPSetting> { smtpPort, smtpServer, smtpUser, smtpPassword };

            IInstanceSettingsBundle instanceSettingsBundle = this.Helper.GetInstanceSettingBundle();

            foreach (var smtpInstanceSettingSingle in smtpSettings)
            {
                RaiseMessage("Beginning query", 1);
                try
                {
                    GetSMTPValue(smtpInstanceSettingSingle.Name, smtpInstanceSettingSingle, instanceSettingsBundle);
                }
                catch (Exception ex)
                {
                    RaiseError("Failed to pull data values", ex.Message + "\r\n " + ex.StackTrace);
                }
            }
            try
            {
                string toAddress = "ToAddress@Email.Com";
                string fromAddress = "NoReply@relativity.one";
                string subject = "This is only a test!";

                SendEmail(toAddress, fromAddress, subject);
                RaiseMessage("Sending email now...", 1);

            }
           catch (Exception ex)
            {
                RaiseError("Failed to send E-mail! ", ex.Message + "\r\n " + ex.StackTrace);
            }
        }

        private static void SendEmail(string emailTo, string emailFrom, string emailSubject)
        {

            MailMessage emailOut = new MailMessage(emailFrom, emailTo);
            SmtpClient client = new SmtpClient();
            client.Port = smtpPortValue;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Host = smtpServerValue;
            emailOut.Subject = emailSubject;
            emailOut.IsBodyHtml = true;
            client.Credentials = new NetworkCredential(smtpUserValue, smtpPasswordValue);
            client.Send(emailOut);
        }

        private static void GetSMTPValue(string settingName, SMTPSetting smtpInstanceSettingSingle, IInstanceSettingsBundle instanceSettingsBundle)
        {
            switch (settingName)
            {
                case "SMTPPassword":
                    var singleSettingValuePass = instanceSettingsBundle.GetStringAsync(smtpInstanceSettingSingle.Section, smtpInstanceSettingSingle.Name);
                    smtpPasswordValue = singleSettingValuePass.Result;
                    break;

                case "SMTPPort":
                    int singleSettingValuePort = Convert.ToInt32(instanceSettingsBundle.GetUIntAsync(smtpInstanceSettingSingle.Section, smtpInstanceSettingSingle.Name).Result.Value);
                    smtpPortValue = singleSettingValuePort;
                    break;

                case "SMTPUserName":
                    var singleSettingValueUser = instanceSettingsBundle.GetStringAsync(smtpInstanceSettingSingle.Section, smtpInstanceSettingSingle.Name);
                    smtpUserValue = singleSettingValueUser.Result;
                    break;

                case "SMTPServer":
                    var singleSettingValueServer = instanceSettingsBundle.GetStringAsync(smtpInstanceSettingSingle.Section, smtpInstanceSettingSingle.Name);
                    smtpServerValue = singleSettingValueServer.Result;
                    break;
            }
        }

        public override string Name
        {
            get
            {
                return "Send Relativity Email";
            }
        }
    }
    
    public class SMTPSetting
    {
        public string Section { get; set; }
        public string Name { get; set; }
    }
}