Sending an email from Saleslogix (or ASP.NET)
Posted By: nicocrm on June 23rd, 2008 in Uncategorized
No Gravatar

As the .NET framework is available to code executing on the server side of the Saleslogix web client it is easy to send an email using System.Net.Mail etc.  Lots of available examples for that so I won’t repeat them.

The problem with this approach (for some scenario, anyway) is that the user does not have a chance to review and edit the text the email (or attach it to Saleslogix for that matter).  Sometimes it would be nicer to just open the email in Outlook and let them complete it, and in some cases it is possible with a little bit of JavaScript, as long as you are able to get your users to adjust their IE security settings to enable unsafe ActiveX (which they have to do for the export to Excel anyway).

My problem was a simple "Quote" screen where the user should be able to print the quote report, have it attached to history and have the opportunity to send an email to the quote contact:

image

This is simple enough to do in the regular Saleslogix client – in the web client there are 2 problems: one is creating and displaying the message in Outlook, the other one is attaching the report (which was exported as a PDF on the server but will not be available from the client side).

In order to resolve the second problem, and since we can give Outlook a URL to attach, I created a "GetReport" service that is placed in an unsecured directory (since it will have to be accessed from Outlook) and simply returns the PDF data.  As a side note, to unsecure a page, place a web.config file in its directory with the following:

<?xml version="1.0"?>

<configuration>
  <location path="GetReport.ashx">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
</configuration>

Now you can get to it without logging in.  Which is OK for this service since you need to have a report token in order to retrieve anything.

To resolve the first problem I sent some code (with ScriptManager.RegisterClientBlock) that will open Outlook on the client, fill in the subject and recipient, and display the message so the user can finish editing it:

public void ShowReportEmail(Uri reportUrl, string to, string cc, string subject, string body)
{
    StringBuilder script = new StringBuilder();

    var msgData = new { to = to, cc = cc, subject = subject, body = body, reportUrl = reportUrl.ToString() };
    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    script.Append("try {n" +
        "var outlook = new ActiveXObject("Outlook.Application");n" +
        "var msg = outlook.CreateItem(0);n")
        .AppendFormat("var msgData = {0};n", serializer.Serialize(msgData))
        .Append("msg.Subject = msgData.subject;n" +
            "msg.Body = msgData.body;n" +
            "msg.To = msgData.to;n" +
            "msg.Cc = msgData.cc;n" +
            "msg.Attachments.Add(msgData.reportUrl);n" +
            "msg.Display();n" +
            "} catch(e) {n" +
            "  alert('Error accessing outlook: ' + e.description + '.\nPlease consult your administrator on recommended IE settings.');n" +
            "}");

    ScriptManager.RegisterClientScriptBlock(this, GetType(), "EmailReport", script.ToString(), true);
}

The code makes use of the anonymous object syntax so requires VS 2008 to compile (should not require .NET 3.5 to run, though).  You could adapt easily if needed.

This probably works on Outlook 2003 and higher, but I only tested on Outlook 2007.

The end result looks like this… I am not thrilled about the fact that the attachment name looks so crappy but other than that I quite like it:

image

And this is by the way the setting that needs to be enabled in IE… you probably only want to enable that for trusted sites:

image


2 Responses to “Sending an email from Saleslogix (or ASP.NET)”

  1. varmaNo Gravatar says:

    How to make this script work for Firefox. It works great in IE.

    • Alex.CottnerNo Gravatar says:

      Varma,

      You would only be able to make it work on a supported version of Firefox that has the SLX Enhancements installed. To do this you would use the MailMergeService, like we did in the link below for Excel automation. But you can’t do this with a newer/unsupported version of Firefox unfortunately. Please consult your SLX compatibility chart to see what is compatible with your version of SLX.
      http://blog.sssworld.com/2012/08/01/excel-automation-in-saleslogix-web-7-5-4/

Leave a Reply