码迷,mamicode.com
首页 > Windows程序 > 详细

在Delphi中使用indy SMTP发送gmail邮件[转]

时间:2014-12-31 17:50:12      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

在Delphi中使用indy SMTP发送gmail邮件[转]  

2012-01-01 22:44:30|  分类: Delphi |  标签: |举报 |字号 订阅

 
 
在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.From.Address := ‘SunnyYu2000@gmail.com‘;
  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.Connectedthen
      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

在Delphi中使用indy SMTP发送gmail邮件[转]

标签:

原文地址:http://www.cnblogs.com/honeynm/p/4196087.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!