加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

使用TLS从DELPHI(Indy)发送SMTP

发布时间:2020-12-15 10:09:52 所属栏目:大数据 来源:网络整理
导读:我使用INDY发送电子邮件使用端口25上的SMTP客户端没有问题. 现在我需要使用Gmail帐户发送电子邮件,因此我需要使用TLS. 任何人都可以提供一个简单的样本,如何做到这一点. 谢谢 解决方法 此代码适用于GMail: begin IDSMTP1 := TIDSMTP.Create; IdSSLIOHandler
我使用INDY发送电子邮件使用端口25上的SMTP客户端没有问题.

现在我需要使用Gmail帐户发送电子邮件,因此我需要使用TLS.

任何人都可以提供一个简单的样本,如何做到这一点.

谢谢

解决方法

此代码适用于GMail:
begin
  IDSMTP1 := TIDSMTP.Create;
  IdSSLIOHandlerSocketOpenSSL1 := TIdSSLIOHandlerSocketOpenSSL.Create;
  try

    with IDSMTP1 do
    begin
      Host := srvr.Host;
      Port := srvr.Port;
      if (srvr.needAuthentication = 'Y') then
        AuthType := satDefault
      else
        AuthType := satNone;
      IOHandler := IdSSLIOHandlerSocketOpenSSL1;

      if (srvr.secureMode = 'Y') then
        UseTLS := utUseRequireTLS
      else
        UseTLS := utNoTLSSupport;

      Username := srvr.Username;
      Password := srvr.Password;
    end;

    idMBHTML := TIdMessageBuilderHTML.Create;
    Idmessage1 := TIDMessage.Create;

    try
      with idMBHTML do
      begin
        enc := TEncoding.Unicode;
        HTML.LoadFromStream(FEmlMsg.MsgBody,enc);
        for c := 0 to FEmlMsg.Attachmnts.Count - 1 do
          Attachments.Add(FEmlMsg.Attachmnts[c]);
        FillMessage(IDMessage1);
      end;

      with Idmessage1 do
      begin
        Subject := FEmlMsg.MsgSubject;
        From.Address := FEmlMsg.FromAddress;
        From.Name := FEmlMsg.FromName;
        Recipients.EMailAddresses := FEmlMsg.RecipientAddress;
        if FEmlMsg.ReceiptRecipientAddress <> '' then
          ReceiptRecipient.Address := FEmlMsg.ReceiptRecipientAddress;
        if FEmlMsg.ReceiptRecipientName <> '' then
          ReceiptRecipient.Name := FEmlMsg.ReceiptRecipientName;
      end;

      with IDSMTP1 do
      begin
        if not Connected then
          Connect;
        Send(IdMessage1);
      end;

    finally
      Idmessage1.Free;
      idMBHTML.Free;
    end;
  finally
    IDSMTP1.Free;
    IdSSLIOHandlerSocketOpenSSL1.Free;
  end;
end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读