PowerBuilder
PowerBuilder
Fetch an IMAP Attachment into a BinData
See more IMAP Examples
Demonstrates the Chilkat Imap.FetchAttachmentBd method, which stores one attachment's bytes in a BinData object. The first argument is the Email, the second is the zero-based attachment index, and the third is the BinData, which is cleared before the operation. This example fetches the first attachment and prints its byte count.
Background: A
BinData holds raw bytes, so it works for any attachment type — image, PDF, zip, and so on. From there you can save it, hash it, or hand it to another API expecting a byte buffer, all without touching the filesystem. Use the string/StringBuilder variants only when the attachment is known to be text.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Imap
integer li_HeadersOnly
integer li_UseUid
integer li_SeqNum
oleobject loo_Email
integer li_NumAttach
oleobject loo_Bd
li_Success = 0
// Demonstrates the Imap.FetchAttachmentBd method, which downloads one attachment's bytes into
// a BinData object. The 1st argument is the Email, the 2nd is the zero-based attachment
// index, and the 3rd is the BinData (which is cleared first).
//
// The message is fetched headers-only, then the attachment is downloaded on demand.
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
li_Success = loo_Imap.Connect("imap.example.com")
if li_Success = 0 then
Write-Debug loo_Imap.LastErrorText
destroy loo_Imap
return
end if
li_Success = loo_Imap.Login("user@example.com","myPassword")
if li_Success = 0 then
Write-Debug loo_Imap.LastErrorText
destroy loo_Imap
return
end if
li_Success = loo_Imap.SelectMailbox("Inbox")
if li_Success = 0 then
Write-Debug loo_Imap.LastErrorText
destroy loo_Imap
return
end if
// Fetch only the message headers. Attachment bodies are NOT downloaded, but the ckx-imap-*
// metadata describing the attachments is present, so the attachment info methods still work.
li_HeadersOnly = 1
li_UseUid = 0
li_SeqNum = 1
loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")
li_Success = loo_Imap.FetchEmail(li_HeadersOnly,li_SeqNum,li_UseUid,loo_Email)
if li_Success = 0 then
Write-Debug loo_Imap.LastErrorText
destroy loo_Imap
destroy loo_Email
return
end if
li_NumAttach = loo_Imap.GetMailNumAttach(loo_Email)
if li_NumAttach > 0 then
// Download just the first attachment's bytes from the server into a BinData.
loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")
li_Success = loo_Imap.FetchAttachmentBd(loo_Email,0,loo_Bd)
if li_Success = 0 then
Write-Debug loo_Imap.LastErrorText
destroy loo_Imap
destroy loo_Email
destroy loo_Bd
return
end if
Write-Debug "Attachment size: " + string(loo_Bd.NumBytes) + " bytes"
end if
li_Success = loo_Imap.Disconnect()
if li_Success = 0 then
Write-Debug loo_Imap.LastErrorText
destroy loo_Imap
destroy loo_Email
destroy loo_Bd
return
end if
destroy loo_Imap
destroy loo_Email
destroy loo_Bd