procedure TForm1.ExtractPlainTextFromEMLFile;
// Drop a TIdMessage component on the form and name it "IdMessage"
var
IndyStringList: TStringList;
IndyStream: TMemoryStream;
I: Integer;
HTMLBody, PlaintextBody:
string;
const
CRLF = #13#10;
begin
IndyStringList := TStringList.Create;
IndyStream := TMemoryStream.Create;
try
IndyStringList.LoadFromFile(Trim(Edit1.Text));
IndyStringList.Add(CRLF + '
.' + CRLF);
IndyStringList.SaveToStream(IndyStream);
IndyStream.Position := 0;
IdMessage.Clear;
IdMessage.LoadFromStream(IndyStream);
Memo1.Clear;
Memo1.Lines.Assign(IdMessage.Headers);
Memo2.Clear;
Memo2.Lines.Add(IdMessage.From.Text);
Memo2.Lines.Add(IdMessage.Recipients.EMailAddresses);
Memo2.Lines.Add(IdMessage.Subject);
Memo2.Lines.Add(DateTimeToStr(IdMessage.Date));
Memo2.Lines.Add('
');
if (Pos('
multi-part', IdMessage.ContentType) > 0)
or (Pos('
multipart', IdMessage.ContentType) > 0)
then
begin
for I := 0
to IdMessage.MessageParts.Count - 1
do
begin
if IdMessage.MessageParts.Items[I]
is IdText.TidText
then
begin
if Pos('
text/plain', IdText.TidText(IdMessage.MessageParts.Items[I]).ContentType) > 0
then
begin
PlaintextBody := IdText.TidText(IdMessage.MessageParts.Items[I]).Body.Text;
BREAK;
end
else if IdText.TidText(IdMessage.MessageParts.Items[I]).ContentType = '
text/html'
then
HTMLBody := IdText.TidText(IdMessage.MessageParts.Items[I]).Body.Text;
end;
end;
if PlaintextBody <> '
'
then
Memo2.Lines.Add(Trim(PlaintextBody))
else if HTMLBody <> '
'
then
Memo2.Lines.Add(ExtractTextFrom(HTMLBody))
else Memo2.Lines.Add('
The body could not be extracted from the .EML file. Please report this error and attach your .EML file');
end
else
begin
Memo2.Lines.Add(Trim(IdMessage.Body.Text));
end;
finally
IndyStringList.Free;
IndyStream.Free;
end;
end;