使用 TLS 使用来自 DELPHI(Indy) 的 Gmail 传出 SMTP [英] Using Gmails Outgoing SMTP from DELPHI(Indy) using TLS

查看:18
本文介绍了使用 TLS 使用来自 DELPHI(Indy) 的 Gmail 传出 SMTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 INDY 使用 SMTP 客户端在端口 25 上发送电子邮件,没有问题.

I am using INDY to send Email using the SMTP client on port 25 with no problem.

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

Now I need to send an Email using a Gmail account and for that I need to use TLS.

谁能提供一个简单的示例来说明如何做到这一点.

Can anyone provide a simple sample on how to do that.

谢谢

推荐答案

此代码适用于 GMail:

This code works for 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;

这篇关于使用 TLS 使用来自 DELPHI(Indy) 的 Gmail 传出 SMTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆