Selecting values from an object

This article provides an example of a script for sending an email via ELMA built-in service from a business process to the contacts of the selected contractor.

Context variables:

context.Client - Client variable of the Contractor type;

context.prop_att – Attachment variable of the Attachment type with the List link type; allows attaching several files to the sent email;

context.Comment - Comment variable of the Text type.

Script with PublicAPI

Note
Documentation on PublicAPI is available here
Attention
The script below is relevant for ELMA up to 3.12.1 inclusive.

Namespaces:

using EleWise.ELMA.API;
using EleWise.ELMA.Messaging.Email;
using System.Net.Mail;

Script text:

string mailto;
 
//Attach files
 
var ft = new List<MailMessageFile>();
 
foreach (var newatt in context.prop_att.ToList())
{
 
    var ft2 = new MailMessageFile();
 
     ft2.FileBody = System.IO.File.ReadAllBytes(newatt.File.ContentFilePath);
 
    ft2.FileName = newatt.File.Name;
 
    ft.Add(ft2);
 
}
 
//get client's contacts
 
var knt = context.Client.Contacts.ToList();
 
foreach (var i in knt)
{
 
    mailto = i.Email.First().ToString(); //get the contact email
 
    //Authentication on SMTP server
 
    if (mailto != null)
    {
        var smtp_st = new SmtpSettings("mail.elewise.com", 25, "login", "password"); //Specify the mail server parameters and user authentication information
 
        var msg = new MailMessage("from@mail.com", mailto, "Email subject", "Email body"); //Specify the sender's address, the recipient's address, email subject and body
 
        PublicAPI.Services.Email.SendMessage(smtp_st, msg, ft); // Send the email using the mail server parameters, specified in smtp_st
 
        //PublicAPI.Services.Email.SendMessage(msg, ft, null); // Send the email (can be used instead of line 43). In this case, the settings from Administration - System - Outgoing Mail Settings will be used for sending the email
        // If you use this method, comment out the lines 39 and 43
    }
    else
    {
        context.Comment += "Contact email address" + i.Name + "  not specified, the email was not sent\n";
    }
}

Script without PublicAPI

Namespaces:

using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using EleWise.ELMA.Messaging.Email;
using EleWise.ELMA.Services;
using System.Web.Mvc;
using EleWise.ELMA.CRM;

Script text:

string mailto;
   
var emailSender = Locator.GetService<EmailSender>();
   
//Attach files
   
var ft = new List<MailMessageFile>();
   
foreach (var newatt in context.prop_att.ToList())
   
{
   
    var ft2 = new MailMessageFile();
   
    ft2.FileBody = System.IO.File.ReadAllBytes(newatt.File.ContentFilePath);
   
    ft2.FileName = newatt.File.Name;
   
    ft.Add(ft2);
   
}
   
//get client's contacts
   
var knt = context.Client.Contacts.ToList();
   
foreach (var i in knt)
   
{
   
    mailto = i.Email.First().ToString(); //получаем email контакта
   
    //Authentication on SMTP server
   
    if (mailto != null)
   
    {
   
        var smtp_st = new SmtpSettings("mail.elewise.com", 25, "login", "password"); //Specify the mail server parameters and user authentication information
   
        var msg = new MailMessage("from@mail.com", mailto, "Тема письма", "Тело сообщения"); //Specify the sender's address, the recipient's address, email subject and body
   
        emailSender.SendMessage(smtp_st, msg, ft); // Send the email using the mail server parameters, specified in smtp_st
          
        //emailSender.SendMessage(msg, ft); // Send the email (can be used instead of line 43). In this case, the settings from Administration - System - Outgoing Mail Settings will be used for sending the email
                                          // If you use this method, comment out the lines 39 and 43
   
    }
   
    else
       
    {
   
        context.Comment += "Contact email address" + i.Name + "  not specified, the email was not sent\n";
   
    }
   
}

It is also possible to use SmtpSettings without authentication (if the server allows sending email without authentication). In this case, you specify only the server address and port for sending an email:

var smtp_st = new SmtpSettings("mail.elewise.com", 25);