Using the Indy TidHttp component to send email file attachments through sendgrid

。_饼干妹妹 提交于 2019-12-22 11:03:35

问题


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;

回答1:


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;


来源:https://stackoverflow.com/questions/14574235/using-the-indy-tidhttp-component-to-send-email-file-attachments-through-sendgrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!