MAPI Sample

The following sample uses the new MAPI API to:

  • Logon to a MAPI Session

  • Read all messages in the Inbox

  • Delete the first message in the Inbox

  • Change the subject of the first message in the Inbox

  • Sends a new Message

benchmark BenchmarkName 

use "kernel.bdh"
use "mapi.bdh"

var
  ghSession : number init 0;
// Workload Section
dcluser
  user
    VirtUser
  transactions
    TInit           : begin;
    TGetMsgs        : 1;
    TSendMsg        : 1;
    TSendMsgEx      : 1;
    TDeleteMsg      : 1;
    TChangeMsg      : 1;
    TEnd            : end;

// Transactions Section
dcltrans 
  transaction TInit
  begin
    MapiInit();
    ghSession := MapiLogon("Outlook", null);
    if(ghSession = 0) then halt; end;
  end TInit;

  transaction TGetMsgs
  var
    hMsg, nRecipCount, nAttachCount, i : number;
    sLastMsgId, sNextMsgId             : string;
    sValue                             : string;
  begin
    sLastMsgId := "";
    // Iterate through all Mails and retrieve mail properties
    while(MapiGetNextMsgId(ghSession, sLastMsgId, sNextMsgId)) do
      hMsg := MapiGetMessage(ghSession, true, false, false, false, sNextMsgId);

      MapiGetMessageProperty(hMsg, MAPI_PROP_MSG_MSGTYPE, sValue);
      writeln("MsgType: " + sValue);
      MapiGetMessageProperty(hMsg, MAPI_PROP_MSG_SUBJECT, sValue);
      writeln("Subject: " + sValue);
      MapiGetMessageProperty(hMsg, MAPI_PROP_MSG_DATERECEIVED, sValue);
      writeln("Date: " + sValue);
      MapiGetMessageProperty(hMsg, MAPI_PROP_MSG_SENDER_NAME, sValue);
      writeln("Send-Name: " + sValue);
      MapiGetMessageProperty(hMsg, MAPI_PROP_MSG_SENDER_ADDRESS, sValue);
      writeln("Sender-Address: " + sValue);

      // Iterate through all Recipients
      if(MapiGetMessageProperty(hMsg, MAPI_PROP_MSG_RECIPIENT_COUNT, sValue)) then
        nRecipCount := number(sValue);
        writeln("RecipientCount: " + sValue);

        for i:=0 to nRecipCount-1 do
          MapiGetMessageRecipient(hMsg, i, true, sValue);
          writeln("Recipient-Name: " + sValue);
          MapiGetMessageRecipient(hMsg, i, false, sValue);
          writeln("Recipient-Address: " + sValue);
        end;
      end;

       // Iterate through all Attachments
      if(MapiGetMessageProperty(hMsg, MAPI_PROP_MSG_ATTACH_COUNT, sValue)) then
        nAttachCount := number(sValue);
        writeln("AttachCount: " + sValue);
                
        for i:=0 to nAttachCount-1 do
          MapiGetMessageAttachment(hMsg, i, true, sValue);
          writeln("Attach-File: " + sValue);
          MapiGetMessageAttachment(hMsg, i, false, sValue);
          writeln("Attach-Path: " + sValue);
        end;
      end;

      writeln("---------------------------------------------------");            

      // Free the message
      MapiFreeMessage(hMsg);
      sLastMsgId := sNextMsgId;
    end;
  end TGetMsgs;

  transaction TSendMsg
  begin
    // Send a new message
    MapiSendMessage(
      ghSession,
      "TestSubject",
      "Test Message Text" ,
      "",
      "SenderName",
      "sender@sendersdomain",
      "RecipientName",
      "recipient@recipientdomain");
  end TSendMsg;

  transaction TSendMsgEx
  var
    hMsg : number;
  begin
    // Create a new message
    hMsg := MapiCreateMessage(
      "TestSubject",
      "Test Message Text",
      "",
      "SenderName",
      "sender@sendersdomain",
      "RecipientName",
      "recipient@recipientdomain");
    if(hMsg = 0) then halt; end;

    // Add a 2nd Recipient
    MapiSetMessageProperty(hMsg, MAPI_PROP_MSG_RECIPIENT_COUNT, "2");
    MapiSetMessageRecipient(hMsg, 1, "2nd Recipient", "test@test.com",
MAPI_MSG_RECIPTYPE_CC);

    // Add an attachment
    MapiSetMessageProperty(hMsg, MAPI_PROP_MSG_ATTACH_COUNT, "1");
    MapiSetMessageAttachment(hMsg, 0, "test.txt", "c:\\temp\\test.txt");

    // Send the message
    MapiSendMessageEx(ghSession, hMsg);

    // Free the message
    MapiFreeMessage(hMsg);
  end TSendMsgEx;

  transaction TDeleteMsg
  var
    sMsgId : string;
    hMsg   : number;
  begin
    // delete the first message
    if MapiGetNextMsgId(ghSession, "", sMsgId) then
      MapiDeleteMessage(ghSession, sMsgId);
    end;
  end TDeleteMsg;

  transaction TChangeMsg
  var
    sMsgId, sSubject : string;
    hMsg   : number;
  begin
    // retrieve the first message and change the message subject
    if MapiGetNextMsgId(ghSession, "", sMsgId) then
      hMsg := MapiGetMessage(ghSession, true, false, true, true, sMsgId);
      if(hMsg <> 0) then
        // get the subject - add the value 'Modified' and set the new value
        MapiGetMessageProperty(hMsg, MAPI_PROP_MSG_SUBJECT, sSubject);
        sSubject := sSubject + " Modified";
        MapiSetMessageProperty(hMsg, MAPI_PROP_MSG_SUBJECT, sSubject);

        // Save the changes
        MapiSaveMessage(ghSession, hMsg, sMsgId);

        // Free Message
        MapiFreeMessage(hMsg);
      end;
    end;
  end TChangeMsg;

  transaction TEnd
  begin
    // Logoff
    MapiLogoff(ghSession);
    MapiExit();
  end TEnd;