【问题标题】:Sending email from an AngularJS web application从 AngularJS Web 应用程序发送电子邮件
【发布时间】:2014-11-03 18:20:06
【问题描述】:

在我的一个 AngularJS Web 应用程序中,我需要通过向相关人员发送电子邮件来确认密码。如何在 AngularJS 中实现这一点?我是 .NET 人员,我正在使用 Visual Studio 2013。

【问题讨论】:

  • 您不能使用 AngularJS 应用程序发送电子邮件,您应该向您的 .NET 服务器发送 AJAX 请求,服务器将处理其余的。
  • 使用 webapi2 并创建一个 AngularJS 服务来发送电子邮件,只需从 angular 绑定 http 服务,你就可以开始了。

标签: javascript angularjs email


【解决方案1】:

.controller('sentMailCntrl',function($scope, $http){
  $scope.sendMail = function(a){
    console.log(a.toEmail);
    var mailJSON ={
        "key": "xxxxxxxxxxxxxxxxxxxxxxx",
        "message": {
          "html": ""+a.mailBody,
          "text": ""+a.mailBody,
          "subject": ""+a.subject,
          "from_email": "sender@sending.domain.com",
          "from_name": "Support",
          "to": [
            {
              "email": ""+a.toEmail,
              "name": "John Doe",
              "type": "to"
            }
          ],
          "important": false,
          "track_opens": null,
          "track_clicks": null,
          "auto_text": null,
          "auto_html": null,
          "inline_css": null,
          "url_strip_qs": null,
          "preserve_recipients": null,
          "view_content_link": null,
          "tracking_domain": null,
          "signing_domain": null,
          "return_path_domain": null
        },
        "async": false,
        "ip_pool": "Main Pool"
    };
    var apiURL = "https://mandrillapp.com/api/1.0/messages/send.json";
    $http.post(apiURL, mailJSON).
      success(function(data, status, headers, config) {
        alert('successful email send.');
        $scope.form={};
        console.log('successful email send.');
        console.log('status: ' + status);
        console.log('data: ' + data);
        console.log('headers: ' + headers);
        console.log('config: ' + config);
      }).error(function(data, status, headers, config) {
        console.log('error sending email.');
        console.log('status: ' + status);
      });
  }
})

【讨论】:

    【解决方案2】:

    您还可以考虑使用第三方 SaaS,例如 Mandrill、SendGrid 或 MailGun。我曾使用 Mandrill 构建电子邮件功能。帐户是免费设置的,消息费用的门槛是每月 12k。发送只是按照他们的 API 到 HTTP POST 到他们的 REST 接口的问题。下面的小例子:

    .controller('sentMailCntrl',function($scope, $http){
      $scope.sendMail = function(a){
        console.log(a.toEmail);
        var mailJSON ={
            "key": "xxxxxxxxxxxxxxxxxxxxxxx",
            "message": {
              "html": ""+a.mailBody,
              "text": ""+a.mailBody,
              "subject": ""+a.subject,
              "from_email": "sender@sending.domain.com",
              "from_name": "Support",
              "to": [
                {
                  "email": ""+a.toEmail,
                  "name": "John Doe",
                  "type": "to"
                }
              ],
              "important": false,
              "track_opens": null,
              "track_clicks": null,
              "auto_text": null,
              "auto_html": null,
              "inline_css": null,
              "url_strip_qs": null,
              "preserve_recipients": null,
              "view_content_link": null,
              "tracking_domain": null,
              "signing_domain": null,
              "return_path_domain": null
            },
            "async": false,
            "ip_pool": "Main Pool"
        };
        var apiURL = "https://mandrillapp.com/api/1.0/messages/send.json";
        $http.post(apiURL, mailJSON).
          success(function(data, status, headers, config) {
            alert('successful email send.');
            $scope.form={};
            console.log('successful email send.');
            console.log('status: ' + status);
            console.log('data: ' + data);
            console.log('headers: ' + headers);
            console.log('config: ' + config);
          }).error(function(data, status, headers, config) {
            console.log('error sending email.');
            console.log('status: ' + status);
          });
      }
    })

    【讨论】:

    • 嗯,问题出在 api 密钥上。如果它可用于角度,则基本上每个人都可以使用。所以如果你关心自己,我不会推荐这个:)
    • 如果您在自己的服务器上编写了一个 PHP 脚本会怎样?
    【解决方案3】:

    我已经通过web服务实现了请参考下面的代码sn-p

    public bool EmailNotification()
        {
                using (var mail = new MailMessage(emailFrom, "test.test.com"))
                {
                    string body = "Your message : [Ipaddress]/Views/ForgotPassword.html";
                    mail.Subject = "Forgot password";
                    mail.Body = body;
                    mail.IsBodyHtml = false;
                    var smtp = new SmtpClient();
                    smtp.Host = "smtp.gmail.com";
                    smtp.EnableSsl = true;
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new NetworkCredential(emailFrom, emailPwd);
                    smtp.Port = 587;
                    smtp.Send(mail);
                    return true;
                }
        }
    

    和 ajax 调用一样

     $.ajax({
            type: "POST",
            url: "Service.asmx/EmailNotification",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data)
            {
    
            },
            error: function (XHR, errStatus, errorThrown) {
                var err = JSON.parse(XHR.responseText);
                errorMessage = err.Message;
                alert(errorMessage);
            }
        });
    

    【讨论】:

      【解决方案4】:

      您不能通过 javascript 库(angularjs 或 jquery 等)发送电子邮件 你需要服务器端来发送邮件 这种情况下最好的方法是使用 ajax

      【讨论】:

        猜你喜欢
        • 2015-03-26
        • 1970-01-01
        • 1970-01-01
        • 2020-05-28
        • 2013-04-13
        • 2010-12-27
        • 2016-02-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多