I'm using Delphi 10.2 Tokyo and Indy 10.6.2.5366.
I'm trying to receive some SMTP messages from a foreign system, and the messages received are using multi-part MIME. I need to extract some fields and values from the message.
When I read the body as in the Indy SMTPServer Demo, the body is empty, but the body is written to the saved file.
How do I read that body into a TStringList
? Is this a manual thing I must develop, or is there some method (couldn't find one) to do this?
This is what I do (Inspired from the demo):
procedure TForm1.IdSMTPServer1MsgReceive(ASender: TIdSMTPServerContext;
AMsg: TStream; var LAction: TIdDataReply);
var
LMsg : TIdMessage;
LMsg2 : TIdMessage;
LStream : TFileStream;
i : integer;
AMsgpart : TidMessagePart;
begin
// When a message is received by the server, this event fires.
// The message data is made available in the AMsg : TStream.
// In this example, we will save it to a temporary file, and the load it using
// IdMessage and parse some header elements.
AddToLog('Msg recv '+formatdatetime('hh:nn:ss', Now));
LMsg2 := TIdMessage.Create;
try
LMsg2.LoadFromStream(AMsg);
ToLabel.Caption := LMsg2.Recipients.EMailAddresses;
FromLabel.Caption := LMsg2.From.Text;
SubjectLabel.Caption := LMsg2.Subject;
// not working
// Memo1.Lines := LMsg2.Body;
// so what to do with this multi-part message in MIME format.
for i := 0 to LMsg2.MessageParts.Count-1 do
begin
if (LMsg2.MessageParts.Items[i].PartType = mptText) then
begin
//AddToLog(LMsg2.MessageParts.Items[i].);
AMsgpart := LMsg2.MessageParts.Items[i];
end
end;
finally
FreeAndNil(LMsg2);
end;
// Just write to a file as in demo
LStream := TFileStream.Create(ExtractFilePath(Application.exename) + format('test(%d).eml',[mailno]), fmCreate);
Try
LStream.CopyFrom(AMsg, 0);
Finally
FreeAndNil(LStream);
End;
end;
The message stream when written to a file looks like this
Received: from WIN-2SP97MPF39L[192.168.10.141] (helo=DesigoCC1) by HPNB2.hnit.local[192.168.10.131] with esmtp (My SMTP Server)
From: Alarms@DCC.dk
Date: Fri, 02 Feb 2018 09:46:39 +0100
To: mail@mail.com
Subject: =?UTF-8?B?QWxhcm0=?=
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_NextPart_000_0006_01CAB9FA.E6640E80"
This is a multi-part message in MIME format.
------=_NextPart_000_0006_01CAB9FA.E6640E80
Content-Type: text/plain;
format=flowed;
charset="utf-8";
reply-type=original
Content-Transfer-Encoding: 7bit
Date:2/2/2018 9:45:03 AM:02/02 09:45
Category:Information:
Desc:Servers.Main Server:
DescCompl:Project.Management System.Servers.Main Server
Tag:Mail.SMTP Status
TagCompl:Mail.SMTP Status [Mail]
Status:Quiet
EventComplProject.Management System.Servers.Main Server
Message:N/A
Cause:Mail Error (Wrong SMTP Server):Mail Error
-----
Message ID: -(6138661-
------=_NextPart_000_0006_01CAB9FA.E6640E80--