Sending an email to a contractor's contact
This is an example of a script for sending an email via a built-in ELMA service from a business process to the contacts of the selected contractor.
context.Client - Client variable of the Contractor type
context.prop_att – Attachments variable of the Attachment type, with the List link type, allows attaching several files to an email.
context.Comment - Comment variable of the Text type
Include the following 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 the client's contacts
var knt = context.Client.Contacts.ToList();
foreach (var i in knt)
{
mailto = i.Email.First().ToString(); //get the email
//Log in to the SMTP server
if (mailto != null)
{
var smtp_st = new SmtpSettings("mail.elewise.com", 25, "login", "password"); //Specify mail server parameters and user login information
var msg = new MailMessage("from@mail.com", mailto, "Message subject", "Message body"); //Generate the email, specify the sender address, the recepient address, message 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 email sending settings will be taken from Administration – System Settings – Outgoing Mail Settings
// If you use this method, comment the lines 39 and 43
}
else
{
context.Comment += "Contact email address" + i.Name + " is not specified, the email was not sent\n";
}
}
Creating a similar script using PublicAPI
Documentation on PublicAPI is available here
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 the client's contacts
var knt = context.Client.Contacts.ToList();
foreach (var i in knt)
{
mailto = i.Email.First().ToString(); //get the email
//Log in to the SMTP server
if (mailto != null)
{
var smtp_st = new SmtpSettings("mail.elewise.com", 25, "login", "password"); //Specify mail server parameters and user login information
var msg = new MailMessage("from@mail.com", mailto, "Message subject", "Message body"); //Generate the email, specify the sender address, the recepient address, message 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 email sending settings will be taken from Administration – System Settings – Outgoing Mail Settings
// If you use this method, comment the lines 39 and 43
}
else
{
context.Comment += "Contact email address" + i.Name + " is not specified, the email was not sent\n";
}
}
You can also use SmtpSettings without authentication (if the mail server allows sending email without authentication). In this case, you specify only the server address and the port for sending messages:
var smtp_st = new SmtpSettings("mail.elewise.com", 25);