Javascript 从 AngularJS Web 应用程序发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25759686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending email from an AngularJS web application
提问by Ravi Shankar B
In one of my AngularJS web applications, I need to confirm a password by sending emails to a concerned person. How can I achieve this in AngularJS? I am a .NET guy and I am using Visual Studio 2013.
在我的一个 AngularJS Web 应用程序中,我需要通过向相关人员发送电子邮件来确认密码。我怎样才能在 AngularJS 中实现这一点?我是一个 .NET 人,我使用的是 Visual Studio 2013。
采纳答案by Ravi Shankar B
I have achieved through web services Please refer the code snippet below
我已经通过网络服务实现了请参考下面的代码片段
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;
}
}
and ajax call as
和 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);
}
});
回答by cfprabhu
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:
您也可以考虑使用第三方 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": "[email protected]",
"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);
});
}
})
回答by Behnam Mohammadi
you cannot send email via javascript library (angularjs or jquery and so on) you need server side for send mail best way for this case use ajax
您无法通过 javascript 库(angularjs 或 jquery 等)发送电子邮件,您需要服务器端发送邮件,这种情况下使用 ajax 的最佳方式
回答by Ravi
.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": "[email protected]",
"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);
});
}
})

