如何在ng-click中自动发送邮件?

时间:2016-07-18 12:03:07

标签: angularjs

我在Visual Studio 2013中使用angularjs 1.4.8。我想发送电子邮件到xxxx @ xxxx.com,并带有消息"您的注册已完成"单击按钮后自动。如何通过angularjs发送电子邮件?

  [HttpPut]
    [Route("api/SendMail")]
    protected void sendmail()
    {

        var fromAddress = "xxxk@xxxxx.com";
        var toAddress = "rrrr@rrr.com";
        const string fromPassword = "workufs1234";
        MailMessage mailMessage = new MailMessage(fromAddress, toAddress);
        mailMessage.To.Add(toAddress);
        mailMessage.Subject = "Documents";
        string messge = "hellooooo";
        string tempmsg = "";
        var messg = @"<html><body><br />Dear  ss ,<br /><br />  <br />";
        mailMessage.To.Add(toAddress);
        string html = messg.ToString();
        AlternateView altView = AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html);
       MailMessage mail = new MailMessage();
        mailMessage.AlternateViews.Add(altView);
        mailMessage.Body = messg.ToString();
        mailMessage.IsBodyHtml = true;

        SmtpClient mailSender = new SmtpClient("162.222.225.82"); //use this if you are in the development server
        mailSender.Host = "smtp.gmail.com";
        mailSender.Port = 587;
        mailSender.EnableSsl = true;
        mailSender.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        mailSender.Credentials = new NetworkCredential(fromAddress, fromPassword);
        mailSender.Timeout = 40000;
        mailSender.Send(mailMessage);
    }

如何使用邮递员进行测试。是这个httppost或put或?

2 个答案:

答案 0 :(得分:0)

HTML code:

<button ng-click="sendMail()"></button>

控制器:

$scope.message = {} // contact info goes here

$scope.sendMail = function(){
  var mail = 'mailto:mohamed@labouardy.com?subject=' + $scope.message.name +
               '&body=' + $scope.message.content;
  $window.open(mail);
}

答案 1 :(得分:0)

我认为您需要服务器端才能正确执行此操作。 (PHP / NodeJS,...)

在我的NodeJS服务器上,我使用了&#39; nodemailer&#39;,它完美无缺。 https://www.npmjs.com/package/nodemailer

希望它有所帮助。