Sending Emails in ASP COM API

1 minute read time.


Sage CRM offers a very simple MessageBlock object that you can use for your own pages. This object can either be used automatically to send a message without an interface prompt or it can create a form allowing the details of the email message to be completed.

An example use of the MessageBlock that sends the email directly without prompting the users is shown below:

var myMailObject = CRM.GetBlock("messageblock");
with (myMailObject)
{
DisplayForm = false;
mSubject = "Subject Here";
mBody = "Message Goes here";

AddRecipient("[email protected]","Training","TO");
Mode = 2;
Response.Write(Execute());
if (mSentOK)
{
Response.Write("Email Sent")
}
else
{
Response.Write("There has been a problem: "+mErrorMessage);
}
}

This can be changed to work within the Table level and Validation scripts.

To create a form that allows the user to complete the message then this code should give you an idea:

//Create a Visible Message Block and Set Properties
var myMailObject = CRM.GetBlock("messageblock");
with (myMailObject)
{
//works with setting made within CRM administration screens
//hide email form;
DisplayForm = true;
mSubject = "Subject Here";
//mBody is Automatically truncated to 160 chars for SMS
mBody = "Message Goes here";
mShowCC = true;
mShowBCC = true;
//TO|CC|BCC are valid recipients
AddRecipient("[email protected]","Training","TO");
AddRecipient("[email protected]","Support","CC");
AddRecipient("[email protected]","Manager","BCC");
Response.Write(Execute());
}

Note: The MessageBlock object however does not offer the ability to add attachments.

To work with attachments in an ASP page then you may want to consider something like the CDONTS object. This will offer the ability to add attachments.

var MyCDONTSMail = CreateObject("CDONTS.NewMail");
MyCDONTSMail.From= "[email protected]";
MyCDONTSMail.To= "[email protected]";
MyCDONTSMail.Subject="This is a Test";
MyBody = "Thank you for ordering that stuff";
MyCDONTSMail.Body= MyBody;
MyCDONTSMail.Send();

The only problem is that CDONTS or any other object won't create the e-mail form fields on the ASP page for you. These would have to be created manually by you. I have covered how to write screen not linked to a table in previous posts.


Note: You will see in the documentation that there are other objects related to emails, e.g. the MsgHandler Object. The MsgHandler, Email Object, AddressList Object, MailAddress, Attachment, and AttachmentList objects are all only appropriate to the Mail Manager script templates.