Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.0.0+

Find an email by subject and fetch it by IMAP UID

See more Email Object Examples

Search the selected INBOX by subject and fetch the first matching UID into an Email. The example requests complete attachment bodies and uses peek mode to preserve the read flag. Configure your own IMAP TLS endpoint and credentials.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Imap
oleobject loo_Matches
long li_Uid
oleobject loo_Email

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")
if li_rc < 0 then
    destroy loo_Imap
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Imap.Ssl = 1
loo_Imap.Port = 993
loo_Imap.RequireSslCertVerify = 1
loo_Imap.PeekMode = 1
loo_Imap.AutoDownloadAttachments = 1
li_Success = loo_Imap.Connect("imap.example.com")
if li_Success <> 1 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.Login("MAIL_USER","MAIL_PASSWORD")
if li_Success <> 1 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.SelectMailbox("INBOX")
if li_Success <> 1 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

loo_Matches = create oleobject
li_rc = loo_Matches.ConnectToNewObject("Chilkat.MessageSet")

li_Success = loo_Imap.QueryMbx("SUBJECT ~"Sample message~"",1,loo_Matches)
if li_Success <> 1 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Matches
    return
end if

if loo_Matches.Count = 0 then
    Write-Debug "No matching message in INBOX."
    li_Success = loo_Imap.Disconnect()
    if li_Success <> 1 then
        Write-Debug loo_Imap.LastErrorText
        destroy loo_Imap
        destroy loo_Matches
        return
    end if

    destroy loo_Imap
    destroy loo_Matches
    return
end if

//  The search returned UIDs. Use the same identifier type for the fetch.
li_Uid = loo_Matches.GetId(0)
loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

li_Success = loo_Imap.FetchEmail(0,li_Uid,1,loo_Email)
if li_Success <> 1 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Matches
    destroy loo_Email
    return
end if

Write-Debug "Mailbox INBOX, UIDVALIDITY " + string(loo_Imap.UidValidity) + ", UID " + string(li_Uid)
li_Success = loo_Imap.Disconnect()
if li_Success <> 1 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Matches
    destroy loo_Email
    return
end if

//  The downloaded Email remains usable after disconnecting.
Write-Debug "Subject: " + loo_Email.Subject + "  Files: " + string(loo_Email.NumAttachments)

li_Success = loo_Email.SaveEml("imap-received.eml")
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Imap
    destroy loo_Matches
    destroy loo_Email
    return
end if

Write-Debug "Saved imap-received.eml."


destroy loo_Imap
destroy loo_Matches
destroy loo_Email