Sample code for 30+ languages & platforms
Classic ASP 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 Classic ASP Downloads

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

set imap = Server.CreateObject("Chilkat.Imap")
imap.Ssl = 1
imap.Port = 993
imap.RequireSslCertVerify = 1
imap.PeekMode = 1
imap.AutoDownloadAttachments = 1
success = imap.Connect("imap.example.com")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

success = imap.Login("MAIL_USER","MAIL_PASSWORD")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

success = imap.SelectMailbox("INBOX")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

set matches = Server.CreateObject("Chilkat.MessageSet")
success = imap.QueryMbx("SUBJECT ""Sample message""",1,matches)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

If (matches.Count = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( "No matching message in INBOX.") & "</pre>"
    success = imap.Disconnect()
    If (success <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
        Response.End
    End If

    Response.End
End If

'  The search returned UIDs. Use the same identifier type for the fetch.
uid = matches.GetId(0)
set email = Server.CreateObject("Chilkat.Email")
success = imap.FetchEmail(0,uid,1,email)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Mailbox INBOX, UIDVALIDITY " & imap.UidValidity & ", UID " & uid) & "</pre>"
success = imap.Disconnect()
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

'  The downloaded Email remains usable after disconnecting.
Response.Write "<pre>" & Server.HTMLEncode( "Subject: " & email.Subject & "  Files: " & email.NumAttachments) & "</pre>"

success = email.SaveEml("imap-received.eml")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( email.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Saved imap-received.eml.") & "</pre>"

%>
</body>
</html>