Develop a NetLogic for sending emails
This NetLogic sends emails to a predefined address.
-
Create the NetLogic:
- In Project view, right-click NetLogic and select New > Runtime NetLogic.
-
Hover-over the NetLogic, select
, and enter EmailSender
-
Double-click the NetLogic.
The external code editor opens.
-
In the code editor, do the following:
-
Replace the existing code with the following code:
using System; using System.Net.Mail; using System.Net; using UAManagedCore; using FTOptix.NetLogic; public class EmailSender : BaseNetLogic { [ExportMethod] public void SendEmail(string replyToAddress, string mailSubject, string mailBody) { if (string.IsNullOrEmpty(replyToAddress) || mailSubject == null || mailBody == null) { Log.Error("EmailSender", "Invalid values for one or more parameters."); return; } var fromAddress = new MailAddress("mail@domain.com", "Name"); // Email Sender var toAddress = new MailAddress("mail@domain.com", "Name"); // Email Receiver // Password for SMTP server authentication if necessary const string fromPassword = "Insert your password here."; var smtpClient = new SmtpClient { // Fill the following lines with your SMTP server info Host = "smtp.domain.com", Port = 587, EnableSsl = true, // Set to true if the server requires SSL. DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(fromAddress.Address, fromPassword) }; var message = new MailMessage() { // Create the message. Subject = mailSubject, Body = mailBody }; // Specify the sender message.From = fromAddress; // Recipient emails // The MailMessage.To property is a collection of emails, so you can add different recipients using: // message.To.Add(new MailAddress(...)); message.To.Add(toAddress); // Add reply-to address message.ReplyToList.Add(replyToAddress); try { // Send email message smtpClient.Send(message); Log.Info("Message " + mailSubject + " sent successfully."); } catch (Exception ex) { // Insert here actions to be performed in case of an error when sending an email Log.Error("Error while sending message " + mailSubject + ", please try again. " + ex.Message); } } }
Tip: In the example, the email entered at run time is added to theReplyToList
property. This property contains a list of addresses that will be automatically added as recipients in the event of a response. TheSend
method ofSmtpClient
is encapsulated within a try/catch construct to handle possible exceptions. -
Change the
var fromAddress
variable value to reflect the sender email. -
Change the
var toAddress
variable value to reflect the recipient email.Tip: In the example, the email is sent to a single recipient, but you can add more recipients by using theTo
property of theMailMessage
class. See MailMessage.To Property (System.Net.Mail) | Microsoft DocsFor example:
mailMessage.To.Add(new MailAddress(...)
-
If needed, provide the password to the email account by modifying
const string fromPassword = "Insert your password here.";
Important: If you use a two-factor authentication to protect your email account, you must generate a password for the application. -
If needed, in the
var smtpClient
variable value, configure theCredentials
data.
-
Replace the existing code with the following code:
- Save the code.