How to create an ad-hoc email and send it using Acumatica

久未见 提交于 2020-01-04 17:09:09

问题


In Acumatica you can use notifications to automate some emails. In my scenario, we are creating a process that will at non-specific (non-set) times need to send an email when a specific condition is triggered, such as an employee needs to know they need to do something.

We are building this logic into the system and I am looking for a code sample of how to send the email when this happens.

We will be using an email template, but need to accomplish the feat in code. I would hope there should be some kind of acumatica email class where we could just call it and pass the required info something like:

PX.Common.email.Send(params)...

Any example code would be appreciated.


回答1:


Here I want to present shorter version of sending email:

using PX.Objects.EP;
using PX.Data.EP;
**...**

var sender = new NotificationGenerator
{

    To = "someone@example.com",
    Subject = $"Subject information {DateTime.Now:d}",
    Body = "Body of message",
    BodyFormat = EmailFormatListAttribute.Text
};

sender.Send();



回答2:


It turns out that there is a KB article that gives an example of how to do this. for our scenario, Here is a more recent version of the code that has been verified to send an email using either of 2 email templates.

    private void mSendEmail(string toEmail, int? emailTemplateID, long? noteid, string source, string toDisplayName)
    {
        bool sent = false;
        string sError = "Failed to send E-mail.";
        POOrder porec = poOrder.Select(noteid);
        EPExpenseClaim eprec = epExpense.Select(noteid);

        try
        {
            Notification rowNotification = PXSelect<Notification,
                                              Where<Notification.notificationID, Equal<Required<Notification.notificationID>>>>.Select(this, emailTemplateID);

            if (rowNotification == null)
                throw new PXException(PXMessages.Localize("Notification Template for Escalation is not specified."));

            if (String.IsNullOrEmpty(toEmail))
                throw new PXException(PXMessages.Localize("E-mail is not specified for Escalation Employee. Name=[" + toDisplayName +"]"));
            if (source == "PO")
            {
                var sender = TemplateNotificationGenerator.Create(porec, rowNotification.NotificationID.Value);
                sender.MailAccountId = rowNotification.NFrom.HasValue ?
                                       rowNotification.NFrom.Value :
                                       PX.Data.EP.MailAccountManager.DefaultMailAccountID;

                sender.To = toEmail;
                IEnumerable<EPActivity> epActivityArray = sender.Send();
                if (epActivityArray.Count() > 0)
                { sent = true; }
            }
            if (source == "EP")
            {
                var sender = TemplateNotificationGenerator.Create(eprec, rowNotification.NotificationID.Value);
                sender.MailAccountId = rowNotification.NFrom.HasValue ?
                                       rowNotification.NFrom.Value :
                                       PX.Data.EP.MailAccountManager.DefaultMailAccountID;

                sender.To = toEmail;
                IEnumerable<EPActivity> epActivityArray = sender.Send();
                if (epActivityArray.Count() > 0)
                { sent = true; }
            }
        }
        catch (Exception Err)
        {
            sent = false;
            sError = Err.Message;
        }

        if (!sent)
            throw new PXException(PXMessages.Localize(sError));

    }


来源:https://stackoverflow.com/questions/29261668/how-to-create-an-ad-hoc-email-and-send-it-using-acumatica

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!