gpt4 book ai didi

delphi - 使用 Indy 10 和 DELPHI 评估电子邮件

转载 作者:行者123 更新时间:2023-12-03 15:23:35 25 4
gpt4 key购买 nike

我使用以下代码来评估消息。使用 INDY 10 组件收到的电子邮件消息的内容(正文/行)

function LinesFromMsg(aMsg: TIdMessage): TStrings; 
var
i: Integer;
begin
for i := 0 to aMsg.MessageParts.AttachmentCount-1 do
begin
if (amsg.MessageParts.Items[i].ContentType ='HTML') then
begin
if (amsg.MessageParts.Items[i] is Tidtext) then
Result := TidText(amsg.MessageParts.Items[i]).body;
end;
end;
end;

关于这段代码我有两个问题:

a) 这是在任意邮件消息中查找 Tlines 部分的正确方法吗?(考虑 INDY 10 EMAIL MSG PARTS 中显示的建议)

b) 在哪里可以找到所有不同 Contenttype 字符串值的教程?

最佳答案

要查找的正确ContentType 值是text/html。使用 Indy 的 IsHeaderMediaType() 函数进行检查,因为 ContentType 值可能具有与其关联的其他属性,您的比较需要忽略这些属性。

您还需要考虑 TIdMessage.ContentType,因为 HTML 电子邮件可能未经过 MIME 编码,因此根本不使用 TIdMessage.MessageParts` 集合。

最后,您的循环需要使用 MessageParts.Count 属性而不是 MessageParts.AttachmentsCount 属性。

试试这个:

function HTMLFromMsg(aMsg: TIdMessage): TStrings; 
var
i: Integer;
Part: TIdMessagePart;
begin
Result := nil;
if IsHeaderMediaType(aMsg.ContentType, 'text/html') then
begin
Result := aMsg.Body;
Exit;
end;
for i := 0 to aMsg.MessageParts.Count-1 do
begin
Part := aMsg.MessageParts.Items[i];
if (Part is TIdText) and IsHeaderMediaType(Part.ContentType, 'text/html') then
begin
Result := TIdText(Part).Body;
Exit;
end;
end;
end;

话虽如此,从技术上讲这并不是处理 MIME 的正确方法。正式地,符合标准的读者应该向后循环浏览 MIME 部分,因为它们是从最简单的形式向下到最复杂的形式排序的。因此,您向后循环,考虑 MIME 嵌套,寻找您支持的最复杂的形式。更像这样的东西(未经测试):

procedure DisplayPlainText(Body: TStrings);
begin
// display plain text as needed...
end;

procedure DisplayHTML(Body: TStrings);
begin
// display html as needed...
end;

procedure DisplayMultiPartAlternative(aMsg: TIdMessage; aParentIndex, aLastIndex: Integer);
var
Part: TIdMessagePart;
i: Integer:
begin
for i := aLastIndex-1 downto aParentIndex+1 do
begin
Part := aMsg.MessageParts.Items[i];
if (Part.ParentPart = aParentIndex) and (Part is TIdText) then
begin
if IsHeaderMediaType(Part.ContentType, 'text/html') then
begin
DisplayHTML(TIdText(Part).Body);
Exit;
end;
if IsHeaderMediaType(Part.ContentType, 'text/plain') then
begin
DisplayPlainText(TIdText(Part).Body);
Exit;
end;
end;
end;
// nothing supported to display...
end;

procedure DisplayMultiPartMixed(aMsg: TIdMessage; aParentIndex, aLastIndex: Integer);
var
Part: TIdMessagePart;
i: Integer;
begin
for i := aLastIndex-1 downto aParentIndex+1 do
begin
Part := aMsg.MessageParts.Items[i];
if (Part.ParentPart = aParentIndex) and (Part is TIdText) then
begin
if IsHeaderMediaType(Part.ContentType, 'multipart/alternative') then
begin
DisplayMultiPartAlternative(aMsg, ParentPart.Index, aLastIndex);
Exit;
end;
if IsHeaderMediaType(ParentPart.ContentType, 'text/html') then
begin
DisplayHTML(TIdText(Part).Body);
Exit;
end;
if IsHeaderMediaType(Part.ContentType, 'text/plain') then
begin
DisplayPlainText(TIdText(Part).Body);
Exit;
end;
aLastIndex := i;
end;
end;
// nothing supported to display...
end;

procedure DisplayMsg(aMsg: TIdMessage);
var
ContentType: string;
begin
ContentType := ExtractHeaderMediaType(aMsg.ContentType);
case PosInStrArray(ContentType, ['multipart/mixed', 'multipart/alternative', 'text/html', 'text/plain'], False) of
0: begin
DisplayMultiPartAlternative(aMsg, -1, aMsg.MessageParts.Count);
Exit;
end;
1: begin
DisplayMultiPartMixed(aMsg, -1, aMsg.MessageParts.Count);
Exit;
end;
2: begin
DisplayHTML(aMsg.Body);
Exit;
end;
3: begin
DisplayPlainText(aMsg.Body);
Exit;
end;
else
// nothing supported to display...
end;
end;

关于delphi - 使用 Indy 10 和 DELPHI 评估电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14671010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com