The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.

Saturday, 23 February 2013

Mail Classes in PeopleSoft

We have two methods in Peoplesoft to send emails from the PeopleSoft system. The first method is using SendMail built-in function. This function is useful if you want to send out quick email without any formatting and less peoplecode. However PeopleSoft has provided a powerful delivered class called mail class which can be used to create content rich mails with effective formatting, attachments and more. The application package will take care of most of the codes required to achieve the objective, however you need to know how to use the application class to send/receive emails which somewhat tricky.

I would like to give a glimpse on how to use this class for your purposes.  There are a set of application classes delivered along with the package. The classes include
·         MCFBodyPart
·         MCFEmail
·         MCFGetMail
·         MCFHeaders
·         MCFInboundEmail
·         MCFMailStore
·         MCFMultiPart
·         MCFMailUtil
·         MCFOutboundEmail
·         MCFPart
·         SMTPSession

The main classes which we need to import for sending emails are MCFBodyPart, MCFOutboundEmail and MCFMultiPart which I will explain in the coming paragraphs. The MCFMailUtil class sometimes needs to be implemented. The main purpose of the class is to access some utilities like encoding/decoding texts, validating email addresses and checking the status of SMTP email server.

To receive the emails and display it in PIA, the main class you need to access is MCFInboundEmail. Since this is a rare case scenario, I will not be explaining the same in this blog.

Let us see how we can send a formatted or rich content email using the mail classes. I will explain the three main classes mentioned earlier in step by step method.

MCF BodyPart Class

This class is used when we need to have attachments or HTML formatting or any other non-standard email methods. In all the cases you need to create an object of this class. Then using the methods and properties of this class we will assign the non-standard content or formatting to this class. This object is then later added as a property to MCFMultiPart Class.

Main Methods

The main methods of this class is...

1.       SetAttachmentContent – Use this method to make the content of the body part from a file.
Syntax: SetAttachmentContent({FilePath | FileURL}, FilePathType, FileName, FileDescr, OverrideContentType, OverrideCharset)

Main Properties

The main properties of the class are…

1.       AttachmentURL – Specify the URL for attachment here.
2.       ContentType – Mention the content format for the body part. Example “text/plain” , “text/html”.
3.       Disposition – This property can hold two values “inline” and “Attachment”. “inline” will display the message in the email body. “Attachment” method will make the content as an attachment to the email.
4.       MultiPart – If you have multiple parts for the same body then assign the multipart object to this property. In that case your text and attachment are ignored.
5.       Text – Specify the email text over here.
MCFMultiPart Class

A multipart class object can hold multiple body part objects you created using MCFBodyPart Class. You will assign the MCFMultiPart objects to Outbound email class and send the mail from Outbound email object class.

Main Methods

The main methods of MCFMultiPart Class is…

1.       AddBodyPart – It can be used to add body part objects created from the BodyPart class to this class object.
Main Property

The main property of MCFMultiPart Class is…

1.       SubType – Used to mention the type of sub objects of the class. The valid values can be alternative, related and mixed.

MCFOutboundEmail Class

This is the one stop class which you need to be accessed when your sole intension is for sending emails. This class internally inherits the MCFEmail class which is a sub of MCFPart class. So in effect, all the required properties and methods for sending emails are available from the Outbound email class. This class holds all the generic values like To Address, CC, BCC, Subject etc.

Main Methods

The main methods of this class are…

1.       AddAttachment – Use this method to add an attachment to the class. For multiple attachments and other content you can also create MCFBodyPart objects explained earlier and assign it to MCFMultiPart object which again needs to be assigned to MCFOutboundEmail class.
2.       AddHeader – Use this method to add headers to email.
3.       Send – Use this method to send the email.

Main Properties

The main properties of this class are…

1.       BCC – BCC mail addresses
2.       Bounce To – Email address to bounce if the delivery failed.
3.       CC – CC mail addresses
4.       Charset – The character set used for mail or attachment
5.       Content Language – Used to set language of the email. Multiple languages should be separated by comma.
6.       ContentType – Specify the content type of the email if the type is different from the default one.
7.       Importance – To set the importance value of the email. The values can low, normal and high.
8.       MultiPart – You will assign the MCFMultiPart object created in previous steps to this value.
9.       Priority – To set the priority of the email.  The values range from 1-5 with one having highest and 5 having lowest priority.
10.   Recipients – The main addresses to whom this mail should be sent.
11.   ReplyTo – Specify the reply to email address.
12.   Sender – Use it to mention the email address of the sender.
13.   StatusNotifyOptions – Use this to enable notification to the sender.
14.   Text – Specify email text of the email.


You might be confused with the class methods and properties mentioned above. The picture will become clearer if we run through an example from PeopleBooks.

The typical steps involve
1.       Import the Mail classes
2.       Create corresponding objects
3.       Assign contents and attachments to MCFBodyPart class objects.
4.       Assign the MCFBodyPart objects to MCFMultiPart object
5.       Assign the MCFMultiPart object to MCFOutboundEmail object.
6.       Invoke the send method of MCFOutboundEmail class.
/*  Import Mail Classes  */
import PT_MCF_MAIL:*;

/*  Create MCFOutboundEmail class object  */

Local PT_MCF_MAIL:MCFOutboundEmail &email =
create PT_MCF_MAIL:MCFOutboundEmail();

/*  Create MCFBodyPart class object  */
Local PT_MCF_MAIL:MCFBodyPart &text = create PT_MCF_MAIL:MCFBodyPart();
/*  Assign Text property  */
&text.Text = "Hi There";

/* Create Second body part object*/

Local PT_MCF_MAIL:MCFBodyPart &html = create PT_MCF_MAIL:MCFBodyPart();
/*  Assign Text property  for object 2 and make it html type*/

&html.Text =
"<html><BODY><H1>EMail test with HTML content</H1><b>Hi There</b>" |
"<A href=''>Check this out!</A>" |
&html.ContentType = "text/html";

Local string &TestName = "Text and its alternate html body";

   /*  Create MCFMultiPart class object  */

Local PT_MCF_MAIL:MCFMultipart &mp = create PT_MCF_MAIL:MCFMultipart();
/*  Set the SubType property  */

&mp.SubType = "alternative; differences=Content-type";

/*  Add the body parts created earlier to the multi part object  */  

/*  Set the multi part object to the Outbound email object  */ 
&email.MultiPart = &mp;

/*  Set the required properties for the Outbound email class object*/

&email.From = &FromAddress;
&email.Recipients = &ToList;
&email.Subject = &Subject;

/*  Invoke the send method of OutboundEmail class object  */

Local integer &res = &email.Send();

Local boolean &done;

/*  Capture the return code of send and do further processing  */

Evaluate &resp
   When %ObEmail_Delivered
      /* every thing ok */
      &done = True;
   When %ObEmail_NotDelivered
/*-- Check &email.InvalidAddresses, &email.ValidSentAddresses
and &email.ValidUnsentAddresses */
      &done = False;
   When %ObEmail_PartiallyDelivered
/* Check &email.InvalidAddresses, &email.ValidSentAddresses
and &email.ValidUnsentAddresses; */
      &done = True;
   When %ObEmail_FailedBeforeSending
/* Get the Message Set Number, message number;
Or just get the formatted messages from &email.ErrorDescription,
      &done = False;

No comments:

Post a Comment