Send Emails through Smart Integration Direct Connections
Prior to using this business rule, you must have your email server configured. You must establish a direct connection before sending email. See Single Web API Connection for more information on setting up an initial direct connection. The following business rule can send email from an Extender Business rule.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.IO;
using System.Linq;
using Microsoft.CSharp;
using OneStream.Finance.Database;
using OneStream.Finance.Engine;
using OneStream.Shared.Common;
using OneStream.Shared.Database;
using OneStream.Shared.Engine;
using OneStream.Shared.Wcf;
using OneStream.Stage.Database;
using OneStream.Stage.Engine;
using System.Net.Mail;
using System.Net;
using System.Net.Security;
using System.Text.RegularExpressions;
using System.Security.Cryptography.X509Certificates;
namespace OneStream.BusinessRule.Extender.smtp_direct_test
{
public class MainClass
{
public SessionInfo SI;
private const string smtpHostName = "smtp.azurecomm.net"; // expected name to match the cert.
public object Main(SessionInfo si, BRGlobals globals, object api, ExtenderArgs args)
{
var client = new SmtpClient();
var email = new MailMessage();
try
{
SI = si;
// Add custom validation callback to look for expected cert (Host will be localhost, which causes this to fail without a custom callback)
ServicePointManager.ServerCertificateValidationCallback += ValidationCallback;
client.UseDefaultCredentials = false;
client.Port = 20542;
client.Host = "localhost";
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("<UserName>", "<Password>");
email.From = new MailAddress("DoNotReply@domain.com");
email.To.Add("test@onestreamsoftware.com");
email.Subject = "Test from SIC Gateway";
email.IsBodyHtml = false;
email.Body = "Forwarded test from SIC Gateway";
client.Send(email);
return null;
}
catch (Exception ex)
{
throw ErrorHandler.LogWrite(si, new XFException(si, ex));
}
finally
{
// Remove the custom ValidationCallback. It's recommended to remove this before any other network calls.
ServicePointManager.ServerCertificateValidationCallback -= ValidationCallback;
email.Dispose();
client.Dispose();
}
}
public bool ValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
var policyErrors = (sslPolicyErrors as SslPolicyErrors?) ?? SslPolicyErrors.None;
var certSubject = certificate?.Subject ?? string.Empty;
var certName = string.Empty;
// Extract the certName from the certSubject
string namePattern = @"CN=([^,]+)";
var match = Regex.Match(certSubject, namePattern);
if (match.Success)
{
certName = match.Groups[1].Value;
}
if (
(policyErrors == SslPolicyErrors.RemoteCertificateNameMismatch || policyErrors == SslPolicyErrors.None)
&& certName == smtpHostName)
{
// verify the certName matches the expected smtpHostName. No other SslPolicyErrors should be present.
return true;
}
else
{
return false;
}
}
}
}