Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

send email via delphi7 sample code

1,634 views
Skip to first unread message

delphi lover

unread,
May 11, 2008, 9:53:31 AM5/11/08
to

hi there,
can anybody help me to send email via dephi7 to yahoo or gmail email address!

thanks a lot.

E Sterrett

unread,
May 11, 2008, 11:34:40 AM5/11/08
to

delphi lover

unread,
May 11, 2008, 1:07:51 PM5/11/08
to

oh great solution, but what about yahoo or gmail Host(smtp server)
name, i don't know about it.

notradamus

unread,
May 26, 2008, 7:14:28 AM5/26/08
to

Hi Delphi Lover,

The following code is from an application I am currently writing. It sends
HTML format message and works using Gmail server.

I have taken the email code from Indy page :) and some help from the
ProfDHTMLEdit support team as I am using that component for HTML editor.

http://www.indyproject.org/Sockets/Blogs/RLebeau/2005_08_17_A.en.aspx
http://www.profgrid.com/dhtmledit.html


But there is 1 problem: attachment does not display inline in the HTML,
but comes across as an email attachment. I am hoping someone could be so
kind as to help me out with that.

Regards,
Notra Damus

[BEGIN CODE]

// Sends email to email address s
procedure TfrmMailManager.SendEmail(s : string);
var
t: TIDText;
t2: TIDAttachmentFile;
images: TStringList;
c: integer;
Guid : TGUID;
begin

try
//-- set up the message - ND
with IdMessage1 do
begin
From.Address:=e.mail_emailaddr;
From.Name:= e.mail_fromname;
Subject:=strSubject.Text;
Recipients.EMailAddresses:=s;
ContentType:='multipart/related; type="text/html"';

t := TIdText.Create(messageParts, nil);
t.ContentType := 'text/html';
t.ParentPart := -1;

images := Tstringlist.create;

ProfDHTMLEdit21.GetImages(images);
for c:=0 to images.Count-1 do
begin
if FileExists(images[c]) then
begin
t2 := TIdAttachmentFile.Create( messageParts, images[c]);
t2.ParentPart:=-1;
t2.FileName:=images[c];
t2.ContentType:='image/' + MidStr( ExtractFileExt(images[c]),2, 3);
CreateGUID(Guid);
t2.ContentID:= GUIDToString(Guid);
msgText.Text := StringReplace(msgText.Text, images[c],
'cid:'+t2.ContentID, [rfIgnoreCase]);
end;
end;
images.Free;
t.Body:=msgtext;
end; //with


//-- set up the smtp client and send the message - ND
SMTP.Host:= e.smtp_server;
SMTP.Port:= strtoint(e.smtp_port);
if e.smtp_useauth then
begin
SMTP.AuthType:= satDefault;
smtp.Username:= e.smtp_username;
smtp.Password:= e.smtp_password;
end
else
SMTP.AuthType := satNone;

if e.smtp_usessl then
begin
smtp.IOHandler:= IdSSLIOHandlerSocketOpenSSL1;
smtp.UseTLS:= utUseRequireTLS;
end
else
begin
smtp.IOHandler:=nil;
smtp.UseTLS:= utNoTLSSupport;
end;

if smtp.Connected then
smtp.Disconnect;
smtp.Connect;
if smtp.Connected then
begin
smtp.Send(IdMessage1);
end;

except
showmessage('Error sending email.');
end;
end;


[END CODE]

--- posted by geoForum on http://delphi.newswhat.com

Remy Lebeau (TeamB)

unread,
May 27, 2008, 5:20:59 PM5/27/08
to

"notradamus" <nostrad...@yahoo.com> wrote in message
news:483a82f0$1...@newsgroups.borland.com...

> http://www.indyproject.org/Sockets/Blogs/RLebeau/2005_08_17_A.en.aspx

Have you seen my other related blog entry yet?

New HTML Message Builder class
http://www.indyproject.org/Sockets/Blogs/RLebeau/20080116.EN.aspx

> But there is 1 problem: attachment does not display inline in
> the HTML, but comes across as an email attachment.

Then you are not setting up the TIdMessage data correctly.

> t2 := TIdAttachmentFile.Create( messageParts, images[c]);

You are not setting the attachment's ContentDisposition property at all:

t2.ContentDisposition := 'inline';

> t2.ContentType:='image/' + MidStr( ExtractFileExt(images[c]),2,
> 3);

That is not the correct way to set up a ContentType value. Use Indy's
GetMIMETypeFromFile() function instead:

t2.ContentType := GetMIMETypeFromFile(images[c]);

> t2.ContentID:= GUIDToString(Guid);

You need to wrap the ContentID with angle brackets:

t2.ContentID := '<' + GUIDToString(Guid) + '>';


Gambit


notradamus

unread,
May 27, 2008, 10:31:52 PM5/27/08
to

Hi Remy,

Thanks for replying. I put in the changes suggested, but to no avail. It
still behaves the same ie. inline images are blank - there are
placeholders but no image there.

Follows is the complete code from my app.

Regards,
Notra Damus

[BEGIN CODE]

procedure TForm1.Button1Click(Sender: TObject);
var
t : TIDText; t2 : TidAttachmentFile;
messg : Tstringlist;
images : Tstringlist;
c: integer;
GUID : TGUID;
begin

messg := Tstringlist.Create;
messg.Text := ProfDHTMLEdit21.Source ;

with Idmessage1 do
begin
From.Address:='VVVV';
From.Name:='FFFF';
Subject:='test email #445';
Recipients.EMailAddresses := 'SSSS';
ContentType := 'multipart/mixed; type="text/html"';

t := TIdText.Create(messageParts, nil);
t.ContentType := 'text/html';
t.ParentPart := -1;

images := Tstringlist.create;

ProfDHTMLEdit21.GetImages(images);
for c:=0 to images.Count-1 do
begin
if FileExists(images[c]) then
begin

t2 := TIdAttachmentFile.Create( messageParts, images[c]);

t2.ParentPart:=-1;
t2.ContentDisposition:='inline';
//t2.FileName:=images[c];
t2.ContentType := GetMimeTypeFromFile(images[c]);
CreateGUID(Guid);
t2.ContentID:= 'cid:<' + GUIDToString(Guid) +'>';
messg.Text:= StringReplace(messg.Text, images[c], t2.ContentID,
[rfIgnoreCase]);
end;
end;

images.Free;
t.Body.Text := messg.Text;
end;

with idsmtp1 do
begin
Host:='smtp.gmail.com';
Port:=465;
AuthType:=satDefault;
IOHandler:=IdSSLIOHandlerSocketOpenSSL1;
UseTLS:=utUseRequireTLS;
Username:= 'CCCC';
Password:='VVVV';

if Connected then
Disconnect;
Connect;
if Connected then
Send(IdMessage1)
else
showmessage('not connected dude!');

Remy Lebeau (TeamB)

unread,
May 28, 2008, 6:18:24 PM5/28/08
to

"notradamus" <nostrad...@yahoo.com> wrote in message
news:483c...@newsgroups.borland.com...

> Thanks for replying. I put in the changes suggested, but to no avail.
> It still behaves the same ie. inline images are blank - there are
> placeholders but no image there.

Then you are still not setting it up correctly. Please show the actual
message data that TIdMessage generates.

> ContentType := 'multipart/mixed; type="text/html"';

"multipart/mixed" does not have a "type" attribute. Only
"multipart/related" does, which you should be using anyway since you are
trying to get the HTML to display attached images.

Again, I strongly suggest you read the other blog entry I pointed you to,
and try to use the new TIdMessageBuilderHtml class that I wrote. It handles
all of these details for you, ie:

procedure TForm1.Button1Click(Sender: TObject);
var
Source: String;
c: Integer;
begin
Source := ProfDHTMLEdit21.Source;

IdMessage1.Clear;
IdMessage1.From.Address := 'VVVV';
IdMessage1.From.Name := 'FFFF';
IdMessage1.Recipients.EMailAddresses := 'SSSS';

with TIdMessageBuilderHtml.Create do
try


Subject := 'test email #445';

ProfDHTMLEdit21.GetImages(HtmlFiles);

// TIdMessageBuilderHtml does not support user-defined ContentID
values yet...
for c := 0 to HtmlFiles.Count-1 do
Source := StringReplace(Source, images[c],
ExtractFileName(images[c]), [rfIgnoreCase]);

Html.Text := Source;
FillMessage(IdMessage1);
finally
Free;
end;

// send IdMessage1 now...
end;


Gambit


0 new messages