Mastering PowerBuilder

HomePrevious Lesson: Course 3:: Session 26 :: Page 70
Next Lesson: Course 3:: Session 26 :: Page 90
Getting a list of all unread Mail Messages

The following code is taken from the 'w_read_mail' window residing in the example application. Even though the code seems large, the logic is very simple. First, MailGetMessages() is called to pull all the message ids from the mail system for the logged-in user and populate messageid[] array in the mail session object ( Message id in each user mail box is unique. ). Later in a loop MailReadMessage() is called. You can observe MailEnvelopeOnly! is passed as an argument to the above function. That means, we are reading just the envelope information. Envelope contains the following information:

Recipient details
Subject
Date received
Mail read status
Flag to indicate whether return receipt is requested
Attachment Details array
imRet_MailReturnCode = ims_MSession.MailGetMessages()
li_msgs = UpperBound(ims_MSession.MessageID)
For li_index = 1 to li_msgs
    imRet_MailReturnCode = &
         ims_MSession.MailReadMessage( &
         ims_MSession.MessageID[li_index], mMsg[li_index],&
         MailEnvelopeOnly!, FALSE )
    ls_ret = f_mail_error_to_string ( &
	imRet_MailReturnCode, &
	'Read Message Envelope:' , FALSE )
    st_status_bar.text = ' Inbox: Msg ' + &
         string (li_index, '##0') + ': ' + ls_Ret
    li_attachments = UpperBound ( &
         mMsg[li_index].AttachmentFile )
    li_attachnumber =0
    for li_attachindex = 1 to li_attachments
                         ls_filename = &
        mMsg[li_index].AttachmentFile[li_attachindex].&
	   		FileName
        if Lower ( Right ( ls_filename, 4 )) = ".psr" then
	  li_dwattached = 1
	  li_attachnumber = li_attachindex
        else
	  li_dwattached = 0
        end if
     next // Loop on attachments in current message
    // Add mail to the Inbox DataWindow. Note that its
    // MessageId and Attachment Number are not visible;
    // they will be needed later to retrieve the
    // attachment. The dwattached, unread, receipt fields
    // are hidden and referenced by a computed column that
    // will show a bitmap if they equal to one.
li_row = dw_inbox.InsertRow ( 0 )
dw_inbox.object.Sender[li_row] = &
          mMsg[li_index].Recipient[1].Name
dw_inbox.object.Subject[li_row] = &
	mMsg[li_index].Subject
dw_inbox.object.Postmark[li_row] = &
	mMsg[li_index].DateReceived
dw_inbox.object.MessageId[li_row] = &
	ims_MSession.MessageID[li_index]
dw_inbox.object.Attachment_number[li_row] = &
	li_attachnumber
dw_inbox.object.dwattached[li_row] = li_dwattached
if mMsg[li_index].unread then
	dw_inbox.object.unread[li_row] = 1
else
	dw_inbox.object.unread[li_row] = 0
end if
if mMsg[li_index].ReceiptRequested then
	dw_inbox.object.receipt[li_row] = 1
else
	dw_inbox.object.receipt[li_row] = 0
end if
Next

In a loop the envelope information for each mail message is read and a DataWindow is populated with this information. The key things you need to understand in this whole code is MailGetMessages() and MailReadMessage().

HomePrevious Lesson: Course 3:: Session 26 :: Page 70
Next Lesson: Course 3:: Session 26 :: Page 90