我正在使用它在salesforce中使用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--