使用 Sendblue API 和 TIdHTTP 发送电子邮件

Send email with Sendblue API and TIdHTTP

我正在尝试通过 sendblue api 发送电子邮件。

使用 Postman 应用程序,我可以使用如下方式发送电子邮件:

curl -X POST \
  https://api.sendinblue.com/v3/smtp/email \
  -H 'Accept-Encoding: gzip, deflate' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Content-Length: 497' \
  -H 'Cookie: __cfduid=d49b2d4eb12e019d10adc31e48c2682001573229677' \
  -H 'Host: api.sendinblue.com' \
  -H 'Postman-Token: 79980bf9-b0f5-442d-a93e-0835ef471bac,6653fb5c-29da-4a00-83bc-cecb1c70b0bb' \
  -H 'User-Agent: PostmanRuntime/7.19.0' \
  -H 'accept: application/json' \
  -H 'api-key: xkeysib-728f8759d3fdbasdasdasdasd5asdsq083a48387459eba9cc57f9ad7904e-dqLADTR6vxw4y3GQ' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{  
   "sender":{  
      "name":"Sender Alex",
      "email":"gestaaloud@outlook.com"
   },
   "to":[  
      {  
         "email":"cprmlasasaao@gmail.com",
         "name":"John Doe"
      }
   ],
   "subject":"test mail",
   "htmlContent":"<html><head></head><body><h1>Hello this is a test email from sib</h1></body></html>",
   "headers":{  
      "X-Mailin-custom":"custom_header_1:custom_value_1|custom_header_2:custom_value_2|custom_header_3:custom_value_3",
      "charset":"iso-8859-1"
   }
}'

我执行了下一个功能,使用 delphi 10.3 和 indy 发送电子邮件,但我收到 404 错误。我做错了什么?

procedure Test_Email();
var  fIdHTTP : TIdHTTP;
     hdlSocket:TIdSSLIOHandlerSocketOpenSSL;
     RequestUTF8 : TStringStream;
     js,jso : TJSONobject;
     ja :TJsonArray;
     function GetJsonMail: String;
      begin
      js := TJSONobject.Create;
      try
         jso:= TJSONobject.Create;
         jso.AddPair(TJSONPair.Create('name'  , 'Gestan'));
         jso.AddPair(TJSONPair.Create('email'  , 'gestancsadfasd@outlook.com'));
         js.AddPair('sender',jso);

         ja := TJSONArray.Create;
         jso:= TJSONobject.Create;
         jso.AddPair(TJSONPair.Create('email'  , 'cprmlasdaao@gmail.com'));
         jso.AddPair(TJSONPair.Create('name'  , 'Luiz'));
         ja.AddElement(jso);
         js.AddPair(TJSONPair.Create('to'  , ja));


         js.AddPair(TJSONPair.Create('subject'  , 'Teste email'));
         js.AddPair(TJSONPair.Create('htmlContent'  , '<b>Teste email</b>'));

         jso:= TJSONobject.Create;
         jso.AddPair(TJSONPair.Create('X-Mailin-custom'  , 'custom_header_1:custom_value_1|custom_header_2:custom_value_2|custom_header_3:custom_value_3'));
         jso.AddPair(TJSONPair.Create('charset'  , 'iso-8859-1'));
         js.AddPair(TJSONPair.Create('headers'  , jso));
         result:=js.ToJSON;
      finally
          js.Free;
      end;
   end;
begin
  fIdHTTP := TIdHTTP.Create(nil);
  with fIdHTTP do
  begin
    Request.Clear;
    Request.CustomHeaders.AddValue('api-key',API_KEY_BLUE);
    Request.ContentType := 'application/json';
    Request.Accept := 'application/json';
    Request.CharSet := 'iso-8859-1';
    HTTPOptions := [];
    Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0';
    HandleRedirects := true;
    hdlSocket:=TIdSSLIOHandlerSocketOpenSSL.Create(nil);
    with hdlSocket do
    begin
      SSLOptions.Method := sslvSSLv23;
      SSLOptions.Mode := sslmUnassigned;
      SSLOptions.VerifyMode := [];
      SSLOptions.VerifyDepth := 2;
    end;
    IOHandler := hdlSocket;
  end;

  RequestUTF8 := TStringStream.Create(GetJsonMail,TEncoding.UTF8);
  try
    fIdHTTP.Post('https://api.sendinblue.com/v3/email', RequestUTF8);
  finally
    RequestUTF8.Free;
    fIdHTTP.Free;
  end;
end;

HTTP 响应代码 404 表示您正在向不存在的 URL 发帖。查看 curl 和 TIdHTTP 调用使用的 URLs。它们是不同的 URL。卷曲代码发布到此 URL:

https://api.sendinblue.com/v3/smtp/email

但是您的 TIdHTTP 代码正在发布到此 URL 而不是:

https://api.sendinblue.com/v3/email

看出区别了吗?

您的 TIdHTTP 代码也没有设置所有与 curl 代码相同的 HTTP headers。特别是缺少 'Postman-Token' 和 'Cookie' headers。

试试这个:

procedure Test_Email();
var
  fIdHTTP : TIdHTTP;
  hdlSocket : TIdSSLIOHandlerSocketOpenSSL;
  RequestUTF8 : TStringStream;

  function GetJsonMail: String;
  var
    js, jso : TJSONObject;
    ja : TJSONArray;
  begin
    js := TJSONObject.Create;
    try
      jso := TJSONObject.Create;
      try
        jso.AddPair('name', 'Gestan');
        jso.AddPair('email', 'gestancsadfasd@outlook.com');
        js.AddPair('sender', jso);
      except
        jso.Free;
        raise;
      end;
      ja := TJSONArray.Create;
      try
        jso := TJSONObject.Create;
        try
          jso.AddPair('email', 'cprmlasdaao@gmail.com');
          jso.AddPair('name', 'Luiz');
          ja.AddElement(jso);
        except
          jso.Free;
          raise;
        end;
        js.AddPair('to', ja);
      except
        ja.Free;
        raise;
      end;
      js.AddPair('subject', 'Teste email');
      js.AddPair('htmlContent', '<b>Teste email</b>');
      jso := TJSONObject.Create;
      try
        jso.AddPair('X-Mailin-custom', 'custom_header_1:custom_value_1|custom_header_2:custom_value_2|custom_header_3:custom_value_3');
        jso.AddPair('charset', 'iso-8859-1');
        js.AddPair('headers', jso);
      except
        jso.Free;
        raise;
      end;
      Result := js.ToJSON;
    finally
      js.Free;
    end;
  end;

begin
  fIdHTTP := TIdHTTP.Create(nil);
  try
    fIdHTTP.Request.CustomHeaders.AddValue('api-key', API_KEY_BLUE);
    fIdHTTP.Request.CustomHeaders.AddValue('Postman-Token', '...');
    fIdHTTP.Request.CustomHeaders.AddValue('Cookie', '__cfduid=...');
    fIdHTTP.Request.ContentType := 'application/json';
    fIdHTTP.Request.Accept := 'application/json';
    fIdHTTP.Request.CacheControl := 'no-cache';
    fIdHTTP.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0';
    fIdHTTP.HandleRedirects := True;
    fIdHTTP.HTTPOptions := [];

    hdlSocket := TIdSSLIOHandlerSocketOpenSSL.Create(fIdHTTP);
    hdlSocket.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
    hdlSocket.SSLOptions.Mode := sslmUnassigned;
    hdlSocket.SSLOptions.VerifyMode := [];
    hdlSocket.SSLOptions.VerifyDepth := 2;
    fIdHTTP.IOHandler := hdlSocket;

    RequestUTF8 := TStringStream.Create(GetJsonMail, TEncoding.UTF8);
    try
      fIdHTTP.Post('https://api.sendinblue.com/v3/smtp/email', RequestUTF8);
    finally
      RequestUTF8.Free;
    end;
  finally
    fIdHTTP.Free;
  end;
end;