如果您没有SMTP服务器并且想要从共享电子邮件地址发送电子邮件,您可以使用第三方电子邮件服务提供商(例如SendGrid,Mailgun或Amazon SES)作为替代方案
npm install @sendgrid/mail
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey('your_sendgrid_api_key');
const msg = {
to: 'recipient@example.com',
from: 'shared_email@example.com',
subject: 'Hello from SendGrid',
text: 'This is a test email sent using SendGrid.',
};
sgMail.send(msg).then(() => {
console.log('Email sent');
}).catch((error) => {
console.error(error);
});
请将your_sendgrid_api_key
替换为您的实际API密钥,并将recipient@example.com
和shared_email@example.com
替换为收件人和发件人的电子邮件地址。
npm install mailgun-js
const Mailgun = require('mailgun-js');
const api_key = 'your_mailgun_api_key';
const domain = 'your_mailgun_domain';
const mailgun = new Mailgun({ apiKey: api_key, domain: domain });
const data = {
from: 'shared_email@example.com',
to: 'recipient@example.com',
subject: 'Hello from Mailgun',
text: 'This is a test email sent using Mailger.',
};
mailgun.messages().send(data, function (error, body) {
console.log(body);
});
请将your_mailgun_api_key
和your_mailgun_domain
替换为您的实际API密基和域名,并将recipient@example.com
和shared_email@example.com
替换为收件人和发件人的电子邮件地址。
这些示例使用Node.js编写,但SendGrid和Mailgun都提供了其他编程语言的库。您可以查阅官方文档了解如何使用其他语言发送电子邮件。
领取专属 10元无门槛券
手把手带您无忧上云