通过用户输入生成电子邮件正文,从控制器发送电子邮件?

时间:2018-12-21 15:38:30

标签: c# html asp.net-mvc razor system.net.mail

基本上,我有一个html表单,我想从中获取用户输入,并使用它生成电子邮件正文,然后发送电子邮件。我以前从未编程过类似的程序,因此过去几天我一直在研究从网站发送电子邮件的不同方法,但是我发现的示例均与我的实际情况无关。我发现用不同的方法闲逛也未见成效。

就目前而言,我有一个声明每个字符串的模型:

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

namespace myWebsite.Models
{
    public class ServiceRequest
    {
        [Key]
        public int serviceRequestID { get; set; }
        [Required]
        public string serviceRequestName { get; set; }
        [Required]
        public string serviceRequestEmail { get; set; }
        public string serviceRequestCompany { get; set; }
        public string serviceRequestAddress { get; set; }
        [Required]
        public string serviceType { get; set; }
        public string serviceRequestCity { get; set; }
        public string serviceRequestState { get; set; }
        public string serviceRequestZip { get; set; }
        [Required]
        public string serviceRequestNumber { get; set; }
        public string serviceRequestComments { get; set; }

    }
}

然后在视图中,我使用@html帮助器创建了表单,以将模型中的字符串链接到用户输入字段:

@model myWebsite.ServiceRequest

@using (Html.BeginForm("Email", "ServiceRequestsController", FormMethod.Post))
{
    <div class="servicesReq">
        <div class="serviceReqForm">
            <div class="col-left">
                <label>
                    <span class="required">* </span><span>Name:</span><br />
                    @Html.TextBoxFor(model => model.serviceRequestName)
                </label><br />
                        <label>
                            <span class="required">* </span><span>Email:</span><br />
                            @Html.TextBoxFor(model => model.serviceRequestEmail)
                        </label><br />
                <label>
                    <span>Company:</span><br />
                    @Html.TextBoxFor(model => model.serviceRequestCompany)
                </label><br />
                <label>
                    <span>Address:</span><br />
                    @Html.TextBoxFor(model => model.serviceRequestAddress)
                </label><br /><br />
            </div>
            <div class="serviceTypeGrid">
                <span class="required">* </span><span>Please choose what type of servicing you require:</span><br /><br />
            </div>
            <div class="col-left2">
                <label class="serviceType">
                    @Html.RadioButton("serviceType", "Electrical") Electrical<br/>
                    @Html.RadioButton("serviceType", "Lighting") Lighting<br />
                    @Html.RadioButton("serviceType", "Energy") Energy<br />
                    @Html.RadioButton("serviceType", "Multiple") Multiple
                </label><br /><br />
            </div>
            <div class="col-right">
                <label>
                    <span>City:</span><br />
                    @Html.TextBoxFor(model => model.serviceRequestCity)
                </label><br />
                <label>
                    <span>State:</span><br />
                    @Html.TextBoxFor(model => model.serviceRequestState)
                </label><br />
                <label>
                    <span>Zip Code:</span><br />
                    @Html.TextBoxFor(model => model.serviceRequestZip)
                </label><br />
                <label>
                    <span class="required">* </span><span>Phone Number:</span><br />
                    @Html.TextBoxFor(model => model.serviceRequestNumber)
                </label><br />
            </div>
            <label class="commentText">
                <span>Comments:</span><br />
                @Html.TextAreaFor(model => model.serviceRequestComments, new { @class = "servReqComment" })
            </label>
            <div class="formSubmitButton">
                <input type="submit" value="Submit" /><br />
                <br />
            </div>
        </div>

    </div>
}

最后是我的控制器逻辑-这是我几乎完全迷失的地方:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using myWebsite.Models;

namespace myWebsite.Controllers
{
    public class ServiceRequestsController : Controller
    {
        private CuttingEdgeWebsite2_0Context db = new CuttingEdgeWebsite2_0Context();

        // GET: ServiceRequests
        public ActionResult ServiceRequest()
        {
            return View();
        }

    }
}

正如我之前说过的,我已经用几种不同的方法进行了查找,但是它们都涉及对电子邮件正文进行硬编码。我用有限的知识尝试过的所有内容,都无法从表单上的用户输入生成主体。尽管我对如何实际发送邮件有某种想法,但是对该过程进行任何深入的说明也将非常有帮助。如果您在这里没有提供任何有用的信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

这应该让您开始。以编程方式发送电子邮件(至少)有四个属性。这些是从地址,地址,主题和正文开始的。

在您的表单中,您将要利用validationsummary。这向用户提供了验证反馈。只需插入

$count = $compPricing[0]; $result=array(); foreach($count as $number){ $Amount=$count->Amount->__toString(); $CurrencyCode=$count->CurrencyCode->__toString(); $result = array('Amount' => $Amount, 'CurrencyCode' => $CurrencyCode); } echo json_encode($Amount);

在BeginForm行之后

根据您的代码,这种方法将起作用。 在您的控制器中,您需要一种方法来处理表单数据的过帐,并需要一种方法来发送电子邮件(为便于设计,应采用单独的方法)。

ffmpeg -loop 1 -i logo.png -i 01.mp4 -i 02.mp4 -i 03.mp4 -i 04.mp4 -filter_complex "[1:v]scale=960x540[a]; [2:v]scale=960x540[b]; [3:v]scale=960x540[c]; [4:v]scale=960x540[d]; [0:v][a]overlay=0:0:shortest=1[e]; [0:v][b]overlay=960:0:shortest=1[e]; [0:v][c]overlay=540:0:shortest=1[e]; [e][d]overlay=0:960[video]" -y -ss 00:00:00 -t 00:00:05 -b 3000k -map "[video]" 05.mp4