How to send gmail by Indy (TIdSMTP)? [Edit]

My code to send gmail:

{code}
procedure SendEmail(const Recipients: string; const Subject: string; const Body: string);
var
  SMTP: TIdSMTP;
  Email: TIdMessage;
  SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
  eFrom: string;
begin
  eFrom := 'duytuanvn@gmail.com';
  SMTP := TIdSMTP.Create(nil);
  Email := TIdMessage.Create(nil);
  SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);

  try
    SSLHandler.MaxLineAction := maException;
    SSLHandler.SSLOptions.Method := sslvTLSv1;
    SSLHandler.SSLOptions.Mode := sslmUnassigned;
    SSLHandler.SSLOptions.VerifyMode := [];
    SSLHandler.SSLOptions.VerifyDepth := 0;
    SSLHandler.Host :=  'smtp.gmail.com';
    SSLHandler.Port := 587;

    SMTP.IOHandler := SSLHandler;
    SMTP.Host := 'smtp.gmail.com';
    SMTP.Port := 587;
    SMTP.Username := eFrom;
    SMTP.Password := 'xxxxxx';
    SMTP.UseTLS := utUseExplicitTLS;

    Email.From.Address := eFrom;
    Email.Recipients.EmailAddresses := Recipients;
    Email.Subject := Subject;
    Email.Body.Text := Body;

    SMTP.Connect; //always raise 'Host not found'
    SMTP.Send(Email);
    SMTP.Disconnect;

  finally
    SMTP.Free;
    Email.Free;
    SSLHandler.Free;
  end;
end;
;------------------------------------------------------------------------
procedure TestSendMail(Sender: TObject);
begin
  SendEmail('tuanktcdcn@yahoo.com', 'Subject', 'Body');
end;
{code}

I run procedure "TestSendMail" it  always raise 'Host not found'. Please fix it for me.

Thank you!

Edited by: Tuan Nguyen on Oct 10, 2013 3:53 AM

Edited by: Tuan Nguyen on Oct 10, 2013 3:54 AM
0
Tuan
10/10/2013 10:56:01 AM
embarcadero.delphi.winsock 1874 articles. 2 followers. Follow

5 Replies
7218 Views

Similar Articles

[PageSpeed] 12

Tuan wrote:

> My code to send gmail:

Examples of using TIdSMTP to send emails to Gmail have been posted many times 
before, in Embarcadero's forums, AToZed's Indy forums, and various online 
blogs.  Did you search around for them?

> I run procedure "TestSendMail" it  always raise 'Host not found'.

You are setting the Host/Port properties of TIdSSLIOHandlerSocketOpenSSL. 
 Do not do that, Connect() handles that internally for you.

Outside of that, you are setting the TIdSMTP.UseTLS property after setting 
the TIdSMTP.Port property.  Try reversing those.  The UseTLS property setter 
can change the Port value, so you should set the UseTLS first, then set the 
Port to what you really need, in case it is different than what UseTLS assigns.

If that still does not work, then double check your OS network settings. 
 Check that the OS's HOSTS file is not redirecting smtp.gmail.com, make sure 
you can ping smtp.gmail.com, etc.  If you don't find any problems with that, 
then use a packet sniffer like Wireshark to see what Connect() is actually 
doing on the network.  Maybe your OS's DNS system is failing to find the 
IP address for smtp.gmail.com.

--
Remy Lebeau (TeamB)
0
Remy
10/10/2013 6:58:25 PM
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}

> Outside of that, you are setting the TIdSMTP.UseTLS property after setting 
> the TIdSMTP.Port property.  Try reversing those.  The UseTLS property setter 
> can change the Port value, so you should set the UseTLS first, then set the 
> Port to what you really need, in case it is different than what UseTLS assigns.

> --
> Remy Lebeau (TeamB)

I change setting the TIdSMTP.UseTLS before setting TIdSMTP.Port. It is fine ! 

Thanks to Mr. Remy Lebeau!
0
Tuan
10/10/2013 8:46:36 PM
Could you help me. How can i send unicode text in body? I can not send unicode string in the body.

Thank you!
0
Tuan
10/10/2013 9:16:23 PM
Tuan wrote:

> Could you help me. How can i send unicode text in body? I
> can not send unicode string in the body.

Which version of Delphi are you using?  Did you set the TIdMessage.CharSet 
property?

--
Remy Lebeau (TeamB)
0
Remy
10/10/2013 9:34:31 PM
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> Tuan wrote:
> 
> > Could you help me. How can i send unicode text in body? I
> > can not send unicode string in the body.
> 
> Which version of Delphi are you using?  Did you set the TIdMessage.CharSet 
> property?
> 
> --
> Remy Lebeau (TeamB)


My old code TIdMessage.CharSet := 'utf8'. I have changed TIdMessage.CharSet := 'utf-8' is fine!

Thank you so much!
0
Tuan
10/12/2013 2:52:00 PM
Reply:

Similar Artilces:

How to send mail by Indy and Gmail ? (Delphi 7)
How to send mail by Indy and Gmail ? (Delphi 7) Thank you very much . ^_^ > {quote:title=ICST ICST wrote:}{quote} > How to send mail by Indy and Gmail ? (Delphi 7) > > Thank you very much . ^_^ I test successfully. [code] // Send mail by Gmail // // Delphi 7 Test OK // // need OpenSSL for Delphi 7 // http://indy.fulgan.com/SSL/ // UnCompress OpenSSL file for Delphi 7, and get two files : libeay32.dll , ssleay32.dll // put them with Program togeter. unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics...

Converting Delphi 2007 Indy 10.2.3 to Delphi 2009 Indy 10.5.5 [Edit]
Hello, I am currently attempting to port over a Delphi 2007 project that uses Indy 10.2.3 (very successfully) to Delphi 2009 and Indy 10.5.5 (I just got the latest development build this morning). I think I am running into an encoding issue, but am not sure. Specifically, IDHTTP with SSL calls an old CGI and the CGI returns a .zip file and I then save it to the disk. In 2007 and before this worked perfectly. In 2009, it is not. Here is the examples of the 2 different results (though cut way short in the post) I am getting back: 2007: 'PK'#3#4#$14#0#0#0#8#0'rLQ9žrPb€'#0...

Delphi XE2, Indy 10
Dear folks The following code is supposed to send a text string to a client that is listening on a set port and ip address. The connection works fine. The problem is that nothing seems to be sent. {code} function TFCSelectForm.SendNitroMessage(NitroRequest: String): String; var sStrm: TMemoryStream; begin try sStrm := TMemoryStream.Create; try sStrm.WriteBuffer(Pointer(NitroRequest)^,Length(NitroRequest)); // sStrm shows as () in the debugger IdTCPClient1.IOHandler.Write(sStrm, 0, False); sStrm.Position := 0; finally ...

[Delphi] send custom packet from DLL ( winsock ) from DLL-Form [Edit]
hello! i`we hooked winsocket recv-send! in DLL i have form who popup on dlls inject sucess and i need send custom packet to 'Send" from my form how it be posibly?? send(s,buf,len,flags); - dont work its work if i send it only from "DLL" if i use it in my DLL-"form" then nothing happens in DLL - sock:=s; Fla:=flags; in DLL"form" send(sock,buf,len,fla); dont work to.. meybe i do it wrong? .. Thank you! <Ronaldo DeVinche> wrote in message news:136126@forums.codegear.com... > i`we hooked winsocket recv-send! ...

Sending mail using Indy TIdSMTP goes to SPAM/JUNK folder [Edit]
While *sending mail using Indy TIdSMTP*, the mail is *consider as Spam* and it's *going to Spam/Junk folder*. is there anyway to solve this issue ??? Edited by: karthik thirumoorthi on Jun 24, 2014 3:06 AM karthik wrote: > While *sending mail using Indy TIdSMTP*, the mail is *consider as > Spam* and it's *going to Spam/Junk folder*. is there anyway to > solve this issue ??? Not without knowing what criteria is being used to marked it as spam. Are there any extra headers being added to the email when it is put in the Spam folder? Some anti-spam filters will a...

send gmail application [Edit]
Hi, all. This time, I've release sending gmail(google mail) application(made of C++Builder). bin : executable files send_gmail.exe : GUI based application sg.exe : console based application program : source codes common : common header and cpp files send_gmail : GUI based application sources sg : console based application sources http://gilgil.net/543 Thank you. Edited by: Gilbert Lee on Sep 26, 2009 10:40 AM ...

TIdTraceRoute indy delphi xe2 [Edit]
i need trace the route of an ip, actually i am using the TIdTraceRoute indy component from the idTraceRoute unit. (delphi xe2) IdTraceRoute1:= TIdTraceRoute.Create(Self); IdTraceRoute1.ResolveHostNames:= True; IdTraceRoute1.ReceiveTimeout:= 5000; IdTraceRoute1.OnReply:= TraceRoute; IdTraceRoute1.Host:= 'www.google.com';//OR 172.16.0.13 (INTRANET IP) IdTraceRoute1.Trace; procedure TForm1.TraceRoute(ASender: TComponent; const AReplyStatus: TReplyStatus); begin Memo1.Lines.Add(AReplyStatus.FromIpAddress); end; but always return. 0.0.0.0 0....

TIdSMTP->Send() and LastCmdResult [Edit]
Hello! I would like to add some information and do some cleanup processing if my Send() call fails, so I'm trying to catch the exception and use LastCmdResult. Here is some slightly simplified code: {code} String errormsg = ""; __try { try { DataModuleShared->IdSMTP1->Send(message); } catch(...) { errormsg = DataModuleShared->IdSMTP1->LastCmdResult->FormattedReply->Text; if (errormsg.Length() == 0) errormsg = "Unable to send email to " + message->Recipients->EMailAddresses; Application->Messag...

Send Email with Indy and SSL [Edit]
I must send a mail with Indy 10.0.1.1 and SSL This is my code: procedure SendEmail; var IdMessage: TIdMessage; SMTP: TIdSMTP; SSLHandler: TIdSSLIOHandlerSocketOpenSSL; IdUserPassProvider: TIdUserPassProvider; IdSASLCRAMMD5: TIdSASLCRAMMD5; IdSASLCRAMSHA1: TIdSASLCRAMSHA1; IdSASLPlain: TIdSASLPlain; IdSASLLogin: TIdSASLLogin; IdSASLSKey: TIdSASLSKey; IdSASLOTP: TIdSASLOTP; IdSASLAnonymous: TIdSASLAnonymous; IdSASLExternal: TIdSASLExternal; begin IdMessage := TIdMessage.Create(nil); try IdMessage.ContentType := 'text/plain'; IdMes...

Sending email via INDY and GMAIL
This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. --JivePart=_4670c.zeB8CwDOH5aMJzMl Content-Type: text/plain; charset="Utf-8" refer to atricle in embarcadero.public.delphi.internet.winsock --JivePart=_4670c.zeB8CwDOH5aMJzMl Content-Type: application/octet-stream; name="sendindyemail.zip" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="sendindyemail.zip" UEsDBBQAAAAIAM9uODl9piZ0WWYJAMtnFwAMAAAAbGliZWF5MzIuZGxs7L15fFNV/j+cpGmb0kCC...

Delphi 7 Indy 10 HTTPS [Edit]
I have an old program I inherited that I'm required to maintain and Delphi is not a language I regularly use. From what I can see, the program uses an http proxy server to monitor pages as they are loaded into a browser control. Depending on the name and/or the content of the pages, the http proxy server does various tasks. All has been good up until now. The client wishes to now use https for their pages and I was wondering how can I get the http proxy server to be able to deal with https pages and keep the client happy. I did try to add an IdSSLIOHandlerSocketOpenSSL to the program ...

Delphi XE5 Android problem (Indy TCP) [Edit]
Hi! Have some major problems, no ideas left. My Android app. is using a component TCP Server-Client Library (http://www.3delite.hu/Object%20Pascal%20Developer%20Resources/TCPServerClientLibrary.html) that I made before. The server app. is built with Delphi XE2 and is using this same component too. The problem is that when I try to download some files with it to Android the Android app. either freezes, or the download stops, randomly (sometimes 3 files arrive successfully and then freezes/stops). Testing with 3 10MB and 2 100MB audio files which are transfered at the same time. What ...

TIdSMTP dot padding issue(Indy 10) [Edit]
Hi, Just downloaded http://indy.fulgan.com/ZIP/IndyTiburon.zip, I have looked on the forum on how to build this but not been able to get any info. Does any one know how to build the BPL's for C++ Builder 2007? Is there a build doc that I can refer to. Original Post on http://forums2.atozed.com/viewtopic.php?f=7&t=757 Any help will be much appreciated. Thanks Kal "Kal M" <khalidm@gemstv.co.uk> wrote in message news:131797@forums.codegear.com... > ... www.indyproject.org/docsite/ ...

Delphi 2009
I've done some searching on the web, and i can't seem to find any answer to the problem i have. The client is made in c++ using winsock send & recieve. The server is done with Indy IdTCPServer component. When the winsock client connects, IdTCPServer get knowledge of it. However, when the winsock client sends a chararray with the function send(), nothing gets parsed in IdTCPServer.Execute. I know the client works, since i built a winsock testserver for the occation. However when using Acontext.Connection.IOHandler.ReadLn it does not. Is it known that Indy vs winsock base do...