我正在尝试通过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"
}
}'
我执行了下一个功能,使用带有indy的delphi 10.3发送电子邮件,但出现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;