In previous section, I introduced how to send HTML email. In this section, I will introduce how to add attachment to email in JavaScript.
To send an email with file attachment, we need to use AddAttachmentAsync method. This method can attach a file to the email message from local disk or a remote URL.
Note
Remarks: All of samples in this section are based on first section: Send email in A simple JavaScript HTML5 Windows Store App project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.
The following example codes demonstrate how to use EASendMail SMTP component to send email with file attachments.
Note
To get the full sample projects, please refer to Samples section.
(function () {
"use strict";
var appViewState = Windows.UI.ViewManagement.ApplicationViewState;
var ui = WinJS.UI;
var htmlinited = false;
var editor;
var asynCancel = null;
var m_atts = new Array();
ui.Pages.define("/default.html", {
// This function is called whenever a user navigates to this page. It
// populates the page elements with the app's data.
ready: function (element, options) {
init_gui();
},
unload: function () {
}
});
function init_gui() {
// add OnClick event handler
var btn = document.getElementById("btnSend");
btn.addEventListener("click", send_email, false);
}
function send_email() {
var btn = document.getElementById("btnSend");
var result = "";
var oMail = new EASendMail.SmtpMail("TryIt");
// Set sender email address, please change it to yours
oMail.from = new EASendMail.MailAddress("test@emailarchitect.net");
// Add recipient email address, please change it to yours
oMail.to.add(new EASendMail.MailAddress("support@emailarchitect.net"));
// Set email subject
oMail.subject = "test email with attachment from JavaScript HTML5 project";
// Set HTML body
oMail.htmlBody = "<font size=5>This is</font> <font color=red><b>a test</b></font>";
var oServer = new EASendMail.SmtpServer("smtp.emailarchitect.net");
// User and password for ESMTP authentication
oServer.user = "test@emailarchitect.net";
oServer.password = "testpassword";
// If your SMTP server requires TLS connection on 25 port, please add this line
// oServer.connectType = EASendMail.SmtpConnectType.connectSSLAuto;
// If your SMTP server requires SSL connection on 465 port, please add this line
// oServer.port = 465;
// oServer.connectType = EASendMail.SmtpConnectType.connectSSLAuto;
var oSmtp = new EASendMail.SmtpClient();
// get a file path from PicturesLibrary,
// to access files in PicturesLibrary, you MUST have "Pictures Library" checked in
// your project -> Package.appxmanifest -> Capabilities
Windows.Storage.KnownFolders.picturesLibrary.getFileAsync("test.jpg")
.then(function (file) {
var attfile = file.path;
// if you want to add attachment from remote URL instead of local file.
// var attfile = "http://www.emailarchitect.net/test.jpg";
return oMail.addAttachmentAsync(attfile);
})
.then(function (oAttachment) {
// you can change the Attachment name by
// oAttachment.name = "mytest.jpg";
btn.disabled = true;
return oSmtp.sendMailAsync(oServer, oMail);
})
.done(function () {
result = "Email was sent successfully!";
// Display Result by Diaglog box
(new Windows.UI.Popups.MessageDialog(result, "Success")).showAsync();
btn.disabled = false;
},
// error handle
function (e) {
// because javascript exception only gives the stack trace messages, but it is not
// real description of exception, so we give a property lastErrorMessage for javascript.
if (oMail.lastErrorMessage != "") {
result = oSmtp.lastErrorMessage;
}
else if (oSmtp.lastErrorMessage != "") {
result = oSmtp.lastErrorMessage;
}
else {
result = e.message;
}
(new Windows.UI.Popups.MessageDialog(result, "Error Information")).showAsync();
btn.disabled = false;
});
}
})();
Next Section
At next section I will introduce how to add embedded images/pictures to email message.
Appendix
Comments
If you have any comments or questions about above example codes, please click here to add your comments.