gpt4 book ai didi

delphi - 发送带有内存流的电子邮件

转载 作者:行者123 更新时间:2023-12-03 18:36:09 25 4
gpt4 key购买 nike

我正在尝试发送带有内存流的电子邮件,但我总是在收到的电子邮件中收到此消息,但没有附件“这是MIME格式的多部分消息...”

码:

var
MemStrm : TMemoryStream;
begin
MemStrm := TMemoryStream.Create;
Str1.SaveToStream(MemStrm);
MemStrm.Position := 0;

try
with IdSSLIOHandlerSocketOpenSSL1 do
begin
Destination := 'smtp.mail.yahoo.com' + ':' + IntToStr(465);
Host := 'smtp.mail.yahoo.com';
MaxLineAction := maException;
Port := 465;
SSLOptions.Method := sslvTLSv1;
SSLOptions.Mode := sslmUnassigned;
SSLOptions.VerifyMode := [];
SSLOptions.VerifyDepth := 0;
end;
with IdSMTP1 do
begin
IOHandler := IdSSLIOHandlerSocketOpenSSL1;
Host := 'smtp.mail.yahoo.com';
Port := 465;
AuthType := satDefault;
Password := 'password';
Username := 'sender@yahoo.com';
UseTLS := utUseImplicitTLS;
end;
with IdMessage1 do
begin

From.Address := 'sender@yahoo.com';

List:= Recipients.Add;
List.Address:= 'receiver@yahoo.com';
Recipients.EMailAddresses := 'receiver@yahoo.com';
Subject := 'test';
Body.Text := 'test';
ContentType := 'text/plain';

IdAttachment := TIdAttachmentMemory.Create(IdMessage1.MessageParts,MemStrm);
IdAttachment.ContentType := 'text/plain';
IdAttachment.FileName := 'attached.txt';
end;
finally
idsmtp1.Connect;
idsmtp1.Send(idmessage1);
idsmtp1.Disconnect;
MemStrm.Free;
end;
end


我不想先将其另存为文件,然后附加它,如何将示例内存流附加到我的电子邮件?

编辑:即使使用TIdAttachmentFile也无法正常工作,我在delphi 7上使用了Indy10版本10.6.0.5049

最佳答案

您正在将顶级TIdMessage.ContentType设置为text/plain,这告诉读者将整个电子邮件解释为纯文本。您需要将该属性设置为multipart/mixed,然后读者将解释附件:

with IdMessage1 do
begin
...
ContentType := 'multipart/mixed';
end;


我还建议您添加一个 TIdText对象,以使读者知道电子邮件的含义,例如:

IdText := TIdText.Create(IdMessage1.MessageParts, nil);
IdText.ContentType := 'text/plain';
IdText.Body.Text := 'here is an attachment for you.';

IdAttachment := TIdAttachmentMemory.Create(IdMessage1.MessageParts,MemStrm);
IdAttachment.ContentType := 'text/plain';
IdAttachment.FileName := 'attached.txt';


另外,您还要两次填写 Recipients属性。使用 Recipients.Add.AddressRecipients.EMailAddresses,请不要同时使用:

with IdMessage1 do
begin
...
List:= Recipients.Add;
List.Address := 'receiver@yahoo.com';
...
end;




with IdMessage1 do
begin
...
Recipients.EMailAddresses := 'receiver@yahoo.com';
...
end;

关于delphi - 发送带有内存流的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27613124/

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