ドロップダウン選択に基づいて特定の電子メールアドレスに電子メールをリダイレクトすることができました。メールを[email protected]に移動するか、ドロップダウンから選択した情報が[email protected]に移動する必要があります。私はすでにフォームを作成していますが、MVC actionResultと@Razorビューを使用してこれを行う方法についてはわかりません。MVCビューのドロップダウン選択に基づいて専用電子メールアドレスに電子メールを送信するにはどうすればいいですか?
私の見解
@model ETWServices.Models.ContactUs
@{
var culture = System.Threading.Thread.CurrentThread.CurrentUICulture.Name.ToLowerInvariant();
}
<div class="cards">
<div class="card">
<div id="contact-form" class="clearfix">
@Html.ValidationSummary(true)
@using (Html.BeginForm())
{
<h1>
@Resources.ResContact.Title
</h1>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Subject)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Subject)
@Html.ValidationMessageFor(model => model.Subject)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Message)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.Message)
@Html.ValidationMessageFor(model => model.Message)
</div>
<p>
<div class="submit">
<input type="submit" value="@Resources.ResContact.Send" id="btnSubmit" />
</div>
</p>
}
</div>
</div>
</div>
お問い合わせは、コントローラ
[HttpPost]
public ActionResult Contact(ContactUs contUs)
{
if (ModelState.IsValid)
{
try
{
MailMessage mailMsg = new MailMessage();
mailMsg.From = new MailAddress(contUs.Email);
mailMsg.To.Add("[email protected]");
mailMsg.Subject = contUs.Subject;
mailMsg.Body = contUs.Message;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "gmailpassword");
smtp.EnableSsl = true;
smtp.Send(mailMsg);
ModelState.Clear();
}
catch (Exception ex)
{
ModelState.Clear();
ViewBag.Message = " Sorry we are facing Problem here ";
}
}
return View();
}
お問い合わせモデル
public class ContactUs
{
[Display(Name = nameof(Resources.ResContact.Name), ResourceType = typeof(Resources.ResContact))]
[Required(ErrorMessageResourceType =typeof(Resources.ResContact),
ErrorMessageResourceName = nameof(Resources.ResContact.NameReq))]
[StringLength(20, MinimumLength = 5,ErrorMessageResourceType =typeof(Resources.ResContact),
ErrorMessageResourceName = "NameShort")]
public string Name { get; set; }
[Display(Name = "Email", ResourceType =typeof(Resources.ResContact))]
[Required(ErrorMessageResourceType = typeof(Resources.ResContact),
ErrorMessageResourceName = "Emailrequired")]
[RegularExpression("[email protected]+\\..+", ErrorMessageResourceType = typeof(Resources.ResContact),
ErrorMessageResourceName = "EmailWrong")]
public string Email { get; set; }
[Required(ErrorMessageResourceType =typeof(Resources.ResContact),
ErrorMessageResourceName ="subjectRequired")]
[Display(Name = "Subject", ResourceType = typeof(Resources.ResContact))]
public string Subject { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.ResContact),
ErrorMessageResourceName = "msgRequired")]
[Display(Name = "Message", ResourceType = typeof(Resources.ResContact))]
public string Message { get; set; }
}