私はセールスフォースでsendgrid apiを使用して添付ファイル付きのメールを送信するためにこれを使用しています...私は悪い要求とステータスコードとしてエラー応答を取得しています:400
誰か私がそれを理解するのを手伝ってください..???
String Boundary = '----------------------------400f182a9360';
String requestBody;
requestBody ='--' + Boundary +'\r\n\r\n'+'Content-Disposition: form-data; name="api_user"'+'\r\n\r\n'+'youremail'+'\r\n\r\n' ;
requestBody +='--' + Boundary +'\r\n\r\n'+'Content-Disposition: form-data; name="api_key"'+'\r\n\r\n'+'yourpassword'+'\r\n\r\n' ;
requestBody +='--' + Boundary +'\r\n\r\n'+'Content-Disposition: form-data; name="to"'+'\r\n\r\n'+'xxx@gmail.com'+'\r\n\r\n' ;
requestBody +='--' + Boundary +'\r\n\r\n'+'Content-Disposition: form-data; name="subject"'+'\r\n\r\n'+'test'+'\r\n\r\n' ;
//requestBody +='--' + Boundary +'\r\n\r\n'+'Content-Disposition: form-data; name="html"'+'\r\n\r\n'+'<b>hi</b>'+'\r\n\r\n' ;
requestBody +='--' + Boundary +'\r\n\r\n'+'Content-Disposition: form-data; name="text"'+'\r\n\r\n'+'mail'+'\r\n\r\n' ;
requestBody +='--' + Boundary +'\r\n\r\n'+'Content-Disposition: form-data; name="from"'+'\r\n\r\n'+'yyy@gmail.com'+'\r\n\r\n' ;
requestBody += '--' + Boundary + '\r\n\r\n'+'Content-Disposition: form-data; name="files['+mails.filename+']";filename="'+mails.filename+ '"\r\n\r\n'+ 'Content-Type:'+mails.conType+'\r\n\r\n'+Encodingutil.base64Encode(mails.bl)+'\r\n\r\n';
requestBody+= '--' + Boundary + '--\r\n';
String url= 'https://sendgrid.com/api/mail.send.json';
Http h = new Http();
HttpRequest req = new HttpRequest();
// req.setTimeout(60000);
req.setEndpoint(url);
req.setMethod('GET');
req.setHeader('Content-Type', 'multipart/form-data; boundary=' +Boundary);
req.setHeader('Content-Length',String.valueof(requestBody.length()));
system.debug('=====length======'+String.valueof(requestBody.length()));
//req.setHeader('Authorization','Bearer '+ssId);
req.setBody(requestBody);
HttpResponse res;
String resURL;
res = h.send(req);
response=res.getbody();
System.debug('++++++++++++++'+requestBody);
System.debug('++++++++++++++'+url);
system.debug('=====response is======'+response);
if(response.contains('success'))
{
response='{"message":"success"}';
}
else
{
response='error: '+res.getbody();
}
これの出力は
------------------------------400f182a9360
Content-Disposition: form-data; name="api_user"
sendgridusername
------------------------------400f182a9360
Content-Disposition: form-data; name="api_key"
sendgridpassword
------------------------------400f182a9360
Content-Disposition: form-data; name="to"
xxx@gmail.com
------------------------------400f182a9360
Content-Disposition: form-data; name="subject"
test
------------------------------400f182a9360
Content-Disposition: form-data; name="text"
mail
------------------------------400f182a9360
Content-Disposition: form-data; name="from"
yyy@gmail.com
------------------------------400f182a9360
Content-Disposition: form-data; name="files[New Text Document.txt]";filename="New Text Document.txt"
Content-Type:text/plain
YW5pbWVzaCBzZGYgc2RmIHMgZiBzIGRmIHNk
------------------------------400f182a9360--