Are you developing a .NET application using C# and need the ability to send custom emails? Look no further. One of the beauties of .NET is the bounty of (often) free to use libraries available via Nuget. My favorite for sending emails is MailKit which is provided under the MIT License. We will start with some prerequisites and then get into a code sample:
Make sure that connection details for SMTP are stored securely and ideally easy to update when needed. For M-Files, this means putting them in configuration and masking the password field. The below code sample refers to a Configuration class, but you can simply replace it with where you have stored your SMTP settings as needed:
using System;
using MimeKit;
using MailKit.Net.Smtp;
namespace local_testing
{
static class SMTP
{
public static Configuration Configuration;
public static bool TrySendSmtpMessage(string subject, string body, string[] toEmails, string[] ccEmails, string[] bccEmails, string[] filepaths)
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress(
Configuration.SMTP_FROM_NAME,
Configuration.SMTP_FROM_EMAIL // providing an invalid address can result in errors and/or spam alerts
));
foreach (string email in toEmails)
{ // First param can be set to recipient's name
message.To.Add(new MailboxAddress(email, email));
};
foreach (string email in ccEmails)
{
message.Cc.Add(new MailboxAddress(email, email));
}
foreach (string email in bccEmails)
{
message.Bcc.Add(new MailboxAddress(email, email));
}
message.Subject = subject;
var builder = new BodyBuilder
{
HtmlBody = body
};
ContentType ct = new ContentType("application", "octet-stream");
foreach (string path in filepaths)
{ // Note that you may run into errors trying to include more than about 10MB of attachments depending on your mail server
builder.Attachments.Add(path, ct);
}
message.Body = builder.ToMessageBody();
try
{
using (SmtpClient client = GetClient())
{
client.Send(message);
client.Disconnect(true);
}
return true;
}
catch (Exception e)
{
// Do something to handle errors
throw e;
}
finally
{
// Clean up temporary file downloads if needed
}
}
private static SmtpClient GetClient()
{
var client = new SmtpClient();
client.Connect(Configuration.SMTP_HOST, Configuration.SMTP_PORT);
if (!Configuration.USE_SIMPLE_MAIL)
{ // Simple mail may be used for on-prem servers with necessary security in place but most servers will require authentication
client.Authenticate(Configuration.SMTP_USERNAME, Configuration.SMTP_PASSWORD);
}
return client;
}
}
}