【问题标题】:Unable to send email programmatically using JavaScript无法使用 JavaScript 以编程方式发送电子邮件
【发布时间】: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


    【解决方案1】:

    您可以找到一个非常有用的教程,在此处使用提到的 js 库来完成此操作,但是您需要对您的 Google 帐户进行一些调整才能允许它,这不是我写的,当然,但我现在正在尝试:

    https://netcorecloud.com/tutorials/how-to-send-emails-with-javascript/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      • 2013-08-30
      • 2014-12-10
      • 2010-11-22
      相关资源
      最近更新 更多