Click here to Skip to main content
15,064,293 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to send mail from my ASP.NET Core 2.0 MVC application but mail is sending from only home page not sending from other pages, I am using same method for all page. There is no error in sending the mail. But I am not receiving the mail in my Gmail, but receiving mail from only home page.

What I have tried:

----Home Controller--
C#
[HttpPost]
public async Task<IActionResult> HomeContact(ContactModel contact)
{
    try
    {
        await emailHelper.SendEmail(contact);
        TempData["Message"] = "Email Has Been Sent Successfully.";
    }
    catch (Exception ex)
    {
        Global.SaveError(ex);
        TempData["Message"] = ex.ToString();

    }
    return RedirectToAction("Index");  
}

Note-- this above HomeContact is working if I call it from the home Page but for testing if I call this method from another pages this is not working , this was working only once but now not working.

----home page contact us view code ---
Razor
<form name="contactForm" method="post" enctype="multipart/form-data">
   
    <input type="hidden" name="Subject" value="Discuss" />
    <input type="hidden" name="PageLocation" value="Home" />

    <div class="row">
        <div class="col-lg-6 col-md-6 col-sm-6">
            <input type="text" name="Name" placeholder="Your Name *" required />
          
        </div>
        <div class="col-lg-6 col-md-6 col-sm-6">
            <input type="email" name="EmailID" placeholder="Your Mail *" required />
        </div>
        <div class="col-lg-6 col-md-6 col-sm-6">
            <input type="text" name="MobileNo" placeholder="Phone Number *" required />
        </div>
        <div class="col-lg-6 col-md-6 col-sm-6">
            <input type="text" name="Message" placeholder="Subject *" required />
        </div>
    </div>
    <button class="color1_bg tran3s" asp-action="HomeContact"
            style="background:#fff;">
        Submit
    </button>


    @if (ViewBag.Message != null)
    {
        <script> window.alert( '@ViewBag.Message') </script>
    }

</form>

-------mail method--
C#
public async Task SendEmail(ContactModel contact)
{
  
    try
    {


        var dateTime = Global.CurrentDate();

        var formattedTime = dateTime.ToString("h:mm tt", CultureInfo.InvariantCulture);

        var CurrentDateTime = $"{ DateTime.Now.ToString("dd-MM-yyyy")} {formattedTime}";

        MailMessage msg = new MailMessage();

        msg.From = new MailAddress("Student New Enquiry< noreply@domain.com > ");
        msg.To.Add("mymailid@gmail.com");
        msg.Subject = contact.Subject;

        string Mailmsg = "";

        Mailmsg += "This is the Student has a Enquiry for apply course." + Environment.NewLine + Environment.NewLine;
        Mailmsg += $"Date : { CurrentDateTime }" + Environment.NewLine + Environment.NewLine;
        Mailmsg += $"Name : {contact.Name}" + Environment.NewLine + Environment.NewLine;
        Mailmsg += $"Contact No : {contact.MobileNo}" + Environment.NewLine + Environment.NewLine;
        Mailmsg += $"Course Apply : {contact.CourseName}" + Environment.NewLine + Environment.NewLine;
        Mailmsg += $"Message : {contact.Message}" + Environment.NewLine + Environment.NewLine;
        Mailmsg += $"Page Location : {contact.PageLocation}" + Environment.NewLine + Environment.NewLine;

        msg.Body = Mailmsg;

        using (SmtpClient client = new SmtpClient())
        {

            client.EnableSsl = false;
            client.UseDefaultCredentials = true;
            client.Host = "relay-hosting.secureserver.net";
            client.Port = 25;
            await client.SendMailAsync(msg);

        }
    }
    catch (Exception ex)
    {
        throw;
    }

}

Below is my contact us page method i have called this from homeController or contactcontroller this is not wokring .

---contact us ---
C#
[HttpPost]
[Route("Contact")]
public async Task<IActionResult> Contact(ContactModel contact)
{
    try
    {
        contact.Subject = "Enquiry";
        contact.PageLocation = "Contact US";

        await emailHelper.SendEmail(contact);
        TempData["Message"] = "Email Has Been Sent Successfully.";
    }
    catch (Exception ex)
    {
        Global.SaveError(ex);
        TempData["Message"] = ex.ToString();

    }      
    return View("~/Views/Home/Contact.cshtml");
}

--view code of contact us page---
HTML
<form name="contactusForm" method="post" enctype="multipart/form-data" id="form_validation">

    <div class="row">
        <div class="col-lg-6 col-md-6 parsley-row">

            <select class="selectmenu" name="CourseName" value="" required>
                <option value="">--Select Course--</option>
                <option value="B.Sc (Nautical Science)">B.Sc (Nautical Science)</option>
                <option value="B.TECH IN PETROLEUM ENGINEERING">B.TECH IN PETROLEUM ENGINEERING</option>
                <option value="B.Tech Marine Engineering">B.Tech Marine Engineering</option>
                <option value="B.Tech Naval Architechture Ship Building Course">B.Tech Naval Architechture Ship Building Course</option>
                <option value="ETO">DNS</option>
                <option value="ETO">ETO</option>
                <option value="GP RATING">GP RATING</option>
                <option value="GRADUATE MARINE ENGINEERING">GRADUATE MARINE ENGINEERING</option>
            </select>

        </div>
        <div class="col-lg-6 col-md-6 parsley-row">
            <input type="text" placeholder="Your Name *" name="Name" data-parsley-pattern="^[a-zA-Z]+$+%" required>

        </div>
        <div class="col-lg-6 col-md-6 parsley-row">
            <input type="email" placeholder="Your Mail *" name="EmailID" data-parsley-trigger="change" required>

        </div>
        <div class="col-lg-6 col-md-6 parsley-row">
            <input type="text" placeholder="Phone Number *" name="MobileNo" maxlength="11" pattern="[0-9]{10,11}" required>
        </div>
        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 parsley-row">
            <textarea placeholder="Your Message" name="Message" required></textarea>
        </div>
    </div>

    <input type="hidden" name="Subject" value="Enquiry" />
    <input type="hidden" name="PageLocation" value="Contact US" />

    <button type="submit" value="Contact" asp-controller="Home" asp-action="Contact" class="color1_bg tran3s">
        Submit Now
    </button>

    @if (ViewBag.Message != null)
    {
        <script> window.alert( '@ViewBag.Message') </script>
    }

</form>
Posted
Updated 1-Sep-21 22:39pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900