Dec 23, 2014

SMTP Bundle

Occasionally, we might find the need to send an Email message that contains one or more attachments.

Although SharePoint has the “SendEmail” function on SPUtility (and exposed currently through Barista) this method doesn’t provide the ability to add attachments to the outgoing mail message… thus forcing the developer to go bonkers:

With the Smtp bundle, we can write the following code to perform similar functionality:

var sp = require("SharePoint");

//Grab the outgoing smtp server configured in CA for the current web application.
var outgoingSmtpServer =

//Create a new SmtpClient with the outgoing server address
var client = new SmtpClient(outgoingSmtpServer.server.address);

//Load an existing file from SharePoint
var file = sp.loadFileAsByteArray("/Documents/BreakReport.xlsx");

//Create a new message and add the file we loaded as an attachment.
var message = new MailMessage("", "somesharepointguy@ofsportal", "Hi", "testtesttesttest");
message.attachments.add(new SmtpAttachment(file));

//Add an alternate view for mail readers that understand html.
message.alternateViews.add(AlternateView.createAlternateViewFromString("<html><body><b>Hi!</b></body></html>", "Unicode", "text/html"));

//Send the mail message synchronously.

results: alt text

‘till next time.