为什么 body 和 sender 是空的?
Why body and sender are empty?
我正在使用此代码从服务器读取电子邮件,它可以正常工作,除了 Sender.Address
和 Body.Text
是空的,这是为什么?。这是代码:
var
MsgCount : Integer;
i : Integer;
FMailMessage : TIdMessage;
begin
Memo1.Lines.Clear;
//The IdPop31 is on the form so it is constructing when the
//form is created and so is Memo1.
IdPOP31.Host := 'server.com'; //Setting the HostName;
IdPOP31.Username := 'email@server.com';//Setting UserName;
IdPOP31.Password := 'xxxxxx';//Setting Password;
IdPOP31.Port := 110;//Setting Port;
try
IdPOP31.Connect();
//Getting the number of the messages that server has.
MsgCount := IdPOP31.CheckMessages;
for i:= 1 to Pred(MsgCount) do
begin
try
FMailMessage := TIdMessage.Create(nil);
IdPOP31.Retrieve(i,FMailMessage);
Memo1.Lines.Add('=================================================');
Memo1.Lines.Add(FMailMessage.From.Address);
Memo1.Lines.Add(FMailMessage.Recipients.EMailAddresses);
Memo1.Lines.Add(FMailMessage.Subject);
Memo1.Lines.Add(FMailMessage.Sender.Address);
Memo1.Lines.Add(FMailMessage.Body.Text);
Memo1.Lines.Add('=================================================');
finally
FMailMessage.Free;
end;
end;
finally
IdPOP31.Disconnect;
end;
end;
仅当电子邮件具有 top-level Sender
header 时才会填充 TIdMessage.Sender
,这种情况很少见。通常发件人在 From
header 中。
body 内容将存储在 TIdMessage.Body
或 TIdMessage.MessageParts
中,具体取决于电子邮件的编码方式。通常,multi-piece 封电子邮件,例如使用 MIME 编码的电子邮件,特别是如果它们包含附件,将使用 TIdMessage.MessageParts
,而简单的电子邮件,如 plaintext-only 封电子邮件,将使用 TIdMessage.Body
。因此,您需要根据需要检查两者。
例如:
var
MsgCount, I: Integer;
FMailMessage: TIdMessage;
Body: TStrings;
function FindTextBody(AParent: Integer): TStrings;
var
J: integer;
Part: TIdMessagePart;
begin
Result := nil;
// MIME parts are ordered from least complex to most complex, and can be nested,
// so loop backwards through the parts, recursing through nested levels as needed...
for J := Pred(FMailMessage.MessageParts.Count) downto (AParent+1) do
begin
Part := FMailMessage.MessageParts[J];
if Part.ParentPart = AParent then
begin
if IsHeaderMediaType(Part.ContentType, 'multipart') then
begin
Result := FindTextBody(Part.Index);
if Result <> nil then Exit;
end
else if IsHeaderMediaType(Part.ContentType, 'text') then
begin
Result := (Part as TIdText).Body;
Exit;
end;
end;
end;
end;
begin
Memo1.Lines.Clear;
//The IdPop31 is on the form so it is constructing when the
//form is created and so is Memo1.
IdPOP31.Host := 'server.com'; //Setting the HostName;
IdPOP31.Username := 'email@server.com';//Setting UserName;
IdPOP31.Password := 'xxxxxx';//Setting Password;
IdPOP31.Port := 110;//Setting Port;
try
IdPOP31.Connect();
//Getting the number of the messages that server has.
MsgCount := IdPOP31.CheckMessages;
for I := 1 to Pred(MsgCount) do
begin
FMailMessage := TIdMessage.Create(nil);
try
IdPOP31.Retrieve(I, FMailMessage);
Memo1.Lines.Add('=================================================');
Memo1.Lines.Add(FMailMessage.From.Address);
Memo1.Lines.Add(FMailMessage.Recipients.EMailAddresses);
Memo1.Lines.Add(FMailMessage.Subject);
Memo1.Lines.Add(FMailMessage.Sender.Address);
if FMailMessage.MessageParts.Count > 0 then
Body := FindTextBody(-1)
else
Body := FMailMessage.Body;
if Body <> nil then
Memo1.Lines.Add(Body.Text);
Memo1.Lines.Add('=================================================');
finally
FMailMessage.Free;
end;
end;
finally
IdPOP31.Disconnect;
end;
end;
我正在使用此代码从服务器读取电子邮件,它可以正常工作,除了 Sender.Address
和 Body.Text
是空的,这是为什么?。这是代码:
var
MsgCount : Integer;
i : Integer;
FMailMessage : TIdMessage;
begin
Memo1.Lines.Clear;
//The IdPop31 is on the form so it is constructing when the
//form is created and so is Memo1.
IdPOP31.Host := 'server.com'; //Setting the HostName;
IdPOP31.Username := 'email@server.com';//Setting UserName;
IdPOP31.Password := 'xxxxxx';//Setting Password;
IdPOP31.Port := 110;//Setting Port;
try
IdPOP31.Connect();
//Getting the number of the messages that server has.
MsgCount := IdPOP31.CheckMessages;
for i:= 1 to Pred(MsgCount) do
begin
try
FMailMessage := TIdMessage.Create(nil);
IdPOP31.Retrieve(i,FMailMessage);
Memo1.Lines.Add('=================================================');
Memo1.Lines.Add(FMailMessage.From.Address);
Memo1.Lines.Add(FMailMessage.Recipients.EMailAddresses);
Memo1.Lines.Add(FMailMessage.Subject);
Memo1.Lines.Add(FMailMessage.Sender.Address);
Memo1.Lines.Add(FMailMessage.Body.Text);
Memo1.Lines.Add('=================================================');
finally
FMailMessage.Free;
end;
end;
finally
IdPOP31.Disconnect;
end;
end;
仅当电子邮件具有 top-level Sender
header 时才会填充 TIdMessage.Sender
,这种情况很少见。通常发件人在 From
header 中。
body 内容将存储在 TIdMessage.Body
或 TIdMessage.MessageParts
中,具体取决于电子邮件的编码方式。通常,multi-piece 封电子邮件,例如使用 MIME 编码的电子邮件,特别是如果它们包含附件,将使用 TIdMessage.MessageParts
,而简单的电子邮件,如 plaintext-only 封电子邮件,将使用 TIdMessage.Body
。因此,您需要根据需要检查两者。
例如:
var
MsgCount, I: Integer;
FMailMessage: TIdMessage;
Body: TStrings;
function FindTextBody(AParent: Integer): TStrings;
var
J: integer;
Part: TIdMessagePart;
begin
Result := nil;
// MIME parts are ordered from least complex to most complex, and can be nested,
// so loop backwards through the parts, recursing through nested levels as needed...
for J := Pred(FMailMessage.MessageParts.Count) downto (AParent+1) do
begin
Part := FMailMessage.MessageParts[J];
if Part.ParentPart = AParent then
begin
if IsHeaderMediaType(Part.ContentType, 'multipart') then
begin
Result := FindTextBody(Part.Index);
if Result <> nil then Exit;
end
else if IsHeaderMediaType(Part.ContentType, 'text') then
begin
Result := (Part as TIdText).Body;
Exit;
end;
end;
end;
end;
begin
Memo1.Lines.Clear;
//The IdPop31 is on the form so it is constructing when the
//form is created and so is Memo1.
IdPOP31.Host := 'server.com'; //Setting the HostName;
IdPOP31.Username := 'email@server.com';//Setting UserName;
IdPOP31.Password := 'xxxxxx';//Setting Password;
IdPOP31.Port := 110;//Setting Port;
try
IdPOP31.Connect();
//Getting the number of the messages that server has.
MsgCount := IdPOP31.CheckMessages;
for I := 1 to Pred(MsgCount) do
begin
FMailMessage := TIdMessage.Create(nil);
try
IdPOP31.Retrieve(I, FMailMessage);
Memo1.Lines.Add('=================================================');
Memo1.Lines.Add(FMailMessage.From.Address);
Memo1.Lines.Add(FMailMessage.Recipients.EMailAddresses);
Memo1.Lines.Add(FMailMessage.Subject);
Memo1.Lines.Add(FMailMessage.Sender.Address);
if FMailMessage.MessageParts.Count > 0 then
Body := FindTextBody(-1)
else
Body := FMailMessage.Body;
if Body <> nil then
Memo1.Lines.Add(Body.Text);
Memo1.Lines.Add('=================================================');
finally
FMailMessage.Free;
end;
end;
finally
IdPOP31.Disconnect;
end;
end;