Data Browser - Viewing Site  Sector 23 Code Bank Logged in as:  Guest  




           


Open Outlook Email with BCC's on Client from ASP.NET
I have tried several ways to open an outlook email on the client in ASP.NET and only found one okay solution - but it requires IE and Active-X enabled for the site.

Try 1: Use mailto. Fails if message body is too large. No good.

Try 2: Use Outlook API to generate email file and stream to client - this only works if outlook is on the server. No good.

Try 3: Write a plain text .eml file and stream to client - this only works if client is using a version of outlook that can open .eml files, such as 2010 or Express. Also it does not support BCCs. No good.

Try 4: Use .Net MailMessage to save an email to the 'drop folder', manually mark it as unsent, and stream to client. Outlook couldn't handle weird x-headers generated, BCC failed to persist. No good.

Try 5: Active-X JavaScript. Supports BCC. Supports long message body. Supports file attachments if available as a public-facing URL. Best option so far.

To implement:
Uses the ScriptProvider design pattern to render compile-time JS:
(this overload assumes body should be blank, but see other example below for how to specify body)

public ScriptProvider OpenOutlookEmail(List<string> bcc_addresses, string subject, string attachmentUrl)
{
this.BeginTry();

// create our emailer
this.SetVariable("customEmail", "new ActiveXObject('Outlook.Application').CreateItem(0)");

//add some recipients; type is 1 (normal), 2 (cc) or 3 (bcc)
if (bcc_addresses != null)
{
foreach (string bcc in bcc_addresses)
{
this.ExecuteScript(string.Format("customEmail.Recipients.Add({0}).Type = 3", JavaScriptUtility.QuoteText(bcc)));
}
}

// set subject
if (!string.IsNullOrWhiteSpace(subject))
{
this.SetVariable("customEmail.Subject", JavaScriptUtility.QuoteText(subject));
}

// attachment support
if (!string.IsNullOrWhiteSpace(attachmentUrl))
{
//1=Add by value so outlook downloads the file from the url
ExecuteScript(string.Format("customEmail.Attachments.Add({0}, 1);", JavaScriptUtility.QuoteText(attachmentUrl)));
}
ExecuteScript("customEmail.Display();");

// error handling
this.EndTryBeginCatch("ex");
this.AlertString("Outlook Access requires Internet Explorer and ActiveX enabled. Please put this site in your trusted sites and enable 'Initialize and script ActiveX controls not marked as safe for scripting' to use this feature.");
this.EndCatch();

return this;
}

If you would rather have plain JS, start from this and edit as needed.

//get outlook and create new email
function openEmail()
{
try
{
var outlook = new ActiveXObject('Outlook.Application');
var email = outlook.CreateItem(0);

//add some recipients
email.Recipients.Add('user1@company.com').Type = 1; //1=To
email.Recipients.Add('user2@company.com').Type = 2; //2=CC
email.Recipients.Add('user3@company.com').Type = 3; //2=CC

//subject and attachments
email.Subject = '';
//email.Attachments.Add('URL_TO_FILE', 1); //1=Add by value so outlook downloads the file from the url

// display the email (this will make the signature load so it can be extracted)
email.Display();

//use a regular expression to extract the html before and after the signature
var signatureExtractionExpression = new RegExp('/[^~]*(<BODY[^>]*>)([^~]*</BODY>)[^~]*/', 'i');
signatureExtractionExpression.exec(email.HTMLBody);
var beforeSignature = RegExp.$1;
var signature = RegExp.$2;

//set the html body of the email
email.HTMLBody = beforeSignature + '<h1>Our Custom Body</h1>' + signature;
}
catch(ex)
{
alert("Outlook Access requires Internet Explorer and ActiveX enabled. Please put this site in your trusted sites and enable 'Initialize and script ActiveX controls not marked as safe for scripting' to use this feature.");
}
}

Created By: amos 3/23/2015 2:20:32 PM
Updated: 3/23/2015 2:23:34 PM