Description
This function helps you to send an email in ASPRunner.NET.
To use it, you need to set up email parameters (From, SMTP server, SMTP server port, SMTP server username, SMTP server password) in the Email settings.
Syntax
MVCFunctions.runner_mail(params);
Arguments
params
an array with input parameters. The following parameters are supported:
Email parameters
from
the sender's email address. If none is specified, an email address from the Email settings is used.
fromName
the sender's name.
to
the receiver's email address.
cc
email addresses of secondary recipients.
bcc
email addresses of recipients whose addresses are not to be revealed to other recipients of the message.
replyTo
the reply email address.
priority
the message priority (use '1' for urgent, '2' - high, '3' - normal).
body
the plain text message body.
htmlbody
the html message body (do not use the 'body' parameter in this case).
charset
the html message charset. If none is specified, the default website charset is used.
attachments
the description of the attachments. The 'path' (a path to the attachment) is required. Other parameters are optional: 'name' overrides the attachment name, 'encoding' sets the file encoding, 'type' sets the MIME type.
Return value
An array with the following keys:
•mailed: (true or false) indicates whether the email was sent or not.
•message: an error message in case the email was not sent.
Examples
Send simple email
string email = "test@test.com";
string from = "admin@test.com";
string msg = "Hello there\n<b>Best regards</b>";
string subject = "Sample subject";
XVar result;
result = MVCFunctions.runner_mail(new XVar("to", email, "subject", subject, "body", msg, "from", from));
Send HTML email
string email = "test@test.com";
string from = "admin@test.com";
string msg = "Hello there\n<b>Best regards</b>";
string subject = "Sample subject";
XVar result;
result = MVCFunctions.runner_mail(new XVar("to", email, "subject", subject, "htmlbody", msg, "from", from));
Example with error handling
string email = "test@test.com";
string from = "admin@test.com";
string msg = "Hello there\nBest regards";
string subject = "Sample subject";
XVar result;
result = MVCFunctions.runner_mail(new XVar("to", email, "subject", subject, "body", msg, "from", from));
if(!result["mailed"])
{
MVCFunctions.Echo("Error happened: <br>");
MVCFunctions.Echo(result["message"]);
}
Send email with BCC and CC fields
string email = "test@test.com";
string from = "admin@test.com";
string msg = "Hello there\n<b>Best regards</b>";
string subject = "Sample subject";
XVar result;
result = MVCFunctions.runner_mail(new XVar("to", email, "cc", "test1@test.com", "bcc", "test2@test.comu", "subject", subject, "body", msg, "from", from));
Send email with From and FromName fields
string email = "test@test.com,test2@test.com,test3@test.com";
string message = "test";
string subject = "Sample subject";
string from = "bgates@microsoft.com";
string fromName = "Bill Gates";
MVCFunctions.runner_mail(new XVar("to", email, "subject", subject, "body", message, "fromName", fromName, "from", from));
Send email to multiple recipients
string email = "test@test.com,test2@test.com,test3@test.com";
string message = "test";
string subject = "Sample subject";
MVCFunctions.runner_mail(new XVar("to", email, "subject", subject, "body", message));
Send email with attachments
string email = "test@test.com";
string from = "admin@test.com";
string msg = "Hello there\nBest regards";
string subject = "Sample subject";
XVar attachments = XVar.Array();
// Attachments description. The 'path' is required. Others parameters are optional.
attachments = new XVar(
0, new XVar("path", MVCFunctions.getabspath("files/1.jpg")),
1, new XVar("path", MVCFunctions.getabspath("files/2.jpg"), "name", "image.jpg")) ;
XVar result;
result = MVCFunctions.runner_mail(new XVar("to", email, "subject", subject, "body", msg, "from", from, "attachments", attachments));
// You can manually overwrite SMTP settings
// result = MVCFunctions.runner_mail(new XVar("to", email, "subject", subject, "body", msg, "from", from, "attachments", attachments,
// "host", "somehost", "port", 25, "username", "smtpUserName", "password", "password"));
if(!result["mailed"])
{
MVCFunctions.Echo(result["message"]);
}
Send email with new record data. Use this code in the After Add event.
string email = "test@test.com";
string from = "admin@test.com";
string msg = "The new record was added: ";
string subject = "New record";
foreach(var field in values.GetEnumerator())
{
if(!CommonFunctions.IsBinaryType(pageObject.pSet.getFieldType(field.Key)))
{
msg.Append(field.Key.ToString() + " : " + field.Value.ToString() + "\r\n");
}
}
XVar result;
result = MVCFunctions.runner_mail(new XVar("to", email, "subject", subject, "body", msg, "from", from));
if(!result["mailed"])
{
MVCFunctions.Echo(result["message"]);
}
See also:
•A complete guide to sending emails with a web based application