从AngularJS Web应用程序发送电子邮件

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

3
AngularJS应用程序无法发送电子邮件,您需要向.NET服务器发送AJAX请求,然后服务器将处理其余部分。 - Pepijn
使用WebAPI2创建一个AngularJS服务来发送电子邮件,只需将Angular的http服务绑定即可。 - TGarrett
4个回答

9

You might also look into using a third party SaaS, like Mandrill, SendGrid, or MailGun. I have worked w/ building in an email feature using Mandrill. Account is free to setup and threshold for billing on messaging is like 12k/month. Sending is just a matter of following their API to HTTP POST to their REST interface. Small example below:

.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);
      });
  }
})


10
问题在于API密钥。如果它对Angular可用,那么它基本上对每个人都是可用的。所以,如果你关心自己的话,我不建议这样做 :) - tkarls
如果您编写了一个PHP脚本并从自己的服务器运行,会怎样呢? - Dan Mantyla

4
我通过网络服务实现了这个功能,请参考下面的代码片段。
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。


0

.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);
      });
  }
})


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接