Sample code for 30+ languages & platforms
Delphi DLL

Append MIME with Initial IMAP Flags

See more IMAP Examples

Demonstrates the Chilkat Imap.AppendMimeWithFlags method, which uploads a MIME message and sets its initial system flags. The first argument is the mailbox and the second is the MIME text. The third through sixth arguments control \Seen, \Flagged, \Answered, and \Draft respectively — pass true to set a flag. This example uploads a message that is already marked read.

Background: When importing or archiving mail you frequently want the uploaded message to arrive in a particular state — already read, pre-flagged, or marked as a draft. Setting the flags at append time is a single operation, avoiding a second STORE round trip. These explicit arguments take precedence over the AppendSeen property.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
imap: HCkImap;
email: HCkEmail;
mimeText: PWideChar;
seen: Boolean;
flagged: Boolean;
answered: Boolean;
draft: Boolean;

begin
success := False;

//  Demonstrates the Imap.AppendMimeWithFlags method, which uploads a MIME message and sets its
//  initial system flags.  The 1st argument is the mailbox, the 2nd is the MIME text, and the
//  3rd through 6th arguments set the \Seen, \Flagged, \Answered, and \Draft flags.

imap := CkImap_Create();

CkImap_putSsl(imap,True);
CkImap_putPort(imap,993);

success := CkImap_Connect(imap,'imap.example.com');
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;
success := CkImap_Login(imap,'user@example.com','myPassword');
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;

//  Build the email to be uploaded.
email := CkEmail_Create();
CkEmail_putSubject(email,'Meeting agenda');
CkEmail_putFrom(email,'Alice <alice@example.com>');
success := CkEmail_AddTo(email,'Bob','bob@example.com');
CkEmail_putBody(email,'Let''s meet at 10am to review the agenda.');

mimeText := CkEmail__getMime(email);
if (CkEmail_getLastMethodSuccess(email) = False) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;

//  Choose the initial flags.  Named variables make each argument's meaning clear at the call.
seen := True;
flagged := False;
answered := False;
draft := False;

//  Upload to the Inbox already marked as read (\Seen), with the other flags unset.
success := CkImap_AppendMimeWithFlags(imap,'Inbox',mimeText,seen,flagged,answered,draft);
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;

Memo1.Lines.Add('Appended UID: ' + IntToStr(CkImap_getAppendUid(imap)));

success := CkImap_Disconnect(imap);
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;

CkImap_Dispose(imap);
CkEmail_Dispose(email);