在我的一个AngularJS网络应用程序中,我需要通过向相关人员发送电子邮件来确认密码。我怎样才能在AngularJS中实现这一目标?我是一个.NET人,我正在使用Visual Studio 2013。
答案 0 :(得分:9)
您可能还会考虑使用第三方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);
});
}
})

答案 1 :(得分:4)
你不能通过javascript库发送电子邮件(angularjs或jquery等) 你需要服务器端发送邮件 这种情况的最佳方法是使用ajax
答案 2 :(得分: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);
}
});
答案 3 :(得分: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);
});
}
})