Select e-mail from list using UID
Since Unique-ID values persist accross
sessions, they are often more reliable way to identify messages on the
mail server. Persisting of Unique-ID values is especially important in web environments where multiple users can simultaneously access shared mail account. Assume the following scenario:
|
' IMAP4 object Dim objIMAP4 ' Stores Unique-ID for each message in the Inbox Dim arrUIDs ' Connects to server, logs in email account ' and selects Inbox. ' Returns True if succeeded, otherwise False Function GetInbox() GetInbox = False ' Set IMAP4 server name objIMAP4.ServerName = "mail.server.com" ' Set user credentials objIMAP4.UserName = "username" objIMAP4.Password = "password" ' Connect to the server and ' log in email account If objIMAP4.Connect Then ' Select Inbox folder If objIMAP4.SelectMailbox("Inbox") Then ' Successully logged in and selected Inbox GetInbox = True Else ' Display error information MsgBox "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc End If Else ' Display error information MsgBox "Error #" & objIMAP4.ErrCode MsgBox "Server response: " & objIMAP4.ServerResponse End If End Function ' Fills ListBox control with "Subject:" field ' of each message in the Inbox and remembers ' Unique-ID's of all messages in arrUIDs array Sub DisplayList() Dim objEnvelopes, objEnvelope ' Try to login If GetInbox Then ' Fill array with Unique-ID values of all ' messages in the mailbox arrUIDs = objIMAP4.Search(True) ' Get envelopes for all messages in the Inbox Set objEnvelopes = objIMAP4.RetrieveEnvelopes(1, objIMAP4.MessageCount, False) If Not objIMAP4.IsError Then For Each objEnvelope In objEnvelopes ' Add each message's "Subject:" to ListBox List1.AddItem objEnvelope.Subject Next Else ' Display error information MsgBox "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc End If End If ' Disconnect if needed If objIMAP4.Connected Then objIMAP4.Disconnect End If End Sub ' Display body of an e-mail when this e-mail's ' "Subject:" is clicked in List1 control Private Sub List1_Click() Dim objMsg ' Try to login If GetInbox Then ' Get message by Unique-ID stored in arrUIDs array Set objMsg = objIMAP4.RetrieveSingleMessage(arrUIDs(List1.ListIndex + 1), True) If Not objIMAP4.IsError Then ' Display message body MsgBox objMsg.BodyText Else ' Display error information MsgBox "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc End If End If ' Disconnect if needed If objIMAP4.Connected Then objIMAP4.Disconnect End If End Sub ' Display message list on application start Private Sub Form_Load() ' Create and unlock IMAP4 object Set objIMAP4 = CreateObject("MailBee.IMAP4") objIMAP4.LicenseKey = "put your license key here" ' Display messages' subjects and remember messages' Unique-ID's DisplayList End Sub
DisplayList.asp: <% Dim objIMAP4, objEnvelopes, I ' Connects to server, logs in email account ' and selects Inbox. ' Returns True if succeeded, otherwise False. ' ' This function is duplicated in both ASP files. ' In production code, it's better to move duplicates ' into single include file. Function GetInbox() GetInbox = False ' Set IMAP4 server name objIMAP4.ServerName = "mail.server.com" ' Set user credentials objIMAP4.UserName = "username" objIMAP4.Password = "password" ' Connect to the server and ' log in email account If objIMAP4.Connect Then ' Select Inbox folder If objIMAP4.SelectMailbox("Inbox") Then ' Successully logged in and selected Inbox GetInbox = True Else ' Display error information Response.Write "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc End If Else ' Display error information Response.Write "Error #" & objIMAP4.ErrCode & "<br>" Response.Write "Server response: " & objIMAP4.ServerResponse End If End Function ' Create and unlock IMAP4 object Set objIMAP4 = CreateObject("MailBee.IMAP4") objIMAP4.LicenseKey = "put your license key here" ' Try to login If GetInbox Then ' Fill array with Unique-ID values of all ' messages in the mailbox arrUIDs = objIMAP4.Search(True) If Not objIMAP4.IsError Then ' Get envelopes for all messages in the Inbox Set objEnvelopes = objIMAP4.RetrieveEnvelopes(1, objIMAP4.MessageCount, False) If Not objIMAP4.IsError Then For I = 1 To objEnvelopes.Count ' Print each e-mail's "Subject:" in the form of ' "A HREF" link that points to DisplayEmail.asp with UID parameter ' set to Unique-ID of the e-mail Response.Write "<a href='DisplayEmail.asp?uid=" & arrUIDs(I) & "'>" & objEnvelopes(I).Subject & "</a><br>" Next Else ' Display error information Response.Write "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc End If Else ' Display error information Response.Write "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc End If End If ' Disconnect if needed If objIMAP4.Connected Then objIMAP4.Disconnect End If %> DisplayEmail.asp: <% Dim objIMAP4, objMsg, I ' Connects to server, logs in email account ' and selects Inbox. ' Returns True if succeeded, otherwise False. ' ' This function is duplicated in both ASP files. ' In production code, it's better to move duplicates ' into single include file. Function GetInbox() GetInbox = False ' Set IMAP4 server name objIMAP4.ServerName = "mail.server.com" ' Set user credentials objIMAP4.UserName = "username" objIMAP4.Password = "password" ' Connect to the server and ' log in email account If objIMAP4.Connect Then ' Select Inbox folder If objIMAP4.SelectMailbox("Inbox") Then ' Successully logged in and selected Inbox GetInbox = True Else ' Display error information Response.Write "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc End If Else ' Display error information Response.Write "Error #" & objIMAP4.ErrCode & "<br>" Response.Write "Server response: " & objIMAP4.ServerResponse End If End Function ' Create and unlock IMAP4 object Set objIMAP4 = CreateObject("MailBee.IMAP4") objIMAP4.LicenseKey = "put your license key here" ' Try to login If GetInbox Then ' Get message by Unique-ID taken from Request variable Set objMsg = objIMAP4.RetrieveSingleMessage(Request.QueryString("uid"), True) If Not objIMAP4.IsError Then ' Display message body Response.Write objMsg.BodyText Else ' Display error information Response.Write "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc End If End If ' Disconnect if needed If objIMAP4.Connected Then objIMAP4.Disconnect End If %>See Also: