Sending mail problem in Asp.net core MVC with Godaddy shared Hosting

Prashant Sharma 66 Reputation points
2021-09-02T05:56:09.67+00:00

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.

Home controller:

 [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 HomeContact is working if I call it from the home Page but for testing if I call this method from another page, it is not working, this was working only once but now not working.

Home page contact us view code:  

128515-home.png

Mail method:

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 working .

Contact us:

 [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 of contact us page:
128555-contact.png

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,400 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes