smtp - Send email using indy component delphi xe2 SSL negotiation faild -
i tried indy component send email in xe2, , works fine on laptop compiled project on.
but if take exe project pc, shows error message
connection closed gracefully
or,
ssl negotiation failed
actually tried many times solve problem, can't.
this code - mistake? need practical solution.
procedure gmail(username, password, totarget, subject, body :string); var data : tidmessage; smtp : tidsmtp; ssl : tidssliohandlersocketopenssl; result:boolean; begin try smtp := tidsmtp.create(nil); data := tidmessage.create(nil); ssl := tidssliohandlersocketopenssl.create(nil); ssl.destination := 'smtp.gmail.com:587'; ssl.host := 'smtp.gmail.com'; // ssl.maxlineaction.maexception; ssl.port:= 587; ssl.ssloptions.method := sslvtlsv1; ssl.ssloptions.mode := sslmunassigned; ssl.ssloptions.verifymode := []; ssl.ssloptions.verifydepth := 0; data.from.address := username; data.recipients.emailaddresses := totarget; data.subject := subject; data.body.text := body; if fileexists('d:\test1.txt') tidattachmentfile.create(data.messageparts, 'd:\test1.txt'); smtp.iohandler := ssl; smtp.host := 'smtp.live.com'; smtp.port := 587 ; smtp.username := username; smtp.password := password; // smtp.saslmechanisms; smtp.usetls := utuseexplicittls; try try smtp.connect; smtp.send(data); result := true; except on e:exception begin showmessage('cannot send e-mail: ' + e.message); result := false; end; end; if smtp.connected smtp.disconnect; end; except on e : exception begin showmessage('error in function sendemaildelphi: ' + e.message); result := false; end; end; end; procedure tform1.formcreate(sender: tobject); begin timer1.enabled:= true; end; procedure tform1.timer1timer(sender: tobject); begin mail_username := 'email@gmail.com'; mail_password := 'pass'; mail_to := 'email@gmail.com'; mail_subject := 'text email delphi project'; mail_body := 'this test email sent delphi project, not reply'; try begin gmail(mail_username, mail_password, mail_to , mail_subject, mail_body); end; end; end;
do not set ssl.destination, ssl.host, or ssl.port properties manually! tidtcpclient.connect() handles you. besides, don't think it's odd setting ssl.destination/host smtp.gmail.com setting smtp.host smtp.live.com instead? gmail , live not same service provider.
also, ssl.ssloptions.mode should set sslmclient instead of sslmunassigned. not important, tidssliohandlersocketopenssl flip when configures connection. should anyway, since know code acting client.
and lastly, try setting smtp.usetls before setting smtp.port, setting usetls may change port, want make sure connecting correct port expecting.
with said, ssl negotiation failed error means tls handshake started failed part-way through negotiation. try assigning handlers tidssliohandlersocketopenssl's onstatusinfo/ex events see how far handshake getting. , if using relatively modern version of indy 10, try looking @ raised exception's innerexception property, might give clue went wrong before eidtlsclienttlshandshakefailed exception raised afterwards.
Comments
Post a Comment