@independentsoft/msg

Outlook .msg file module

Usage no npm install needed!

<script type="module">
  import independentsoftMsg from 'https://cdn.skypack.dev/@independentsoft/msg';
</script>

README

This module allows you to easy create, read, update and parse Outlook message files.

Features

  • Create message, appointment, task, contact or any other Outlook item.
  • Read any Outlook .msg file and get all properties.
  • Set/Get MAPI properties.
  • Set/Get named properties.
  • Add attachments.
  • Read attachments.
  • Save attachments.
  • Add embedded messages.
  • Read embedded messages.
  • Save embedded messages.

Examples

Create message 
import * as msg from './independentsoft/msg.js';
import * as fs from 'fs';

let recipient1 = new msg.Recipient();
recipient1.addressType = "SMTP";
recipient1.displayType = msg.DisplayType.MAIL_USER;
recipient1.objectType = msg.ObjectType.MAIL_USER;
recipient1.displayName = "John@domain.com";
recipient1.emailAddress = "John@domain.com";
recipient1.recipientType = msg.RecipientType.TO;

let recipient2 = new msg.Recipient();
recipient2.addressType = "SMTP";
recipient2.displayType = msg.DisplayType.MAIL_USER;
recipient2.objectType = msg.ObjectType.MAIL_USER;
recipient2.displayName = "Mary@domain.com";
recipient2.emailAddress = "Mary@domain.com";
recipient2.recipientType = msg.RecipientType.CC;

let message = new msg.Message();
message.subject = "Test";
message.body = "Body text";
message.displayTo = "John Smith";
message.displayCc = "Mary Smith";
message.recipients.push(recipient1);
message.recipients.push(recipient2);
message.messageFlags.push(msg.MessageFlag.UNSENT);
message.storeSupportMasks.push(msg.StoreSupportMask.CREATE);

fs.writeFileSync("e:\\message.msg", message.toBytes());
Create HTML message 
import * as msg from './independentsoft/msg.js';
import * as fs from 'fs';

let recipient1 = new msg.Recipient();
recipient1.addressType = "SMTP";
recipient1.displayType = msg.DisplayType.MAIL_USER;
recipient1.objectType = msg.ObjectType.MAIL_USER;
recipient1.displayName = "John@domain.com";
recipient1.emailAddress = "John@domain.com";
recipient1.recipientType = msg.RecipientType.TO;

let recipient2 = new msg.Recipient();
recipient2.addressType = "SMTP";
recipient2.displayType = msg.DisplayType.MAIL_USER;
recipient2.objectType = msg.ObjectType.MAIL_USER;
recipient2.displayName = "Mary@domain.com";
recipient2.emailAddress = "Mary@domain.com";
recipient2.recipientType = msg.RecipientType.CC;

const htmlBody = "<html><body><b>Hello World bold html text</b></body></html>"
const htmlBodyWithRtf = "{\\rtf1\\ansi\\ansicpg1252\\fromhtml1 \\htmlrtf0 " + htmlBody + "}"
const rtfBody = new TextEncoder().encode(htmlBodyWithRtf)

let message = new msg.Message();
message.subject = "Test";
message.body = "Body text";
message.displayTo = "John Smith";
message.displayCc = "Mary Smith";
message.recipients.push(recipient1);
message.recipients.push(recipient2);
message.bodyTtmlText = htmlBody
message.bodyRtf = rtfBody
message.messageFlags.push(msg.MessageFlag.UNSENT);
message.storeSupportMasks.push(msg.StoreSupportMask.CREATE);

fs.writeFileSync("e:\\message.msg", message.toBytes());
Read message 
import * as msg from './independentsoft/msg.js';
import * as fs from 'fs';

const buffer = fs.readFileSync('e:\\unicode.msg');
const message = new msg.Message(buffer)

console.log("Subject: " + message.subject);
console.log("Sender name: " + message.senderName);
console.log("Sender email address: " + message.senderEmailAddress);
console.log("Received by name: " + message.receivedByName);
console.log("Received by email address: " + message.receivedByEmailAddress);
console.log("Sent time: " + message.clientSubmitTime);
console.log("Received time: " + message.messageDeliveryTime);
console.log("Display to: " + message.displayTo);
console.log("Display cc: " + message.displayCc);
console.log("Body: " + message.body);


Add attachment 
import * as msg from './independentsoft/msg.js';
import * as fs from 'fs';

const buffer = fs.readFileSync("e:\\test.pdf");
let attachment = new msg.Attachment(buffer);
attachment.fileName = "test.pdf";
attachment.displayName = "test.pdf";

let message = new msg.Message();
message.subject = "Test";
message.body = "Body text";
message.messageFlags.push(msg.MessageFlag.UNSENT);
message.storeSupportMasks.push(msg.StoreSupportMask.CREATE);
message.attachments.push(attachment)

fs.writeFileSync("e:\\message.msg", message.toBytes());
Get attachments 
import * as msg from './independentsoft/msg.js';
import * as fs from 'fs';

const buffer = fs.readFileSync("e:\\message.msg");
const message = new msg.Message(buffer);

for(let i=0; i < message.attachments.length; i++) {
    const attachment = message.attachments[i];
    fs.writeFileSync("e:\\temp\\" + attachment.longFileName, attachment.toBytes());
}
Create appointment 
import * as msg from './independentsoft/msg.js';
import * as fs from 'fs';

let appointment = new msg.Message()
appointment.messageClass = "IPM.Appointment";
appointment.subject = "Test";
appointment.body = "Body text";
appointment.location = "My Office";
appointment.appointmentStartTime = new Date(2020,11,10,8,0,0);
appointment.appointmentEndTime = new Date(2020,11,10,10,0,0);

fs.writeFileSync("e:\\appointment.msg", appointment.toBytes());
Create contact 
import * as msg from './independentsoft/msg.js';
import * as fs from 'fs';

let contact = new msg.Message()
contact.messageClass = "IPM.Contact";
contact.subject = "John Smith";
contact.displayNamePrefix = "Mr.";
contact.displayName = "John Smith";
contact.givenName = "John";
contact.surname = "Smith";
contact.companyName = "Independentsoft";
contact.email1Address = "john@independentsoft.com";
contact.email1DisplayAs = "John";
contact.email1DisplayName = "John";
contact.email1Type = "SMTP";
contact.businessAddressCity = "NY";
contact.businessAddressStreet = "First Street";
contact.businessAddressCountry = "USA";
contact.businessAddress = "First Street, NY, USA";
contact.businessPhone = "555-666-777";

fs.writeFileSync("e:\\contact.msg", contact.toBytes());