使用gmail rest api -objective-c发送带有附件的电子邮件 [英] Send e-mail with attachments using gmail rest api -objective-c
问题描述
我正在尝试使用gmail api发送带有附件的电子邮件. 这是我的网址请求:
I am trying to send an e-mail with attachment using gmail api. This is my url request:
NSString *urlString = [NSString stringWithFormat:@"%@?access_token=%@&uploadType=multipart", @"apiURL", @"access_token"]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"message" ofType:@"txt"];
NSString *myText = [NSString stringWithContentsOfFile:filePath];
[request setHTTPBody:[myText dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:@"multipart/related; boundary=foo_bar_baz" forHTTPHeaderField:@"Content-Type"];
//make request, etc....
这是message.txt文件的内容,其中包含json和附件数据.
and this is the content of message.txt file which contains json and attachment data.
POST /upload/gmail/v1/users/userId/messages/send?uploadType=multipart HTTP/1.1
Content-Type: multipart/related; boundary="foo_bar_baz"
Content-Length: 99999999999999999999999999
--foo_bar_baz
Content-Type: application/json; charset=UTF-8
{
"raw":"RnJvbTogSm9obiBEb2UgPGpkb2VAbWFjaGluZS5leGFtcGxlPiAKVG86IFRlc3QgTmFtZSA8Y2VtaWx0b2thdGxpQGNyZWF2ZXguY29tPiAKU3ViamVjdDogU2F5aW5nIEhlbGxvIApEYXRlOiBGcmksIDIxIE5vdiAxOTk3IDA5OjU1OjA2IC0wNjAwIAoKVGhpcyBpcyBhIG1lc3NhZ2UganVzdCB0byBzYXkgaGVsbG8uIFNvLCAiSGVsbG8i"
}
--foo_bar_baz
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="anothertest.jpg"
Content-Type: message/rfc822;
(image data)
--foo_bar_baz--
我可以成功发送消息,但是看不到附件.
I can send message successfully but I can't see the attachment.
推荐答案
我对Objective-c不太熟悉,但是不久前我在JavaScript中也犯了同样的错误(
I'm not that familiar with objective-c, but I did the same mistake in JavaScript a while back (Mail attachment wrong media type Gmail API)
在发送之前,必须在原始参数中提供整个邮件.在您的情况下,可能是这样的:
You have to supply the ENTIRE mail in the raw-parameter before sending it. In your case it could be something like:
Content-Type: multipart/mixed; boundary="foo_bar_baz"
Content-Length: 99999999999999999999999999
--foo_bar_baz
Content-Type: text/plain; charset="UTF-8"
This is a message just to say hello. So, "Hello"
--foo_bar_baz
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="anothertest.jpg"
Content-Type: message/rfc822;
(image data)
--foo_bar_baz--
像这样构造邮件后,必须对urlSafe Base64进行编码,并提供此编码后的字符串作为原始参数!
When you've constructed your mail like this, you have to urlSafe Base64-encode it and supply this encoded string as your raw-parameter!
$.ajax({
type: "POST",
url: "https://www.googleapis.com/gmail/v1/users/me/messages/send",
contentType: "application/json",
dataType: "json",
beforeSend: function(xhr, settings) {
xhr.setRequestHeader('Authorization','Bearer ' + accessToken);
},
data: JSON.stringify({"raw": mail}) //mail is the encoded mail above
});
这篇关于使用gmail rest api -objective-c发送带有附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!