我正在尝试使用nodemailer 0.7.1发送附件。附件发送正常,但当我尝试打开它时,显示打开文件时出错。
下面是我的代码:
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);
}
});任何关于如何解决这个问题的建议都会有很大的帮助。
发布于 2016-11-09 00:16:33
将filename和filePath行替换为path: './result/results.txt',然后尝试。
发布于 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中,这对您很有帮助。
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');
}
})
});https://stackoverflow.com/questions/39164251
复制相似问题