Sending gmail attachment using api failed

前端 未结 1 835
醉酒成梦
醉酒成梦 2020-12-09 14:10

I\'m trying create a draft (or send a message) with attachment to gmail using its API. I\'ve read some answers and tried to built the request according to what I\'ve read he

相关标签:
1条回答
  • 2020-12-09 14:45

    OK, so if you're using the /upload media feature (works for all messages irregardless of size) then for example it should be something like the following (and looks like i was a bit mistaken):

    POST https://www.googleapis.com/upload/gmail/v1/users/me/messages/send
    Content-Type: multipart/related; boundary=foo_bar_baz
    

    then your POST body should be something like the following (not encoded, etc):

    --foo_bar_baz
    Content-Type: application/json; charset=UTF-8
    
    {
    }
    
    --foo_bar_baz
    Content-Type: message/rfc822
    MIME-Version: 1.0
    to: receiver@gmail.com
    from: sender@gmail.com
    subject: Testing Subject
    
    --foo_bar_baz
    Content-Type: text/plain; charset="UTF-8"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    
    This is the testing text
    
    --foo_bar_baz
    
    Content-Type: image/jpeg
    MIME-Version: 1.0
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment; filename="test.jpg"
    
    
    
    
    --foo_bar_baz--
    

    So things to note are that it's actually "multipart/related" and that has a application/json (for some requests you can add parameters there) part as well as a message/rfc822 part that contains the entire email.

    It's not easy for sure--libraries definitely make it less painful if you can use them!

    0 讨论(0)
提交回复
热议问题