问题描述
我正在使用Indy 。
现在我的任务是包括文件附件。在API文档中,它指出:文件内容必须是多部分HTTP POST的一部分
我试图修改函数以包括 TIdMultipartFormDataStream 失败。
我应该如何修改代码以支持文件附件?
过程SendGridEmailProc;
var
IdHTTP1:TIdHTTP;
IdSSLIOHandlerSocket1:TIdSSLIOHandlerSocketOpenSSL;
mString:字符串;
mParams:TStringList;
i:整数;
开始
尝试
mParams:= TStringList.Create;
IdHTTP1:= TIdHTTP.create(nil);
IdSSLIOHandlerSocket1:= TIdSSLIOHandlerSocketOpenSSL.create(nil);
与IdSSLIOHandlerSocket1开始
SSLOptions.Method:= sslvSSLv3;
SSLOptions.Mode:= sslmUnassigned;
SSLOptions.VerifyMode:= [];
SSLOptions.VerifyDepth:= 2;
结尾;
与IdHTTP1一起开始
IOHandler:= IdSSLIOHandlerSocket1;
结尾;
mParams.Add(’api_user = xxxxx’);
mParams.Add(’api_key = xxxxx’);
mParams.Add(’to = xxxxxx’);
mParams.Add(’from = xxxxx’);
mParams.Add(’subject = test:’+ datetimetostr(now));
mParams.Add(’text = this is a test’);
IdHTTP1.Post(’https://sendgrid.com/api/mail.send.xml’,mParams);
最终
个mParams。
idhttp1.free;
IdSSLIOHandlerSocket1.Free;
结尾;
结尾;
尝试以这种方式进行操作(请注意,未经测试):
使用
IdException,IdHTTP,IdSSLOpenSSL,IdMultipartFormData;
过程SendGridEmailProc;
var
HTTPClient:TIdHTTP;
参数:TIdMultiPartFormDataStream;
SSLHandler:TIdSSLIOHandlerSocketOpenSSL;
开始
HTTPClient:= TIdHTTP.Create(nil);
试试
SSLHandler:= TIdSSLIOHandlerSocketOpenSSL.Create(nil);
试试
SSLHandler.SSLOptions.Method:= sslvSSLv3;
SSLHandler.SSLOptions.Mode:= sslmUnassigned;
SSLHandler.SSLOptions.VerifyMode:= [];
SSLHandler.SSLOptions.VerifyDepth:= 2;
HTTPClient.IOHandler:= SSLHandler;
参数:= TIdMultiPartFormDataStream.Create;
试试
Parameters.AddFormField('api_user','xxxxx');
Parameters.AddFormField('api_key','xxxxx');
Parameters.AddFormField('to','xxxxxx');
Parameters.AddFormField('from','xxxxx');
Parameters.AddFormField('subject','test:'+ DateTimeToStr(Now));
Parameters.AddFormField('text','this is a test');
//注意,您需要为文件使用相同的文件名[]
//标识符,并且应显式指定
//类型的内容满足帮助
Parameters.AddFile('files [file.jpg]','c:\file.jpg',
'application / octet-stream')中显示的HTTP转储的文件字段;
试试
HTTPClient.Post(’https://sendgrid.com/api/mail.send.xml’,
参数);
,但E:EIdException上的
除外$ do $$ ShowMessage(E.ClassName +’:’+ E.Message);
结尾;
最终
Parameters.Free;
结尾;
最后
SSLHandler.Free;
结尾;
最终
HTTPClient.free;
结尾;
结尾;
I am using the Indy TIdHTTP component and am successfully able to send emails via the sendgrid API using the following function. The sendgrid api documentation can be found here.
Now I am tasked with including file attachments. In the API documentation it states: The file contents must be part of the multipart HTTP POST
I have attempted to modify my function to include the TIdMultipartFormDataStream with no success.
How should I modify the code to support file attachments?
procedure SendGridEmailProc;
var
IdHTTP1: TIdHTTP;
IdSSLIOHandlerSocket1: TIdSSLIOHandlerSocketOpenSSL;
mString: string;
mParams: TStringList;
i: Integer;
begin
try
mParams := TStringList.Create;
IdHTTP1 := TIdHTTP.create(nil);
IdSSLIOHandlerSocket1 := TIdSSLIOHandlerSocketOpenSSL.create(nil);
with IdSSLIOHandlerSocket1 do begin
SSLOptions.Method := sslvSSLv3;
SSLOptions.Mode := sslmUnassigned;
SSLOptions.VerifyMode := [];
SSLOptions.VerifyDepth := 2;
end;
with IdHTTP1 do begin
IOHandler := IdSSLIOHandlerSocket1;
end;
mParams.Add('api_user=xxxxx');
mParams.Add('api_key=xxxxx');
mParams.Add('to=xxxxxx');
mParams.Add('from=xxxxx');
mParams.Add('subject=test:'+datetimetostr(now));
mParams.Add('text=this is a test');
IdHTTP1.Post('https://sendgrid.com/api/mail.send.xml',mParams);
finally
mParams.Free;
idhttp1.free;
IdSSLIOHandlerSocket1.Free;
end;
end;
Try to do it this way (note, that it's untested):
uses
IdException, IdHTTP, IdSSLOpenSSL, IdMultipartFormData;
procedure SendGridEmailProc;
var
HTTPClient: TIdHTTP;
Parameters: TIdMultiPartFormDataStream;
SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
HTTPClient := TIdHTTP.Create(nil);
try
SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
try
SSLHandler.SSLOptions.Method := sslvSSLv3;
SSLHandler.SSLOptions.Mode := sslmUnassigned;
SSLHandler.SSLOptions.VerifyMode := [];
SSLHandler.SSLOptions.VerifyDepth := 2;
HTTPClient.IOHandler := SSLHandler;
Parameters := TIdMultiPartFormDataStream.Create;
try
Parameters.AddFormField('api_user', 'xxxxx');
Parameters.AddFormField('api_key', 'xxxxx');
Parameters.AddFormField('to', 'xxxxxx');
Parameters.AddFormField('from', 'xxxxx');
Parameters.AddFormField('subject', 'test:' + DateTimeToStr(Now));
Parameters.AddFormField('text', 'this is a test');
// note, that you need to use the same file name for the files[]
// identifier and that you should explicitly specify the content
// type for a file field to meet the HTTP dump shown in the help
Parameters.AddFile('files[file.jpg]', 'c:\file.jpg',
'application/octet-stream');
try
HTTPClient.Post('https://sendgrid.com/api/mail.send.xml',
Parameters);
except
on E: EIdException do
ShowMessage(E.ClassName + ': ' + E.Message);
end;
finally
Parameters.Free;
end;
finally
SSLHandler.Free;
end;
finally
HTTPClient.free;
end;
end;
这篇关于使用Indy TidHttp组件通过sendgrid发送电子邮件文件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!