Saturday, February 9, 2013

Configure Niagara Ax to send Email


Configure Niagara Ax to send Email


The below steps explains how to configure the email service in Naigara Ax station, using its workbench. And below you will find the code to use the baja Email api to be able to send email in Niagara.
Navigate to BEmailService service under Station
Double click on the EmailService, you will find the Email Account Manager.
Click New Button
Provide the Outgoing Account information including the smtp server and port. And click ok.

email1


Now right click on the added account manager and choose Actions->Send
Now provide your email id information in from and to address fields. And click Ok.
In your station spy if you have enabled trace for module ‘email’. Then you can see the message below in station console.

 Code to Send Email

public void doSendEmail(){
 BEmailService bemailservice =
 (BEmailService)Sys.getService(BEmailService.TYPE);
 bemailservice.lease(2);
 BOutgoingAccount outGoingAccount =
 (BOutgoingAccount)bemailservice.getDefaultOutgoingAccount();
 if(outGoingAccount == null) {
  System.out.println("Email Service not configured")
  return;
 }
 BEmail mail = new BEmail();
 mail.setFrom(BEmailAddress.make("My Name", "myname@domain.com"));
 //Add to address
 List<BEmailAddress> toEmailList = new ArrayList<BEmailAddress>();
 toEmailList.add(BEmailAddress.make("Your Name","yourname@domain.com"));
 BEmailAddressList listEmail = BEmailAddressList.make(
    toEmailList.toArray(new BEmailAddress[toEmailList.size()]));
 mail.setTo(listEmail);
 mail.setDate(BAbsTime.now());
 mail.setSubject("Test Subject");
 // Add attachment
 BEmailPartList part = new BEmailPartList();
 //part.addAttachment(BOrd.make("ord to attachement file"));
 mail.setAttachments(part);
 //Add body
 StringBuffer body = new StringBuffer();
 body.append("This is a test mail ");
 mail.setBody(new BTextPart(body.toString()));
//send mail
 outGoingAccount.send(mail);
}

Detailed description to configure Email can be found here.