How to send gmail by Indy (TIdSMTP)?
2016-02-06 11:51
579 查看
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;
I run procedure "TestSendMail" it always raise 'Host not found'. Please fix it for me. Thank you!
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.
I change setting the TIdSMTP.UseTLS before setting TIdSMTP.Port. It is fine ! Thanks to Mr. Remy Lebeau!
相关文章推荐
- pentaho saiku 安装全过程
- pentaho saiku 安装全过程
- hdu 1848 Fibonacci again and again
- aix平台安装10.2.0.5的CRS时遇到的bug
- 【题解】2016 BUPT Winter Training #2
- quailty's Contest #1 题解
- cf#AIM Tech Round -B. Making a String-贪心/set
- cf#AIM Tech Round -C. Graph and String-贪心/ 二分图染色
- cf#AIM Tech D. Array GCD (数学+枚举)
- AIM Tech Round (Div. 2) D. Array GCD(dp)
- codeforces AIM Tech Round
- WM_PAINT消息详解,使用InvalidateRect或InvalidateRgn函数刻意产生WM_PAINT消息(WIN7里有变化,“调整视觉效果”,将“启用桌面组合”去掉)
- WM_PAINT消息在窗口重绘的时候产生,那什么时候窗口会重绘(异步工作方式,效率更高,灵活性更强)
- 四川成都电信天邑TEWA-300AI EPON光猫各种折腾
- Cloud Foundry warden container 安全性探讨
- async await
- VIM配置:vim-airline插件安装
- AIM Tech Round (Div. 2) C. Graph and String
- AIM Tech Round (Div. 2)C - Graph and String(二分图染色)
- gmail如何设置邮箱别名