yas*_*gra 7 node.js oauth-2.0 nodemailer exchange-basicauth
我正在使用nodemailer我的 Nodejs 应用程序发送电子邮件。
var payload = { auth:
{
user: smtpuser,
pass: smtppass
},
to : toAddr,
from : emailfrom,
cc : ccAddr,
subject : subject,
html : content,
attachments: attachments
};
var transporter = nodemailer.createTransport(
{ host: payload.host || 'smtp.office365.com', // Office 365 server
port: payload.port || 587, // secure SMTP
secure:payload.secure || false, // false for TLS - as a boolean not string - but the default is false so just remove this completely
auth: payload.auth,
debug: true,
tls: payload.tls || {ciphers: 'SSLv3'}
});
transporter.sendMail(payload, function (error, info) {
if (error) {
return console.log(error);
}
updateMessage(updatedMsg);
});
Run Code Online (Sandbox Code Playgroud)
我开始收到此错误:
错误:无效登录:535 5.7.3 身份验证失败 [SN4PR0601CA0002.namprd06.prod.outlook.com]
我的团队现在似乎已禁用基本身份验证。
我需要实现现代身份验证(Oauth2)才能通过nodemailer使用 Outlook id 发送邮件。
有人对此有任何想法吗?这需要什么配置(代码)更改?
经过长时间探索如何使用 OAuth2 从服务器发送电子邮件后,我最终得到了这个工作示例。
const msal = require('@azure/msal-node');
const fetch = require('node-fetch');
const clientSecret = process.env.CLIENT_SECRET;
const clientId = process.env.CLIENT_ID;
const tenantId = process.env.TENANT_ID;
const aadEndpoint =
process.env.AAD_ENDPOINT || 'https://login.microsoftonline.com';
const graphEndpoint =
process.env.GRAPH_ENDPOINT || 'https://graph.microsoft.com';
const msalConfig = {
auth: {
clientId,
clientSecret,
authority: aadEndpoint + '/' + tenantId,
},
};
const tokenRequest = {
scopes: [graphEndpoint + '/.default'],
};
const cca = new msal.ConfidentialClientApplication(msalConfig);
const tokenInfo = await cca.acquireTokenByClientCredential(tokenRequest);
const mail = {
subject: 'Microsoft Graph JavaScript Sample',
//This "from" is optional if you want to send from group email. For this you need to give permissions in that group to send emails from it.
from: {
emailAddress: {
address: 'noreply@company.com',
},
},
toRecipients: [
{
emailAddress: {
address: 'someemail@domain.com',
},
},
],
body: {
content:
'<h1>MicrosoftGraph JavaScript Sample</h1>This is the email body',
contentType: 'html',
},
};
const headers = new fetch.Headers();
const bearer = `Bearer ${tokenInfo.accessToken}`;
headers.append('Authorization', bearer);
headers.append('Content-Type', 'application/json');
const options = {
method: 'POST',
headers,
body: JSON.stringify({ message: mail, saveToSentItems: false }),
};
await fetch(
graphEndpoint + '/v1.0/users/youroutlookemail@company.com/sendMail',
options
);
Run Code Online (Sandbox Code Playgroud)
另请查看此处的电子邮件设置:
https://learn.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=javascript
也许它会对某人有所帮助;)
小智 4
我建议您使用 Microsoft Graph 发送电子邮件。有一个易于使用的 REST API,可以很好地与 OAuth 配合使用。
请找到下面的一些链接来帮助您快速构建它。
https://learn.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=http https://learn.microsoft.com/en-us/graph/auth/ auth-concepts?view=graph-rest-1.0 https://learn.microsoft.com/en-us/graph/tutorials/node?view=graph-rest-1.0
| 归档时间: |
|
| 查看次数: |
9472 次 |
| 最近记录: |