【问题标题】:Unable to send mail using gmail api node js无法使用gmail api节点js发送邮件
【发布时间】:2019-04-15 05:42:07
【问题描述】:

我已关注gmail api 发送电子邮件。我收到错误:

“消息”:“400 - \”{\n \\“错误\\”:{\n \\“错误\\”:[\n {\n \\“域\\”:\\ "global\\",\n \\"reason\\": \\"invalidArgument\\",\n \\"message\\": \\"'raw' RFC822 有效负载消息字符串或通过 /upload 上传消息/* URL required\\"\n }\n ],\n \\"code\\": 400,\n \\"message\\": \\"'raw' RFC822 有效负载消息字符串或通过/upload/* URL 必填\\"\n }\n}\n\""

这是我为使用 gmail api 和 node.js 发送邮件而编写的一段代码。帮我解决问题。

router.post('/composeMail', async (req, res, next) => {
    function makeBody(to, from, subject, message) {
        let str = ["Content-Type: text/plain; charset=\"UTF-8\"\n",
            "Content-length: 5000\n",
            "Content-Transfer-Encoding: message/rfc822\n",
            "to: ", to,"\n",
            "from: ", from,"\n",
            "subject: ", subject,"\n\n",
            message
        ].join('');
        console.log("String: ", str);
        // let encodedMail = new Buffer(str).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
        let encodedMail = btoa(str).replace(/\+/g, '-').replace(/\//g, '_');
        return encodedMail;
    }
    let raw = makeBody("dinesh.kumar@gmail.com", "dinesh.kumar@gmail.com", "Test mail", "Everything is fine");
    let obj = {};
    obj.raw = raw;
    let body = JSON.stringify(obj);
    let option = {
        url: "https://www.googleapis.com/gmail/v1/users/userId/messages/send",
        method: 'POST',
        headers: {
            'Authorization': `Bearer ${req.query.access_token}`
        },
        qs: {
            userId: 'me'
        },
        body: body
    };

    await request(option).then(body => {
        return res.apiOk(body);
    }).catch(err => {
        return res.apiError(err);
    })
});

【问题讨论】:

  • 我的回答是否向您展示了您想要的结果?你能告诉我吗?这对我学习也很有用。如果这可行,与您有相同问题的其他人也可以将您的问题作为可以解决的问题。如果您对我的回答有疑问,请随时告诉我。我想学习解决你的问题。
  • 是的,你的回答对我有用@Tanaike
  • 欢迎。谢谢你让我知道。如果您的问题得到解决,请按接受按钮。与您有相同问题的其他人也可以将您的问题作为可以解决的问题。如果找不到按钮,请随时告诉我。 stackoverflow.com/help/accepted-answer

标签: node.js gmail-api google-api-nodejs-client google-apis-explorer


【解决方案1】:
  • 您想通过请求模块使用 Gmail API 发送电子邮件。

如果我的理解是正确的,那么这个修改呢?我认为有几个答案。因此,请将此视为其中之一。

修改点:

  • 请使用https://www.googleapis.com/upload/gmail/v1/users/userId/messages/send作为端点。
  • 将值用作字符串。
  • 在标题中添加'Content-Type': 'message/rfc822'

修改脚本:

请修改makeBody()如下。

function makeBody(to, from, subject, message) {
    let str = [
        "to: ", to, "\n",
        "from: ", from, "\n",
        "subject: ", subject, "\n\n",
        message,
    ].join('');
    return str;
}

请修改option如下。

let raw = makeBody("dinesh.kumar@gmail.com", "dinesh.kumar@gmail.com", "Test mail", "Everything is fine");
const userId = 'me'; // Please modify this for your situation.
let option = {
    url: "https://www.googleapis.com/upload/gmail/v1/users/" + userId + "/messages/send",
    method: 'POST',
    headers: {
        'Authorization': `Bearer ${req.query.access_token}`,
        'Content-Type': 'message/rfc822',
    },
    body: raw,
};

注意:

  • 此修改后的脚本假设在 API 控制台中启用了 Gmail API,并且发送电子邮件所需的范围包含在访问令牌的范围中。

参考:

在我的环境中,我可以确认这个修改后的脚本运行良好。但如果这不是你想要的,我很抱歉。

【讨论】:

  • @Dinesh Sathrasala 对不起。我无法理解你的评论。您可以将其发布为包含更多信息的新问题吗?当您发布它时,它将帮助包括我在内的用户想到您的问题。
  • 田池请查看我的帖子链接stackoverflow.com/questions/53761566/…
  • @Dinesh Sathrasala 感谢您的回复。我想检查一下。
  • 我已经找到了解决方案。感谢您的回复。
  • @Dinesh Sathrasala 谢谢你让我知道。我很高兴你的问题得到了解决。也谢谢你。
猜你喜欢
  • 2021-01-27
  • 1970-01-01
  • 1970-01-01
  • 2016-10-28
  • 1970-01-01
  • 1970-01-01
  • 2018-11-16
  • 2012-01-28
  • 1970-01-01
相关资源
最近更新 更多