Thursday, 23 July 2026

How to Send Email Using SMTP in ASP.NET MVC 5 (C#) on Windows Hosting with Plesk Webmail

 


Sending emails is an essential feature of almost every web application, whether it's for user registration, password recovery, contact forms, order confirmations, or notifications. In this tutorial, you'll learn how to send emails from an ASP.NET MVC 5 (C#) application using SMTP with an email account created in Plesk Webmail on Windows Hosting. We'll cover the SMTP configuration, C# implementation, common errors, and troubleshooting tips to help you integrate reliable email functionality into your MVC application.

To keep this tutorial simple, I have created a basic form with a Send Email button. When you click the button, an email is sent using the SMTP settings configured in the application.

You can customize the email content by modifying the body variable in the C# code. The email body supports HTML, allowing you to include headings, paragraphs, tables, images, buttons, and other HTML elements to create professional-looking emails.

<form method="post" action="/yourcontroller/mailtest">
     <input type="submit" value="Send mail" class="g-btn-primary" />
 </form>

 

[HttpPost]
public ActionResult mailtest()
{
    string to, subject, body,ErrMsg="";
    to = "rajkumar9795@gmail.com";
    subject = " test mail at " + DateTime.Now.ToLongTimeString();
    body="<h1>Test Mail at " + DateTime.Now.ToLongTimeString()  +"</h1>";
    if (SendEmail(to, subject, body,ref ErrMsg))
    {
        TempData["Success"] = "Email sent successfully.";
    }
    else
    {
        TempData["Success"] = "Error ! " + ErrMsg;
    }
    return RedirectToAction("Index");
}

public bool SendEmail(string to, string subject, string body, ref string ErrMsg)
{
    try
    {
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("noreply@85inches.com", "85inches.com");
        mail.To.Add(to);
        mail.Subject = subject;
        mail.Body = body;
        mail.IsBodyHtml = true;
       SmtpClient smtp = new SmtpClient();
        smtp.Host = "webmail.85inches.com";      // SMTP Server
        smtp.Port = 25;                        // 587(TLS) or 465(SSL)
        smtp.EnableSsl = false;
        smtp.Credentials = new NetworkCredential(
            "noreply@85inches.com",
            "yourpassword");
        smtp.Send(mail);
        return true;
    }
    catch (Exception ex)
    {
        ErrMsg = ex.Message;
        if (ex.InnerException != null)
            ErrMsg += " | Inner: " + ex.InnerException.Message;
        return false;
    }
}

If the email is not sending, log in to your Webmail account and navigate to Settings → Advanced (or Advanced Options). There you can find the correct SMTP configuration, including the SMTP server, port number, encryption method (SSL/TLS), and authentication details. Use these settings in your application to ensure the SMTP connection is configured correctly     

Next Topic

No comments:

Post a Comment