Develop a NetLogic for sending emails
This NetLogic sends emails to a predefined address.
- Create the NetLogic:
- InProject view, right-clickNetLogicand select .
- Hover-over the NetLogic, select , and enterEmailSender
- 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 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); } } }MANCIA: In the example, the email entered at run time is added to theReplyToListproperty. This property contains a list of addresses that will be automatically added as recipients in the event of a response. TheSendmethod ofSmtpClientis encapsulated within a try/catch construct to handle possible exceptions.
- Change thevar fromAddressvariable value to reflect the sender email.
- Change thevar toAddressvariable value to reflect the recipient email.MANCIA: In the example, the email is sent to a single recipient, but you can add more recipients by using theToproperty of theMailMessageclass. 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 modifyingconst string fromPassword = "Insert your password here.";IMPORTANTE: If you use a two-factor authentication to protect your email account, you need to generate a password for the application.
- If needed, in thevar smtpClientvariable value, configure theCredentialsdata.
- Save the code.
Fornire un feedback