邮件附件错误的介质类型的Gmail API [英] Mail attachment wrong media type Gmail API

查看:111
本文介绍了邮件附件错误的介质类型的Gmail API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试发送邮件通过在Javascript客户端的Gmail的API连接的JPEG文件。在code我写到目前为止如下:

I'm trying to send a mail with a jpeg file attached through the Gmail API in Javascript client side. The code I've written so far is as follows:

$.ajax({
 type: "POST",
 url: "https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=multipart",
 contentLength: data.length,
 beforeSend: function (xhr, settings) {
   xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
   xhr.setRequestHeader('Content-Type', 'multipart/related; boundary="foo_bar_baz"');
 },
 data: data,
 success: function (x) {
   log(x);
 },
 error: function (x) {
   log(x);
 }
 });

在哪里数据是建立喜欢这里的例子中的字符串:的https: //developers.google.com/gmail/api/guides/uploads#multipart

--foo_bar_baz
Content-Type: application/json; charset=UTF-8

{ "raw": "RnJvbTogRW1pbCBUaG9saW4gPGVtdGhvbGluQGdtYWlsLmNvbT4KVG86IEV4YW1wbGUgTmFtZSA8ZW10aG9saW5AZ21haWwuY29tPgpTdWJqZWN0OiBzZHNkCgpzZHNk"}

--foo_bar_baz
Content-Type: image/jpeg

data:image_jpeg;base64,_9j_4AAQSkZJRgABAQEAYABgAAD_2wBDAAIBAQIBAQICAgICAgIC…bHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3-Pn6_9oADAMBAAIRAxEAPwD-f-iiigD_2Q==

--foo_bar_baz--

我得到的错误是不支持的媒体类型为image / jpeg有效的介质类型:[邮件/ RFC822],这是可以理解的,因为[消息/ RFC822]是唯一有效的MIME类型的媒体根据规范( https://developers.google.com/的Gmail / API / V1 /参考/用户/邮件/发送),但上面链接的例子另有规定的除外。

The error I get is "Media type 'image/jpeg' is not supported. Valid media types: [message/rfc822]", which is understandable since [message/rfc822] is the only valid MIME-type for the media according to the specification (https://developers.google.com/gmail/api/v1/reference/users/messages/send), but the example linked above states otherwise.

我是什么做错了吗?这将是pciated如果有人能摆脱一些这多少AP $ P $!

What am I doing wrong? It would be much appreciated if someone could shed some light on this!

推荐答案

这第一块code适用于附件的几MB的一个组合的大小。如果你想使用的35 MB允许的极限,检查编辑在回答结束。

EDIT

This first piece of code works for attachments with a combined size of a few mb. If you want to use the allowed limit of 35 mb, check the edit at the end of the answer.

在(整个邮件必须处于原始 - 参数),我只是尝试了Python API,看着由生成邮件。

After Steve pushed me in the right direction (the entire mail has to be in the "raw"-parameter), I simply tried the Python API and looked at the mail generated by that.

邮件withouth的附件

Content-Type: text/plain; charset="UTF-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
to: receiver@gmail.com
from: sender@gmail.com
subject: Subject Text

The actual message text goes here

邮件带有附件

Content-Type: multipart/mixed; boundary="foo_bar_baz"
MIME-Version: 1.0
to: receiver@gmail.com
from: sender@gmail.com
subject: Subject Text

--foo_bar_baz
Content-Type: text/plain; charset="UTF-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

The actual message text goes here

--foo_bar_baz
Content-Type: image/jpeg
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="example.jpg"

{JPEG data}

--foo_bar_baz--

所以,我只是写我的code解决这个问题,和它的工作太棒了!

So I just wrote my code around this, and it worked great!

var mail = "";
mail += "From: sender@gmail.com\n";
mail += "To: receiver@gmail.com\n";
mail += "Subject: Subject Text";
// If there are no attachments, the content is simply plain text
if(attachment == null)
{
  mail = 'Content-Type: text/plain; charset="UTF-8"\n' + mail;
  mail += "\n\n" + message;
  sendMessage();
}
// If there is an attachment, the type is mulipart/mixed
else {
  mail = 'Content-Type: multipart/mixed; boundary="foo_bar_baz"\n' + mail + '\n\n';

  // The regular message
  mail += '--foo_bar_baz\n';
  mail += 'Content-Type: text/plain; charset="UTF-8"\n\n';
  mail += message + '\n';

  var reader = new FileReader();
  reader.onloadend = function (e) {
    // The relevant base64-encoding comes after "base64,"
    var result = e.target.result.split('base64,')[1];
    mail += '--foo_bar_baz\n';
    mail += 'Content-Type: ' + attachment.type + '\n';
    mail += 'Content-Transfer-Encoding: base64\n';
    mail += 'Content-Disposition: attachment; filename="' + attachment.name + '"\n\n';

    mail += result + '\n';
    mail +=  '--foo_bar_baz--';
    sendMessage();
  }

  reader.readAsDataURL(attachment);

  function sendMessage() {
    // The Gmail API doesn't like regular btoa-encoding. Replace '+' with '-', and '/' with '_'
    mail = btoa(mail).replace(/\+/g, '-').replace(/\//g, '_');

    $.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}),
      success: function(data) {
        log(data);
      },
      error: function(data) {
        log(data);
      }
    });
  }
}


修改

在code以上的作品,但还需要一些改变,以使用35 MB的最大限制。


Edit

The code above works, but a few alterations are needed to use the max limit of 35 mb.

通过建立作为标题带附件 邮件下,例如邮件,改变的AJAX请求如下所示:

With a mail built up as the example under the heading Mail with attachment, the altered ajax-request looks as follows:

$.ajax({
      type: "POST",
      url: "https://www.googleapis.com/gmail/v1/users/me/messages/send?uploadType=multipart",
      contentType: "message/rfc822",
      beforeSend: function(xhr, settings) {
        xhr.setRequestHeader('Authorization','Bearer ' + accessToken);
      },
      data: mail // NOT encoded! The mail should now be in the unchanged rfc822-format.
}); 

将contentType现在是信息/ RFC822,而不是应用/ JSON的URI已经得到了一个新的参数uploadType =多部分,而最重要的是邮件不再Base64的EN codeD,但供应在RFC822格式。

The contentType is now "message/rfc822" instead of "application/json", the uri has gotten a new parameter "uploadType=multipart", and most importantly the mail is no longer Base64 encoded, but supplied in the rfc822-format.

这篇关于邮件附件错误的介质类型的Gmail API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆