我正在使用Delphi 2006在个人使用的应用程序中创建和发送带有附件的电子邮件.我使用TIdSMTP实例发送消息,然后将副本放入具有TIdIMAP4实例的特定IMAP文件夹中.这与使用BDS2006一起分发的Indy 10版本非常吻合,但有一个例外:电子邮件标题中的时间始终不正确.
如果可以的话,我决定解决这个问题,在寻找解决方案之后,获取最新的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"
Run Code Online (Sandbox Code Playgroud)
这是来自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"
Run Code Online (Sandbox Code Playgroud)
时间戳是固定的,但现在边界字符串不正确.结果是,消息中似乎什么也没有添加到我的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;
Run Code Online (Sandbox Code Playgroud)
正如我所说,发送的电子邮件很好并且显示正常.但是添加到我的IMAP文件夹(在上面的FImap.AppendMsg()中)的那个是不正确的.我试图浏览代码以查看它可能出错的地方,但坦率地说,我对Indy和各种电子邮件协议/ RFC不够熟悉,无法确定出现了什么问题.关于我可以告诉的是,旧版本将消息保存到临时文件,然后将其附加到文件夹,而较新版本则将其保存到内存流中.显然,有些事情是不同的,但我现在太无知,无法确定什么.
有没有一种简单的方法来纠正旧版本中的时间戳问题?如果是这样,那对我的使用来说没问题,因为其他一切看起来都是正确的.如果没有,我还需要做些什么来解决这里展示的问题与不正确的边界字符串?
(由于这是一个严格用于我自己的应用程序,如果必须,我可以使用不正确的日期,但不能使用"已发送邮件"文件夹中的"空出现"副本.)
如果需要更多信息,我很乐意尽我所能.
[编辑:我确实在我的代码中加入了一些kludge,使用旧版本的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;
Run Code Online (Sandbox Code Playgroud)
要么:
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;
Run Code Online (Sandbox Code Playgroud)
现在,说到这一点,它可能无论如何仍然无法正常工作. 内部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是一个非常棘手的协议.
| 归档时间: |
|
| 查看次数: |
3235 次 |
| 最近记录: |