📄 idimap4.pas
字号:
{ $HDR$}
{**********************************************************************}
{ Unit archived using Team Coherence }
{ Team Coherence is Copyright 2002 by Quality Software Components }
{ }
{ For further information / comments, visit our WEB site at }
{ http://www.TeamCoherence.com }
{**********************************************************************}
{}
{ $Log: 10205: IdIMAP4.pas
{
{ Rev 1.10 20/09/2003 20:09:38 CCostelloe
{ Support added for different servers; some restructuring also
}
{
{ Rev 1.9 12/08/2003 01:39:04 CCostelloe
{ Minor patches.
}
{
{ Rev 1.8 05/07/2003 23:55:22 CCostelloe
{ More bug fixes and improvements, also IFDEF INDY100's stripped out.
}
{
{ Rev 1.7 10/06/2003 21:04:20 CCostelloe
{ ReceiveBody added to resolve problems with some servers and
{ (UID)Receive(Peek) functions. See comments starting "CC6".
}
{
{ Rev 1.6 29/05/2003 22:47:02 CCostelloe
{ Major update, most of code rewritten. See comments in source. Tested
{ against CommuniGate and Cyrus IMAP servers.
}
{
{ Rev 1.5 12/05/2003 00:03:52 CCostelloe
{ Bug fix between Windows 98 and Windows 2000 resolved
}
{
{ Rev 1.4 11/05/2003 23:14:18 CCostelloe
{ More bug fixes plus IMAP-specific code moved up from TIdRFCReply.
}
{
{ Rev 1.3 08/05/2003 02:31:28 CCostelloe
}
{
{ Rev 1.2 15/04/2003 00:46:50 CCostelloe
}
{
{ Rev 1.1 01/04/2003 21:54:10 CCostelloe
}
{
{ Rev 1.0 2002.11.12 10:42:04 PM czhower
}
unit IdIMAP4;
{ ########********########********########********########********########********
WARNING:
KNOWN ISSUES AND WORKAROUNDS AS OF 20th July 2003:
(UID)RetrievePart(Peek) functions occasionally cause erratic crashes,
but only if invalid parameters are used (e.g. calling them with the
wrong encoding specified).
Some functions may still be susceptible to breakage by "unsolicited
responses" from an IMAP server. As an example, one server sends
"* 93 EXISTS" and "* 1 RECENT" when a new message (number 93)
arrives in the Inbox.
The Indy 9 version of AppendMsg does not support attachments.
The Indy 9 version of (UID)Retrieve(Peek) does not support body text
lines longer than 16K (these can arise with long html messages).
This is only for Windows, no work has been done on a Linux version.
Ciaran Costelloe
ccostelloe@flogas.ie
########********########********########********########********########********
}
{*
IMAP 4 (Internet Message Access Protocol - Version 4 Rev 1)
By Idan Cohen i_cohen@yahoo.com
2001-FEB-27 IC: First version most of the IMAP features are implemented and
the core IdPOP3 features are implemented to allow a seamless
switch.
The unit is currently oriented to a session connection and not
to constant connection, because of that server events that are
raised from another user actions are not supported.
2001-APR-18 IC: Added support for the session's connection state with a
special exception for commands preformed in wrong connection
states. Exceptions were also added for response errors.
2001-MAY-05 IC:
2001-Mar-13 DS: Fixed Bug # 494813 in CheckMsgSeen where LastCmdResult.Text
was not using the Ln index variable to access server
responses.
2002-Apr-12 DS: fixed bug # 506026 in TIdIMAP4.ListSubscribedMailBoxes. Call
ParseLSubResut instead of ParseListResult.
2003-Mar-31 CC: Added GetUID and UIDSearchMailBox, sorted out some bugs (details
shown in comments in those functions which start with "CC:").
2003-Apr-15 CC2:Sorted out some more bugs (details shown in comments in those
functions which start with "CC2:"). Set FMailBoxSeparator
in ParseListResult and ParseLSubResult.
Some IMAP servers generally return "OK completed" even if they
returned no data, such as passing a non-existent message
number to them: they possibly should return NO or BAD; the
functions here have been changed to return FALSE unless they
get good data back, even if the server answers OK. Similar
change made for other functions.
There are a few exceptions, e.g. ListMailBoxes may only return
"OK completed" if the user has no mailboxes, these are noted.
Also, RetrieveStructure(), UIDRetrieveStructure, RetrievePart,
UIDRetrievePart, RetrievePartPeek and UIDRetrievePartPeek
added to allow user to find the structure of a message and
just retrieve the part or parts he needs.
2003-Apr-30 CC3:Added functionality to retrieve the text of a message (only)
via RetrieveText / UIDRetrieveText / RetrieveTextPeek /
UIDRetrieveTextPeek.
Return codes now generally reflect if the function succeeded
instead of returning True even though function fails.
2003-May-15 CC4:Added functionality to retrieve individual parts of a message
to a file, including the decoding of those parts.
2003-May-29 CC5:Response of some servers to UID version of commands varies,
code changed to deal with those (UID position varies).
Some servers return NO such as when you request an envelope
for a message number that does not exist: functions return
False instead of throwing an exception, as was done for other
servers. The general logic is that if a valid result is
returned from the IMAP server, return True; if there is no
result (but the command is validly structured), return FALSE;
if the command is badly structured or if it gives a response
that this code does not expect, throw an exception (typically
when we get a BAD response instead of OK or NO).
Added IsNumberValid, IsUIDValid to prevent rubbishy parameters
being passed through to IMAP functions.
Sender field now filled in correctly in ParseEnvelope
functions.
All fields in ParseEnvelopeAddress are cleared out first,
avoids an unwitting error where some entries, such as CC list,
will append entries to existing entries.
Full test script now used that tests every TIdIMAP command,
more bugs eradicated.
First version to pass testing against both CommuniGate and
Cyrus IMAP servers.
Not tested against Microsoft Exchange, don't have an Exchange
account to test it against.
2003-Jun-10 CC6:Added (UID)RetrieveEnvelopeRaw, in case the user wants to do
their own envelope parsing.
Code in RetrievePart altered to make it more consistent.
Altered to incorporate Indy 10's use of IdReplyIMAP4 (not
complete at this stage).
ReceiveBody added to IdIMAP4, due to the response of some
servers, which gets (UID)Receive(Peek) functions to work on
more servers.
2003-Jun-20 CC7:ReceiveBody altered to work with Indy 10. Made changes due to
LoginSASL moving from TIdMessageSASLClient to TIdSASLList.
Public variable FGreetingBanner added to help user identify
the IMAP server he is connected to (may help him decide the
best strategy). Made AppendMsg work a bit better (now uses
platform-independent EOL and supports ExtraHeaders field).
Added 2nd version of AppendMsg. Added "if Connected then "
to Destroy. Attachment filenames now decoded if necessary.
Added support for multisection parts.
2003-Jul-16 CC8:Added RemoveAnyAdditionalResponses. Resolved issue of some
servers leaving out the trailing "NIL NIL NIL" at the end of
some body structures. (UID)Retrieve(Peek) functions
integrated via InternalRetrieve, new method of implementing
these functions (all variations of Retrieve) added for Indy
10 based on getting message by the byte-count and then feeding
it into the standard message parser.
UIDRetrieveAllHeaders removed: it was never implemented anyway
but it makes no sense to retrieve a non-contiguous list which
would have gaps due to missing UIDs.
In the Indy 10 version, AppendMsg functions were altered to
support the sending of attachments (attachments had never
been supported in AppendMsg prior to this).
Added CopyMsg and UIDCopyMsgs to complete the command set.
2003-Jul-30 CC9:Removed wDoublePoint so that the code is compliant with
the guidelines. Allowed for servers that don't implement
search commands in Indy 9 (OK in 10). InternalRetrieve
altered to (hopefully) deal with optional "FLAGS (\Seen)"
in response.
2003-Aug-22 CCA:Yet another IMAP oddity - a server returns NIL for the
mailbox separator, ParseListResult modified. Added "Length
(LLine) > 0)" test to stop GPF on empty line in ReceiveBody.
*}
{ TODO -oIC :
Change the mailbox list commands so that they receive TMailBoxTree
structures and so they can store in them the mailbox name and it's attributes. }
{ TODO -oIC :
Add support for \* special flag in messages, and check for \Recent
flag in STORE command because it cant be stored (will get no reply!!!) }
{ TODO -oIC :
5.1.2. Mailbox Namespace Naming Convention
By convention, the first hierarchical element of any mailbox name
which begins with "#" identifies the "namespace" of the remainder of
the name. This makes it possible to disambiguate between different
types of mailbox stores, each of which have their own namespaces.
For example, implementations which offer access to USENET
newsgroups MAY use the "#news" namespace to partition the USENET
newsgroup namespace from that of other mailboxes. Thus, the
comp.mail.misc newsgroup would have an mailbox name of
"#news.comp.mail.misc", and the name "comp.mail.misc" could refer
to a different object (e.g. a user's private mailbox). } {Do not Localize}
{ TO BE CONSIDERED -CC :
Double-quotes in mailbox names can cause major but subtle failures. Maybe
add the automatic stripping of double-quotes if passed in mailbox names,
to avoid ending up with ""INBOX""
}
interface
{CC3: WARNING - if the following gives a "File not found" error on compilation,
you need to add the path "C:\Program Files\Borland\Delphi7\Source\Indy" in
Project -> Options -> Directories/Conditionals -> Search Path}
{$I IdCompilerDefines.inc}
uses
IdMessage,
Classes,
SysUtils, {CC3: SysUtils added to support Exception}
IdAssignedNumbers,
IdGlobal,
IdMailBox,
IdTCPStream,
IdMessageClient,
IdComponent, {CC6: Now needed for ReceiveBody}
IdMessageCoder, {CC2: Now needed for parsing BODYSTRUCTURE}
IdHeaderList, {CC7: Added for 2nd version of AppendMsg}
IdCoderHeader, {CC7: Needed for decoding filenames}
IdCoderMIME,
IdCoderQuotedPrintable,
IdMessageCollection;
const
wsOk = 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -