【发布时间】:2021-05-14 21:38:52
【问题描述】:
如何使用 javascript 使用 Gmail SMTP 发送电子邮件。 我无法使用 javascript 以编程方式发送电子邮件。下面是我的代码。我已经允许谷歌使用不太安全的应用程序。请帮忙。 下面是我用过的全部代码。
/* SmtpJS.com - v3.0.0 */
var Email = {
send: function(a) {
return new Promise(function(n, e) {
a.nocache = Math.floor(1e6 * Math.random() + 1), a.Action = "Send";
var t = JSON.stringify(a);
Email.ajaxPost("https://smtpjs.com/v3/smtpjs.aspx?", t, function(e) {
n(e)
})
})
},
ajaxPost: function(e, n, t) {
var a = Email.createCORSRequest("POST", e);
a.setRequestHeader("Content-type", "application/x-www-form-urlencoded"), a.onload = function() {
var e = a.responseText;
null != t && t(e)
}, a.send(n)
},
ajax: function(e, n) {
var t = Email.createCORSRequest("GET", e);
t.onload = function() {
var e = t.responseText;
null != n && n(e)
}, t.send()
},
createCORSRequest: function(e, n) {
var t = new XMLHttpRequest;
return "withCredentials" in t ? t.open(e, n, !0) : "undefined" != typeof XDomainRequest ? (t = new XDomainRequest).open(e, n) : t = null, t
}
};
function sendEmail() {
console.log('yes');
Email.send({
Host: "smtp.gmail.com",
Username: "email id",
Password: "password",
To: "email id",
From: "email id",
Subject: "Sending Email using javascript",
Body: "Well that was easy!!",
})
.then(function(message) {
alert("mail sent successfully")
});
}
<form method="post">
<input type="button" value="Send Email" onclick="sendEmail()" />
</form>
【问题讨论】:
标签: javascript email smtp