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

2023-06-09,,

在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邮件[转]的相关教程结束。

《在Delphi中使用indy SMTP发送gmail邮件[转].doc》

下载本文的Word格式文档,以方便收藏与打印。