如果可以的话,我决定解决这个问题,在寻找解决方案之后,获取最新的Indy 10快照并使用它是最合理的.
这会将正确的时间放入电子邮件标题中,但是会出现一个新问题.现在,添加到IMAP文件夹的邮件标题中的边界字符串与邮件正文中的边界字符串不同! (请注意,通过SMTP发送的邮件是正确的.)
这是来自Indy 10旧版本的相关标题信息:
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(我昨天安装的快照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和各种电子邮件协议/ RFC不够熟悉,无法确定出现了什么问题.关于我可以告诉的是,旧版本将消息保存到临时文件,然后将其附加到文件夹,而较新版本则将其保存到内存流中.显然,有些事情是不同的,但我现在太无知,无法确定什么.
有没有一种简单的方法来纠正旧版本中的时间戳问题?如果是这样,那对我的使用来说没问题,因为其他一切看起来都是正确的.如果没有,我还需要做些什么来解决这里展示的问题与不正确的边界字符串?
(由于这是一个严格用于我自己的应用程序,如果必须,我可以使用不正确的日期,但不能使用“已发送邮件”文件夹中的“空出现”副本.)
如果需要更多信息,我很乐意尽我所能.
[编辑:我确实在我的代码中加入了一些kludge,使用旧版本的Indy.我只是在发送之前将消息的日期/时间设置为UTC / GMT时间,并且至少允许消息在接收方端包含正确的时间.我并不特别关心这个问题,但是它可以解决这个问题.]
解决方法
试试这个:
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',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',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',[mfSeen])) then StatusBar1.Panels[4].Text := 'Failed append msg'; finally FImap.Disconnect; end; finally FSmtp.Disconnect; end;
现在,说到这一点,它可能无论如何仍然无法正常工作. TIdIMAP4.AppendMsg()在内部调用TIdMessage.SaveToStream(),它会重新生成电子邮件内容(从而改变正文中使用的边界).无论是传入预先存在的TIdMessage.LastGeneratedHeaders还是让TIdIMAP4.AppendMsg()获取当前的TIdMessage.Headers,它们都将与TIdMessage.SaveToStream()生成的新边界不同步.
要确保SMTP和IMAP4同步,他们需要接收相同的数据.首先尝试手动调用TIdMessage.SaveToStream(),并将TIdMessage.NoEncode属性设置为False,然后将TIdMessage.NoDecode属性设置为True并调用TIdMessage.LoadFromStream()将已保存的数据按原样重新加载到TIdMessage.Headers和TIdMessage中.Body属性,然后调用TIdSMTP.Send()和TIdIMAP4.AppendMsg()并将TIdMessage.NoEncode属性设置为True,以便按原样发送TIdMessage.Headers和TIdMessage.Body.
我知道,这与TIdIMAP4.AppendMsg()评论/文档所说的相反. AppendMsg()目前根本不考虑MIME,因此它不能确保标题和正文中的MIME边界相互匹配.我会尝试检查一下.对于Indy 11,Indy的整个MIME处理系统将进行重新设计,因此我将确保可以保留边界和/或指定自定义边界,因此AppendMsg()可以将身体边界与标题边界更好地匹配.
IMAP4是一个非常棘手的协议.