Pregunta

I am trying to have an email being sent based on some events triggered by users in their client. I do not want the email being sent from the client (as this would require us to allow virtually every workstation in the domain to use the SMTP service) but rather from the AOS server.

I thought of creating a class that would extend RunBaseBatch and use SysMailer in it.

Here's what I have so far.

class Batch_Mailer extends RunBaseBatch
{
    str subject;
    str body;
    str fromName;
    str fromAddress;
    str toAddress;
    str smtpServer;

    void new(str _subject, str _body, str _fromName, str _fromAddress, str _toAddress)
    {
        subject = _subject;
        body = _body;
        fromName = _fromName;
        fromAddress = _fromAddress;
        toAddress = _toAddress;
        smtpServer = 'mail.domain.ca';
        super();
    }

    public boolean canGoBatchJournal()
    {
        return true;
    }

    public void run()
    {
        SysMailer mail;
        ;

        super();
        try
        {
            mail = new SysMailer();

            mail.fromAddress(fromAddress, fromName);
            mail.SMTPRelayServer(smtpServer);
            mail.tos().appendAddress(toAddress);
            mail.htmlBody(strfmt(body));
            mail.subject(subject);
           mail.sendMail();
        }
       catch
       {
           //Log something maybe, but nice if the infolog would not pop up... 

       }
    }

}

Here's how I use it:

Batch_Mailer mail;
mail = new Batch_Mailer("Subject.", strfmt("@VDX488", vendTable.AccountNum, curUserId()), "AX Alerts", 
        "AXAlerts@domain.ca", "test.mailbox@domain.ca"

Unfortunately, this seems to run in the client. If I run the code on the dev box VM that has the AOS server on (which can use the SMTP service) the email fires, but not if I run it in the client on my physical box (which is not allowed to use the SMTP service).

I thought extending RunBaseBatch and overriding run would do it, but apparently not. Any ideas ?

I'm also wondering if this approach will fail since I don't think most users can run batches using their accounts... Perhaps I'll have to use impersonation?

Thanks!

¿Fue útil?

Solución

Extending RunBaseBatch doesn't imply that it is always executed on the server tier - where the code is actually executed depends on where the object lives.

Therefore you could ensure that the code is always executed on the server tier by ensuring that such objects are always created there. To accomplish this just create a server static method which you use to create new instances of your class.

Example:

public static server Batch_Mailer newOnServer(
    str _subject, 
    str _body, 
    str _fromName, 
    str _fromAddress, 
    str _toAddress)
{
    ;
    return new Batch_Mailer(_subject, _body, _fromName, _fromAddress, _toAddress);
}

After that you just have to call this static method instead of directly using new:

mail = Batch_Mailer::newOnServer("Subject.", strfmt("@VDX488" ...
mail.run();

Otros consejos

DAXaholic's answer is spot on answer to your question, but perhaps you should consider using the built-in AX framework for sending emails instead of coding up your own method. I'd imagine you will have less problems down the road and an easier upgrade path to 2012+.

See my blog post:

http://alexondax.blogspot.com/2013/09/how-to-properly-send-emails-with-built.html

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top