如何将提交的电子邮件发送到电子邮件

时间:2019-07-17 07:35:37

标签: asp.net asp.net-mvc

我当前正在使用模板来创建网站。我要在联系页面中创建一个表单,用户可以在其中使用“联系我们”模板与公司联系。该模板带有一个contact_us.php文件,但我只想将asp.net用于电子邮件。     我是一个初学者,我不确定如何在asp.net中使用控制器。

这里是我的代码:

查看: Contact.cshtml

@model Gsite.Models.ContactUs

@{
    Layout = "~/Views/Shared/_CustomLayout.cshtml";
}


<script src="~/Content/vendor/jquery/jquery.js"></script>
<script src="~/Content/vendor/jquery/jquery.min.js"></script>

    <!-- Contact Form -->
    <!-- In order to set the email address and subject line for the contact form go to the bin/contact_me.php file. -->
    <div class="row">
        <div class="col-lg-8 mb-4">
            <h3>Send us a Message</h3>
            @if (ViewBag.Message == null)
            {
                <form method ="post" name="sentMessage" id="contactForm" novalidate>
                    <div class="control-group form-group">
                        <div class="controls">
                            <label asp-for="Name">Full Name:</label>
                            <input asp-for="Name" type="text" class="form-control" id="name" required data-validation-required-message="Please enter your name.">
                            <span asp-validation-for="Name"
                                  class="text-muted"></span>
                            <p class="help-block"></p>
                        </div>
                    </div>
                    <div class="control-group form-group">
                        <div class="controls">
                            <label asp-for="phonenumber">Phone Number:</label>
                            <input asp-for="phonenumber" type="tel" class="form-control" id="phone" required data-validation-required-message="Please enter your phone number.">
                            <span asp-validation-for="phonenumber"
                                  class="text-muted"></span>
                        </div>
                    </div>
                    <div class="control-group form-group">
                        <div class="controls">
                            <label asp-for="Email">Email Address:</label>
                            <input asp-for="Email" type="email" class="form-control" id="email" required data-validation-required-message="Please enter your email address.">
                            <span asp-validation-for="Email"
                                  class="text-muted"></span>
                        </div>
                    </div>
                    <div class="control-group form-group">
                        <div class="controls">
                            <label asp-for="Message">Message:</label>
                            <textarea asp-for="Message" rows="10" cols="100" class="form-control" id="message" required data-validation-required-message="Please enter your message" maxlength="999" style="resize:none"></textarea>
                            <span asp-validation-for="Message"
                                  class="text-muted"></span>
                        </div>
                    </div>
                    <div id="success"></div>
                    <!-- For success/fail messages -->
                    <button type="submit" class="btn btn-primary" id="sendMessageButton">Send Message</button>
                </form>
            }
        </div>

    </div>
    <!-- /.row -->
    <div>
        <div>
            @if (ViewBag.Message != null)
            {
                <div>@ViewBag.Message</div>
            }
        </div>
    </div>
</div>


<script src="~/Content/vendor/jquery/jquery.min.js"></script>
<script src="~/Scripts/jqBootstrapValidation.js"></script>
<script src="~/Scripts/contact_me.js"></script>

控制器: ContactController

namespace Gsite.Controllers
{
    public class ContactController : Controller
    {
        // GET: Contact
        public ActionResult Contact()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Contact(ContactUs vm)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    MailMessage msz = new MailMessage();
                    msz.From = new MailAddress(vm.Email);//Email which you are getting 
                                                         //from contact us page 
                    msz.To.Add("xxx@xxx.com");//Where mail will be sent 

                    msz.Body = vm.Message;
                    SmtpClient smtp = new SmtpClient();

                    smtp.Host = "smtp.gmail.com";

                    smtp.Port = 587;

                    smtp.Credentials = new System.Net.NetworkCredential
                    ("xxxxx@xxxxx.com", "xxxxx");

                    smtp.EnableSsl = true;

                    smtp.Send(msz);

                    ModelState.Clear();
                    ViewBag.Message = "Thank you for Contacting us ";
                }
                catch (Exception ex)
                {
                    ModelState.Clear();
                    ViewBag.Message = $" Sorry we are facing Problem here {ex.Message}";
                }
            }

            return View();
        }
        public ActionResult Error()
        {
            return View();
        }
    }
}

型号: ContactUs.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Gsite.Models
{

    public class ContactUs
    {

        [Required]
        [StringLength(20, MinimumLength = 5)]
        public string Name { get; set; }
        [Required]
        [EmailAddress]
        public string Email { get; set; }
        [Required]
        public string Message { get; set; }
        [Required]
        public int phonenumber { get; set; }

    }
}

1 个答案:

答案 0 :(得分:0)

这将在Asp.net MVC应用程序中工作。该应用程序是否同时具有Asp.net MVC和Php代码?假设为“是”,则需要设置表单元素的action属性。

例如<form action="/Contact/Contact">

还要设置输入框的“名称”属性以匹配ContactUs对象的属性。