I'm trying Synapse to send emails with attachments and I use a function like this :
=========
var part: TMimePart;
MM : TMimeMess;
data:tstringlist;
tmp: TMemoryStream;
=========
data:=TStringList.Create;
data.Add('This is a text');
mm:=TMIMEMess.Create;
mm.header.from:='m...@email.com';
mm.header.ToList.add('m...@email.com');
mm.header.subject:='A test';
Part := mm.AddPartMultipart('Mixed', nil);
mm.AddPartText(data, Part);
tmp := TMemoryStream.Create;
try
tmp.LoadFromFile('C:\attachment.zip');
mm.AddPartBinary(tmp, 'attachment.zip',part);
finally
tmp.Free;
end;
mm.EncodeMessage;
SendToEx('m...@email.com','m...@email.com', 'Hey','smtp.email.com', MM.Lines, '','');
MM.Free;
=========
The email is received, but looks like below:
=========
From: m...@email.com
To: m...@email.com
Subject: A test
Date: Wed, 5 Jul 2006 19:48:40 +0300
MIME-Version: 1.0 (produced by Synapse)
X-mailer: Synapse - Pascal TCP/IP library by Lukas Gebauer
Content-type: Multipart/Mixed; boundary="00538921_2DA5ADB8_Synapse_boundary"
Content-Description: Multipart message
--00538921_2DA5ADB8_Synapse_boundary
Content-type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: Quoted-printable
Content-Disposition: inline
Content-Description: Message text
This is a text
--00538921_2DA5ADB8_Synapse_boundary
Content-type: application/ZIP; name="attachment.zip"
Content-Transfer-Encoding: Base64
Content-Disposition: attachment; FileName="attachment.zip"
Content-Description: Attached file: attachment.zip
UEsDBBQAAAAIAIVVNDOt25sN/BoAAMJzAAAHAAAAZmFxLmh0be1dbVfbSJb+PJzT/6HacybY
GyMMTaZ3DCZLwKSZQKDB3Znes+fkyFbZ1iBLGkmGONn8933urRdJfsF2ArNfIN1gW6V7b926
73VLPhhmo+BwY+NgKF3vcOMgzSaBFJ0/rtqtSiY/Zdu9NK0cio0/dY7enLedfuCmQ/FF9KIg
SppiIoMgut8X/SBys6ZI/MEww7sozLb67sgPJk1xlPhuoD+7lxiAcd0o8PYFgd9yA38QNkVP
hplM9LDU/yybYmd3X9z5qd/1Az8DIH4N2r5uCCE6J04YJSM3yGn583GD/k2hz9xhNHKnAM8l
... ... ...
--00538921_2DA5ADB8_Synapse_boundary--
=========
Please can you tell me what is wrong or show me a working sample?
Thanks.
> I'm trying Synapse to send emails with attachments and I
> use a function like this :
<snip>
> The email is received, but looks like below:
As it should be, because that is how you set up the TMIMEMess in the first
place. So what exactly are you having a problem with?
> Please can you tell me what is wrong
There is nothing wrong. Why do you think that there is? Please provide
more details.
Gambit
>As it should be, because that is how you set up the TMIMEMess in the first
>place. So what exactly are you having a problem with?
The email body is *exactly* like below:
"
MIME-Version: 1.0 (produced by Synapse)
X-mailer: Synapse - Pascal TCP/IP library by Lukas Gebauer
Content-type: Multipart/Mixed; boundary="00538921_2DA5ADB8_Synapse_boundary"
Content-Description: Multipart message
--00538921_2DA5ADB8_Synapse_boundary
Content-type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: Quoted-printable
Content-Disposition: inline
Content-Description: Message text
This is a text
--00538921_2DA5ADB8_Synapse_boundary
Content-type: application/ZIP; name="attachment.zip"
Content-Transfer-Encoding: Base64
Content-Disposition: attachment; FileName="attachment.zip"
Content-Description: Attached file: attachment.zip
"
and the attachment is included in the email body, it's not attached.
I would like to have ONLY "This is a text" in the message body, not all that info (MIME-Version, boundary, Content-Type, Content-Description etc. etc.) and the attachment attached.
Sorry for the lack of details in the 1st post.
Dieter
If I cut and paste the text into notepad, save it as an eml then open it in OE the attachment shows as an attachment (its not a valid zip file though). I use Synapse and I can see nothing wrong with the email. Try posting to the Synapse mailing list (synalis...@lists.sourceforge.net) but I don't think you'll get a different answer.
Roy Lambert
>If I cut and paste the text into notepad, save it as an eml then >open it in OE the attachment shows as an attachment (its not a >valid zip file though).
Thanks Roy! Yes, it's true, I've tested myself now. But the weird thing is that when I'm receiving the email it looks like I've told you previously. I've tried also webmail and it didn't look correct. Don't know what's wrong.
> Try posting to the Synapse mailing list >(synalis...@lists.sourceforge.net) but I don't think you'll >get a different answer.
Thanks, I'll try there too.
email me direct so I can check it out.
Roy Lambert
OK, I've used your email address with my Synapse test.
Thank you very much :)
If you're kkk and emailed me a little gif then your messing your headers up.
I received the message below which essentially contains two headers. You can't do that. As a guess when you posted the message the last time what you did was cut and paste from Outlook, and it seems as though you're sending through Outlook (how I have no idea).
I can see what's happening now - its a standard case of RTFM <g> - relevant part posted below. SendToEx essentially adds the headers so you add then then it does. I don't use SendToEx so I don't know what it will do if you simply leave the headers out - you'd be best asking on the Synapse mailing list AFTER reading the documentation <vbg>.
---------------------------------------------------------------------------------------------------------------------------
Return-Path: <roy.l...@skynet.co.uk>
Received: from kkk (55-128-79-82.rdspt.ro [82.79.128.55])
by vulture.skynet.co.uk (8.13.7/8.13.7) with ESMTP id k66FZaQe015348
for <roy.l...@skynet.co.uk>; Thu, 6 Jul 2006 16:35:36 +0100 (BST)
Message-Id: <200607061535....@vulture.skynet.co.uk>
From: roy.l...@skynet.co.uk
To: roy.l...@skynet.co.uk
Date: Thu, 6 Jul 2006 18:35:37 +0300
Subject: Hey
X-mailer: Outlook
From: roy.l...@skynet.co.uk
To: roy.l...@skynet.co.uk
Subject: A test
Date: Thu, 6 Jul 2006 18:35:37 +0300
MIME-Version: 1.0 (produced by Synapse)
X-mailer: Synapse - Pascal TCP/IP library by Lukas Gebauer
Content-type: Multipart/Mixed; boundary="00652C64_2A118274_Synapse_boundary"
Content-Description: Multipart message
----------------------------------------------------------------------------------------------------------------------------
Roy Lambert
SendToEx function
function SendToEx(Const MailFrom, MailTo, Subject, SMTPHost: string; Const MailData: Tstrings; Const Username, Password: string): Boolean;
Unit
SMTPsend
Description
A very useful function and example of its use would be found in the TSMTPsend object. Sends "MailData" (text of e-mail without any SMTP headers!) from "MailFrom" e-mail address to "MailTo" e-mail address (If you need more then one receiver, then separate their addresses by comma).
This function sends the e-mail to the SMTP server defined in the "SMTPhost" parameter. Username and password are used for authorization to the "SMTPhost". If you dont want authorization, set "Username" and "Password" to empty strings. If the e-mail message is successfully sent, the result will be TRUE.
If you need use different port number then standard, then add this port number to SMTPhost after colon. (i.e. '127.0.0.1:1025')
> The email body is *exactly* like below:
The only way that can happen is if the email itself is malformed, or if your
email reader is not interpreting it properly. Since the email data that you
have shown is not malformed, I would have to suspect a problem with your
email reader. But to verify that, you should use a packet sniffer, such as
Ethereal (http://www.ethereal.com) to grab a log of the raw SMTP traffic
that Synapse is generating, to make sure that all is actually good there.
Gambit
But I still have no idea why the headers are messed up :/
If I remove the headers inserted by me (header.from:='...';header.ToList.add...;) I still don't receive the email correctly. I receive all the other headers details (Content-type, Boundary etc. etc.) in the email body.
>SendToEx essentially adds the headers so you add then then it >does. I don't use SendToEx
What function do you use? Can you provide a sample?
>you'd be best asking on the Synapse mailing list AFTER reading >the documentation <vbg>.
The documentation is almost inexistent. And no sample on how to send an email with attachment.
Thanks again,
Dieter
Change this line to:
SendToRaw('m...@email.com','m...@email.com', 'Hey','smtp.email.com',
MM.Lines, '','');
And all will be fine now.
> SendToEx('m...@email.com','m...@email.com', 'Hey','smtp.email.com',
> MM.Lines, '','');
While Roy has pointed out to you what the problem is... To fix it you
should use SendToRaw instead of SendToEx.
--
Pax,
Anthony Frazier
Victor Printing, Inc.
Thanks a lot Lukas, works like a charm now :)
Dieter
> Thanks Gambit, the problem isn't on the email client side. I've tried
> with webmails like Yahoo and Safe-mail and the email body was messed
> up (headers and attachment included in the email body).
Like Roy told you, that will happen when the email headers are messed up,
such that the readers cannot interpret the data properly. Like I told you,
you wll have to look at the raw SMTP traffic with a packet sniffer to see
what is actually happening. Please do so, and post the log here for us to
see.
Gambit
>Like Roy told you, that will happen when the email headers are messed up,
>such that the readers cannot interpret the data properly. Like I told you,
>you wll have to look at the raw SMTP traffic with a packet sniffer to see
>what is actually happening. Please do so, and post the log here for us to
>see.
>
>
>Gambit
>
It's fixed, Lukas Gebauer told me to use SendToRaw in place of SendToEx.
Thank you the help :)
Thank you.
> I have one more problem. The email sending works fine but
> not with Yahoo accounts. Have you any idea what could be?
Not without more details from you. What EXACTLY is actually happening? Are
you getting any errors? Does the sending proceed without error, but the
message does not arrive? Please be more specific.
Gambit
FWIW. Essentially create the email as you have been doing then use the lower level SMTP functions.
if SMTP.MailFrom(SenderTable.FieldByName('_Eddress').AsString, Length(MailData.Text)) then begin
OKtoSend := False;
repeat
OneTo := FetchEx(AllTos, ',', '"');
if OneTo <> '' then Result := SMTP.MailTo(OneTo);
if Result then OKtoSend := True
else MakeLogEntry('E', MsgTable.FieldByName('_InOutInd').AsString, 'Send', 'Eddress error <' + OneTo + '>', nil, logPostingErrors);
until AllTos = '';
if OKtoSend then begin
SMTP.Timeout := CalculateTimeout(MsgTable);
Result := SMTP.MailData(MailData);
end else Result := False;
LogMsg := 'Completed sending <' + MsgTable.FieldByName('_Subject').AsString + '> to [' + MsgOpts.AllTos + ']';
MakeLogEntry('N', MailOut, 'Send', LogMsg, nil, logCompletionOfSend);
end;
if not Result then begin
sl := TStringList.Create;
try
sl.Assign(MsgTable.FieldByName('_Headers'));
except
sl.Clear;
end;
MakeLogEntry('E', MailOut, 'Send', 'Posting error: ' + IntToStr(SMTP.ResultCode) + ' ' + SMTP.ResultString + ' ' +
'Sock code: ' + Inttostr(smtp.Sock.LastError) + ' ' + SMTP.Sock.LastErrorDesc, sl, logPostingErrors);
sl.Free;
end;
Roy Lambert
You can find a description how to build HTML-messages here:
http://www.indyproject.org/Sockets/Blogs/RLebeau/2005_08_17_A.en.aspx
Synapse has nice features to build such messages.
Create a TMimeMess object and use it's methods like
AddPartMultiPart > creates a MultiPart, under which you can create the other
messageparts
AddPartText > creates a plain text-part
AddPartHTML > creates a html-part
AddPartBinary > creates a binary part (good for attachments)
AddPartHTMLBinary > creates a part for an embedded image
e.g. you can create a html-mail with embedded images and with an alternativ
text-part and
with attachment in the following way.
BTW: I haven't found any other library where it is that simple ...
////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
var MultiPartMix, MultiPartRel, MultiPartAlt: TMimePart;
MM := TMimeMess.Create
try
//multiparts (parent-parts)
MultiPartMix := MM.AddPartMultipart('mixed', nil);
MultiPartRel := MM.AddPartMultipart('related', MultiPartMix);
MultiPartAlt := MM.AddPartMultipart('alternative', MultiPartRel);
//plain text-part
MM.AddPartText([Your Text], MultiPartAlt);
//html-part
MM.AddPartHTML([Your HTML], MultiPartAlt);
//embedded images
MM.AddPartHTMLBinary([Your image-stream1], [Your Filename1], [Your
ContentID], MultiPartRel);
MM.AddPartHTMLBinary([Your image-stream2], [Your Filename2], [Your
ContentID], MultiPartRel);
//attachments
MM.AddPartHTMLBinary([Your attachment-stream1], [Your Filename1],
MultiPartMix);
//encode message
MM.EncodeMessage;
//send message via SMTP
SMTP := TSMTPSend.Create;
try
SMTP.SendToEx([MailFrom-address], [MailTo-address], [mail-subject],
[STMP-host],
MM.Lines, [account-Username],
[account-Password]);
finally
SMTP.Free;
end;
finally
MM.Free;
end;
////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
That's it !!!
HTH,
Jens