gpt4 book ai didi

delphi - 多部分/混合消息中的边界字符串不正确

转载 作者:行者123 更新时间:2023-12-03 15:11:59 24 4
gpt4 key购买 nike

我正在使用 Delphi 2006 在仅供个人使用的应用程序中创建并发送带有附件的电子邮件。我使用 TIdSMTP 实例发送邮件,然后将副本放入包含 TIdIMAP4 实例的特定 IMAP 文件夹中。这一切都与 BDS2006 一起分发的 Indy 10 版本配合得很好,但有一个异常(exception):电子邮件 header 中的时间总是不正确。

我决定尽可能修复这个问题,在寻找解决方案后,获取最新的 Indy 10 快照并使用它似乎是最合理的。

这会将正确的时间放入电子邮件标题中,但出现了一个新问题。现在,添加到 IMAP 文件夹的邮件 header 中的边界字符串与电子邮件正文中的边界字符串不同! (请注意,通过 SMTP 发送的消息是正确的。)

这是旧版本 Indy 10 的相关 header 信息:

Content-Type: multipart/mixed; boundary="XNlC6OyS4QSiHY2U=_jsXyps6TR34pFNsh"
MIME-Version: 1.0
Date: Tue, 22 Nov 2011 09:11:58 +0000

A test of the BDS2006-bundled version of Indy

--XNlC6OyS4QSiHY2U=_jsXyps6TR34pFNsh
Content-Type: application/octet-stream;
name="20111122.xls"

这与 Indy 10.5.8 中的 header 信息(我昨天安装的快照 10_4702)相同:

Content-Type: multipart/mixed; boundary="CDbEncbFvL7RZdOJ3DOIRoRBs=_nBsbZms"
MIME-Version: 1.0
Date: Tue, 22 Nov 2011 07:33:46 -0600

investigating more deeply, why does the boundary indicator change?

--h=_WzGWJei29fng7SqdPpDh1nkJxJZhiGc
Content-Type: application/octet-stream;
name="20111122.xls"

时间戳已修复,但现在边界字符串不正确。结果是,添加到我的 IMAP 文件夹中的邮件中似乎没有任何内容。

以下是创建电子邮件和附件、发送电子邮件并将副本放入 IMAP 文件夹的相关代码:

  FTheMsg.Date := Now;  // FTheMsg is a component dropped onto the form
FTheMsg.Recipients.EMailAddresses := edMailTo.Text;
FTheMsg.ClearBody;
FTheMsg.Subject := 'Glucose Readings ' + FormatDateTime('mm/dd/yy', FStartDate) + ' - ' +
FormatDateTime('mm/dd/yy', FEndDate);
FTheMsg.Body.Assign(FMemo.Lines);

// create the attachment
TIdAttachmentFile.Create(FTheMsg.MessageParts, fileName);

// send the mail!
FSmtp.Host := FSMTPHost; // values set up elsewhere, FSmtp is a component on the form
FImap.Host := FIMAPHost; // FImap is also a component on the form

FSmtp.Connect;
try
FSmtp.Send(FTheMsg);
FImap.Connect;
try
if (not FImap.AppendMsg('Sent Items', FTheMsg, FTheMsg.LastGeneratedHeaders, [mfSeen])) then
StatusBar1.Panels[4].Text := 'Failed append msg';
finally
FImap.Disconnect;
end;
finally
FSmtp.Disconnect;
end;

正如我所说,发送的电子邮件很好并且显示正常。但添加到我的 IMAP 文件夹(在上面的 FImap.AppendMsg() 中)的文件夹不正确。我试图追踪代码,看看哪里可能出了问题,但坦率地说,我对 Indy 和各种电子邮件协议(protocol)/RFC 不够熟悉,无法确定出了什么问题。我所能告诉的是,旧版本在将消息附加到文件夹之前将其保存到临时文件中,而新版本则将其保存到内存流中。显然,有些东西是不同的,但我目前太无知,无法确定是什么。

有没有简单的方法来纠正旧版本中的时间戳问题?如果是这样,那对我来说就很好了,因为其他一切似乎都是正确的。如果不是,我还需要做什么来解决此处显示的边界字符串不正确的问题?

(由于这是一个仅供我自己使用的应用程序,如果有必要的话,我可以接受错误的日期,但不能接受“已发送邮件”文件夹中的“空的”副本。)

如果需要更多信息,我很乐意提供任何信息。

[编辑:我确实使用旧版本的 Indy 在我的代码中加入了一些拼凑的东西。我只是在发送消息之前将消息的日期/时间设置为 UTC/GMT 时间,这样至少允许消息在接收方端包含正确的时间。我并不特别关心这个修复,但它确实有效。]

最佳答案

当存在附件时,请勿使用 TIdMessage.Body 属性来保存文本。将文本放入 TIdText 对象中。更好的是,使用 TIdMessageBuilder... 类(例如 TIdMessageBuilderPlain)为您准备 TIdMessage 正文。

试试这个:

FTheMsg.Clear; 
FTheMsg.Date := Now; // FTheMsg is a component dropped onto the form
FTheMsg.Recipients.EMailAddresses := edMailTo.Text;
FTheMsg.Subject := 'Glucose Readings ' + FormatDateTime('mm/dd/yy', FStartDate) + ' - ' + FormatDateTime('mm/dd/yy', FEndDate);
FTheMsg.ContentType := 'multipart/mixed';

TIdText.Create(FTheMsg.MessageParts, FMemo.Lines).ContentType := 'text/plain';
TIdAttachmentFile.Create(FTheMsg.MessageParts, fileName);

FSmtp.Connect;
try
FSmtp.Send(FTheMsg);
FImap.Connect;
try
if (not FImap.AppendMsg('Sent Items', FTheMsg, nil, [mfSeen])) then
StatusBar1.Panels[4].Text := 'Failed append msg';
finally
FImap.Disconnect;
end;
finally
FSmtp.Disconnect;
end;

或者:

FTheMsg.Clear; 
FTheMsg.Date := Now; // FTheMsg is a component dropped onto the form
FTheMsg.Recipients.EMailAddresses := edMailTo.Text;
FTheMsg.Subject := 'Glucose Readings ' + FormatDateTime('mm/dd/yy', FStartDate) + ' - ' + FormatDateTime('mm/dd/yy', FEndDate);

with TIdMessageBuilderPlain.Create do
try
PlainText.Assign(FMemo.Lines);
Attachments.Add(fileName);
FillMessage(FTheMsg);
finally
Free;
end;

FSmtp.Connect;
try
FSmtp.Send(FTheMsg);
FImap.Connect;
try
if (not FImap.AppendMsg('Sent Items', FTheMsg, nil, [mfSeen])) then
StatusBar1.Panels[4].Text := 'Failed append msg';
finally
FImap.Disconnect;
end;
finally
FSmtp.Disconnect;
end;

现在,话虽如此,但无论哪种方式它都可能仍然无法正常工作。 TIdIMAP4.AppendMsg() 在内部调用 TIdMessage.SaveToStream(),这会重新生成最新的电子邮件内容(从而更改正文中使用的边界)。无论您传入预先存在的 TIdMessage.LastGenerateHeaders 还是让 TIdIMAP4.AppendMsg() 获取当前的 TIdMessage.Headers,它们都会消失与 TIdMessage.SaveToStream() 生成的新边界同步。

为了确保 SMTP 和 IMAP4 同步,它们需要接收相同的数据。首先尝试手动调用 TIdMessage.SaveToStream(),并将 TIdMessage.NoEncode 属性设置为 False,然后将 TIdMessage.NoDecode 属性设置为 True,然后调用 TIdMessage.LoadFromStream() 将保存的数据按原样重新加载到 TIdMessage.HeadersTIdMessage.Body 属性中,然后调用 TIdSMTP.Send()TIdIMAP4.AppendMsg(),其中 TIdMessage.NoEncode 属性设置为 True,因此 TIdMessage.HeadersTIdMessage.Body 按原样发送。

我知道,这违背了 TIdIMAP4.AppendMsg() 注释/文档所说的做法。 AppendMsg() 目前根本不考虑 MIME,因此它不能确保 header 和正文中的 MIME 边界相互匹配。我将尝试检查该问题的修复程序。对于 Indy 11,Indy 的整个 MIME 处理系统将重新设计,因此我将确保可以保留边界和/或指定自定义边界,以便 AppendMsg() 可以匹配正文边界到标题边界更好。

IMAP4 通常是一个非常棘手的协议(protocol)。

关于delphi - 多部分/混合消息中的边界字符串不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8229843/

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