首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nodemailer附件在nodemailer 0.7.1中不起作用

Nodemailer附件在nodemailer 0.7.1中不起作用
EN

Stack Overflow用户
提问于 2016-08-26 18:35:30
回答 2查看 942关注 0票数 0

我正在尝试使用nodemailer 0.7.1发送附件。附件发送正常,但当我尝试打开它时,显示打开文件时出错。

下面是我的代码:

代码语言:javascript
复制
var nodemailer = require("nodemailer");

var transport = nodemailer.createTransport("SMTP", {
    host: "smtp.gmail.com", // hostname
    secureConnection: true, // use SSL
    port: <port>, // port for secure SMTP
    auth: {
        user: "example.example@gmail.com",
        pass: "password"
    }
});

console.log("SMTP Configured");

var mailOptions = {
    from: 'example.sender@gmail.com', // sender address
    to: 'example.receiver@gmail.com', // list of receivers
    subject: 'Report for Test Result', // Subject line
    text: 'Contains the test result for the test run in html file', // plaintext body
    attachments: [
        {
            'filename': 'results.txt',
            'filePath': './result/results.txt',
        }

    ]
};
transport.sendMail(mailOptions, function (error, response) {
    if (error) {
        console.log(error);
    } else {
        console.log("Message sent: " + response.message);
    }

});

任何关于如何解决这个问题的建议都会有很大的帮助。

EN

回答 2

Stack Overflow用户

发布于 2016-11-09 00:16:33

filenamefilePath行替换为path: './result/results.txt',然后尝试。

票数 0
EN

Stack Overflow用户

发布于 2017-07-29 19:06:20

尝试此链接您必须在Google Cloud Console中创建应用程序并从单击credentials的app.For的凭据启用Gmail API,然后在此链接的位置重定向此链接并将URIskeep https://developers.google.com/oauthplayground保存在另一个选项卡中打开此链接并单击右侧的设置符号并选中复选框(即,使用您自己的OAuth凭据)之后,您必须同时提供您的clientId和clientSecret.And左侧有一个文本框,其中带有类似输入您自己的作用域的占位符保留此链接https://mail.google.com/,然后单击授权API,然后单击令牌的交换授权码,然后您将获得您的refreshToken和accessToken将这两个保留在您的code.Hope中,这对您很有帮助。

代码语言:javascript
复制
const nodemailer=require('nodemailer');
const xoauth2=require('xoauth2');
var fs=require('fs');
var transporter=nodemailer.createTransport({
service:'gmail',
auth:{
    type: 'OAuth2',
    user:'Sender Mail',
clientId:'Your_clientId',//get from Google Cloud Console
clientSecret:'Your clientSecret',//get from Google Cloud Console
refreshToken:'Your refreshToken',//get from  https://developers.google.com/oauthplayground
accessToken:'Tor accessToken'//get from  https://developers.google.com/oauthplayground
},
});
fs.readFile("filePath",function(err,data){
var mailOptions={
from:' <Sender mail>',
to:'receiver mail',
subject:'Sample mail',
text:'Hello!!!!!!!!!!!!!',
attachments:[
{
    'filename':'filename.extension',//metion the filename with extension
     'content': data,
     'contentType':'application/type'//type indicates file type like pdf,jpg,...
}]
}
transporter.sendMail(mailOptions,function(err,res){
if(err){
    console.log('Error');
}
else{
console.log('Email Sent');
}
})
});
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39164251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档