使用德尔福11。
使用
TIdIMAP4
下载电子邮件后,扫描 MessageParts
,如何检测 MessagePart
是否包含 MIME 编码附件,以及如何将其解码为原始格式(图像、文档等)?
procedure TImapForm.ProcessEmail(MSG: TIdMessage);
begin
ContainsAttachement := false;
if Msg.MessageParts.Count > 0 then
begin
for i := 0 to Pred(Msg.MessageParts.Count) do
begin
if Msg.MessageParts.Items[i] is TIdText then
begin
// Process Text-only message here (Don't want HTML)
end else
begin
ContainsAttachement := true;
// if Msg.MessageParts[i].MessageParts.Encoding = meMIME then
if MSG.ContentTransferEncoding = 'base64' then //??
if Msg.MessageParts.Items[i].IsEncoded then //??
begin
// How to actually decode the MessagePart to a binary file?
end;
end;
end;
end;
end;
电子邮件示例:
This is a multi-part message in MIME format.
--------------wt6iyRLyQwO4w89MYm2jGb0w
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
test
--------------wt6iyRLyQwO4w89MYm2jGb0w
Content-Type: image/jpeg; name="Bart.jpg"
Content-Disposition: attachment; filename="Bart.jpg"
Content-Transfer-Encoding: base64
/9j/4AAQSkZJRgABAQEASABIAAD/4ThFRXhpZgAASUkqAAgAAAAMAA4BAgAgAAAAngAAAA8B
AgAUAAAAvgAAABABAgAHAAAA1gAAABIBAwABAAAAAQAAABoBBQABAAAA7gAAABsBBQABAAAA
9gAAACgBAwABAAAAAgAAADEBAgAIAAAA/gAAADIBAgAUAAAAHgEAABMCAwABAAAAAgAAAGmH
etc.
TIdMessage
已经为你处理好了。寻找 TIdAttachmeent
而不是 TIdText
,例如:
procedure TImapForm.ProcessEmail(MSG: TIdMessage);
begin
ContainsAttachement := false;
for i := 0 to Pred(Msg.MessageParts.Count) do
begin
if Msg.MessageParts.Items[i] is TIdText then
begin
// Process Text-only message here (Don't want HTML)
end
else if Msg.MessageParts.Items[i] is TIdAttachment then
begin
ContainsAttachement := true;
TIdAttachment(Msg.MessageParts.Items[i]).SaveToFile(...);
end;
end;
end;