摘自:http://www.sunnyu.com/?p=192
在Delphi中发送email很简单,发送ssl方式的gmail邮件也很简单,只要在使用的idSMTP上附加一个TIdSSLIOHandlerSocket 就可以了。
使用控件
- procedure sendMail(sToMail, sSubject, sContent: String);
- var
- SMTP: TIdSMTP;
- MailMessage: TIdMessage;
- SSLSocket: TIdSSLIOHandlerSocket;
- begin
- SMTP := TIdSMTP.Create(nil);
- SSLSocket := TIdSSLIOHandlerSocket.Create(nil);
- MailMessage:= TIdMessage.Create(nil);
-
- SMTP.IOHandler := SSLSocket;
- SMTP.Port := 465;
- SMTP.Host := 'smtp.gmail.com';
- SMTP.AuthenticationType := atLogin;
-
- smtp.UserName := 'SunnyYu2000';
- smtp.Password := 'xxxxxx';
-
- // 设置邮件的信息
- MailMessage.Recipients.EMailAddresses := sToMail;
- MailMessage.Subject := sSubject;
- MailMessage.Body.Text := sContent;
-
- //发送邮件
- try
- try
- SMTP.Connect(1000);
- SMTP.Send(MailMessage);
- ShowMessage('发送成功');
- except on E:Exception do
- ShowMessage('发送失败: ' + E.Message);
- end;
- finally
- if SMTP.Connected then
- SMTP.Disconnect;
- end;
-
- MailMessage.Free;
- SSLSocket.Free;
- SMTP.Free;
- end;
编译后需要SSL动态库支持,支持库可以到Indy网站上下载到。
如果需要发送附件,可以再发送前添加如下类似代码
- // 添加邮件的附件
- TIdAttachment.Create(MailMessage.MessageParts, sAttachmentFileName);
————–
Indy需要的SSL支持dll下载地址 http://www.indyproject.org/Sockets/SSL.EN.aspx
没有评论:
发表评论