Delphi DLL
Delphi DLL
POP3 to SMTP Forwarder
Read a POP3 mailbox and forwards the email to another email address, keeping the recipients in the original email the same.Chilkat Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, StringTable, BinData, MailMan;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
mailman: HCkMailMan;
fromAddr: PWideChar;
toAddr: PWideChar;
stUidls: HCkStringTable;
bdMime: HCkBinData;
count: Integer;
i: Integer;
uidl: PWideChar;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
mailman := CkMailMan_Create();
// Set the POP3 server's hostname
CkMailMan_putMailHost(mailman,'pop.example.com');
// Set the POP3 login/password.
CkMailMan_putPopUsername(mailman,'MY_POP3_LOGIN');
CkMailMan_putPopPassword(mailman,'MY_POP3_PASSWORD');
// Set the SMTP hostname for sending.
CkMailMan_putSmtpHost(mailman,'smtp.example.com');
CkMailMan_putSmtpUsername(mailman,'MY_SMTP_LOGIN');
CkMailMan_putSmtpPassword(mailman,'MY_SMTP_PASSWORD');
fromAddr := 'me@example.com';
toAddr := 'recipient@somewhere.com';
// The the UIDLs for all email in the POP3 mailbox.
stUidls := CkStringTable_Create();
success := CkMailMan_FetchUidls(mailman,stUidls);
if (success = False) then
begin
Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
Exit;
end;
// Just mark messages for delete, then delete at the end.
CkMailMan_putImmediateDelete(mailman,False);
// Download the email from the server. Call FetchMimeBd
// because we don't want to load the emails into email objects.
// (We'll delete the emails that are forwarded without error.)
bdMime := CkBinData_Create();
count := CkStringTable_getCount(stUidls);
i := 0;
while i < count do
begin
uidl := CkStringTable__stringAt(stUidls,i);
Memo1.Lines.Add('UIDL: ' + uidl);
success := CkMailMan_FetchMimeBd(mailman,uidl,bdMime);
if (success = False) then
begin
Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
Exit;
end;
success := CkMailMan_SendMimeBd(mailman,fromAddr,toAddr,bdMime);
if (success = False) then
begin
Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
Exit;
end;
success := CkMailMan_DeleteByUidl(mailman,uidl);
if (success = False) then
begin
Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
Exit;
end;
i := i + 1;
end;
// Delete messages marked for delete.
CkMailMan_putImmediateDelete(mailman,True);
success := CkMailMan_Pop3EndSession(mailman);
if (success = False) then
begin
Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
Exit;
end;
Memo1.Lines.Add('Success.');
CkMailMan_Dispose(mailman);
CkStringTable_Dispose(stUidls);
CkBinData_Dispose(bdMime);
end;