Reputation: 175
I am trying to send email with memorystream attached but I always got this message in the email I received and no attachment "This is a multi-part message in MIME format..."
Code:
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 := '[email protected]';
UseTLS := utUseImplicitTLS;
end;
with IdMessage1 do
begin
From.Address := '[email protected]';
List:= Recipients.Add;
List.Address:= '[email protected]';
Recipients.EMailAddresses := '[email protected]';
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
I do not want to save it as file first then attach it, how do I attach the sample memorystream to my email?
EDIT: It didn't work even with TIdAttachmentFile, i'm using Indy10 version 10.6.0.5049 on delphi 7
Upvotes: 2
Views: 2371
Reputation: 596352
You are setting the top-level TIdMessage.ContentType
to text/plain
, which tells the reader to interpret the entire email as plain text. You need to set that property to multipart/mixed
instead, then the reader will interpret the attachment:
with IdMessage1 do
begin
...
ContentType := 'multipart/mixed';
end;
I would also suggest you add a TIdText
object to let the reader know what the email is about, eg:
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';
Also, you are filling in the Recipients
property twice. Use either Recipients.Add.Address
or Recipients.EMailAddresses
, do not use both:
with IdMessage1 do
begin
...
List:= Recipients.Add;
List.Address := '[email protected]';
...
end;
with IdMessage1 do
begin
...
Recipients.EMailAddresses := '[email protected]';
...
end;
Upvotes: 3
Reputation: 150
The problem is the ContentType definition of IdAttachment.
You have to set it to 'application/x-zip-compressed'
like this:
IdAttachment := TIdAttachmentMemory.Create(IdMessage1.MessageParts,MemStrm);
IdAttachment.ContentType := 'application/x-zip-compressed';
IdAttachment.FileName := 'attached.txt';
Upvotes: -1