Title: Backup Your Outlook Attachments.
Question: How to Backup your Outlook attachments?
Answer:
Wont some backups of your outlook attachments are filtered some incoming log files? Here's the function.
-------- WARNING --------
Warning! All your selected Email will be deleted if MailDelete = true
Be careful and don't complaining afterwards to me if something goes wrong.
It's on your own risk!!!
-------- WARNING --------
Have Fun!
// uses ComObj
function TForm1.ManageAttachments(SendersName, AttachmentPath : string; MailDelete : boolean):boolean;
var
oApp : variant;
oNs : variant;
oFolder : variant;
oMsg : variant;
AtC : variant;
AttFilename : variant;
filename : string;
CheckSender : string;
Counter : integer;
MailCounter : integer;
begin
try
oApp := CreateOLEObject('outlook.application');
try
oNs := oApp.GetNamespace('MAPI');
ofolder := oNS.GetDefaultFolder(6); // FolderTypeEnum (olFolderInbox)
MailCounter := 1;
// If there is any email in the Inbox
if ofolder.items.count 0 then
begin
repeat
// Get the first Email
oMsg := ofolder.items(MailCounter);
// Check the name or Email
//
// Use CheckSender := oMsg.subject to search on Subject;
CheckSender := oMsg.sendername;
if CheckSender = SendersName then // Remove this line to backup all your attachments.
begin
// Check how many attachments
atc := oMsg.Attachments.count;
if atc 0 then
begin
// Get all the attachments and save them
for Counter := 1 to atc do
begin
AttFilename := oMsg.Attachments.item(Counter).filename;
filename := IncludeTrailingBackslash( AttachmentPath)+AttFilename;
oMsg.Attachments.Item(Counter).SaveAsFile(filename);
end;
end;
if MailDelete then
begin
oMsg.delete;
// There's 1 Email less, so MailCounter - 1
dec(MailCounter);
end;
end;
// Get the next Email
inc(MailCounter);
// Do until there is no more Email to check
until MailCounter ofolder.items.count;
end;
finally
oApp.quit;
end;
except
result := false;
exit;
end;
result := true;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
// ManageAttachments(Email or name, Backup directory, MailDelete yes or no)
ManageAttachments('info@cleys.com','F:\test',false);
end;
Have Fun!