Reputation: 49
I tried a project with send email in contact form. But when I run it, I get an error
Here is my code - this is my model class:
public class SendMail : IEmailSender
{
public async Task SendEmailAsync(string email, string subject, string htmlMessage)
{
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress(email);
mailMessage.Subject = subject;
mailMessage.Body = email + htmlMessage;
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress("[email protected]"));
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "[email protected]";
NetworkCred.Password = "cxhzevbomlrgebrb";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
await smtp.SendMailAsync(mailMessage);
}
}
}
This is my view .cshtml
:
@model eProject3_6.ViewModel.Contact;
<div class="row">
<div class="eight columns">
<!-- form -->
<form method="post" >
<fieldset>
<div>
<label asp-for="Email">Email <span class="required">*</span></label>
<input asp-for="Email" type="text" value="" size="35" id="contactEmail" name="contactEmail">
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div>
<label asp-for="Subject">Subject</label>
<input asp-for="Subject" type="text" value="" size="35" id="contactSubject" name="contactSubject">
<span asp-validation-for="Subject" class="text-danger"></span>
</div>
<div>
<label asp-for="Message">Message <span class="required">*</span></label>
<textarea asp-for="Message" cols="50" rows="15" id="contactMessage" name="contactMessage"></textarea>
<span asp-validation-for="Message" class="text-danger"></span>
</div>
<div>
<button class="submit">Submit</button>
<span id="image-loader">
<img alt="" src="~/ContactForm/images/loader.gif">
</span>
</div>
</fieldset>
</form> <!-- Form End -->
</div>
My here's my controller class:
[HttpGet]
public IActionResult Contact()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Contact(Contact contact)
{
var msg = contact.Subject + "" + contact.Message;
await _emailSender.SendEmailAsync(contact.Email, "Contact Message", msg);
ViewBag.Message = "Thank for your contact";
return View();
}
Then I call a class to declare string in ViewModel
folder:
public class Contact
{
public string Email { get ; set ; }
public string Subject { get; set; }
public string Message { get; set; }
}
When I run it, I get this error:
ArgumentNullException: Value cannot be null. (Parameter 'address')
System.Net.Mail.MailAddress.TryParse(string address, string displayName, Encoding displayNameEncoding, out ValueTuple<string, string, string, Encoding> parsedData, bool throwExceptionIfFail)
System.Net.Mail.MailAddress..ctor(string address)
eProject3_6.Models.SendMail.SendEmailAsync(string email, string subject, string htmlMessage) in SendMail.cs
mailMessage.From = new MailAddress(email);
eProject3_6.Controllers.HomeController.Contact(Contact contact) in HomeController.cs
+
await _emailSender.SendEmailAsync(contact.Email, "Contact Message", msg);Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor+TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments) System.Threading.Tasks.ValueTask.get_Result() System.Runtime.CompilerServices.ValueTaskAwaiter.GetResult() Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask actionResultValueTask) Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync() Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Why do I get this error? The email can't reference or something I don't know. And how can I fix this?
Upvotes: 0
Views: 334
Reputation: 62
In your cshtml file you put "contactEmail" in your input. Your property is named "Email" in your model "Contact".
Therefore the value of the input is not bind in the controller. You can check that by looking at what is send to your controler using the network tab.
Upvotes: 0