Develop a NetLogic for sending emails

This NetLogic sends emails to a predefined address.
  1. Create the NetLogic:
    1. In
      Project view
      , right-click
      NetLogic
      and select
      New
      Runtime NetLogic
      .
    2. Hover-over the NetLogic, select
      Edit
      , and enter
      EmailSender
    3. Double-click the NetLogic.
      The external code editor opens.
  2. In the code editor, do the following:
    1. 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 required 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 the
      ReplyToList
      property. This property contains a list of addresses that will be automatically added as recipients in the event of a response. The
      Send
      method of
      SmtpClient
      is encapsulated within a try/catch construct to handle possible exceptions.
    2. Change the
      var fromAddress
      variable value to reflect the sender email.
    3. 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 the
      To
      property of the
      MailMessage
      class. See MailMessage.To Property (System.Net.Mail) | Microsoft Docs
      For example:
      mailMessage.To.Add(new MailAddress(...)
    4. 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 need to generate a password for the application.
    5. If needed, in the
      var smtpClient
      variable value, configure the
      Credentials
      data.
  3. Save the code.
Provide Feedback
Have questions or feedback about this documentation? Please submit your feedback here.