How to Send Email in ASP.NET C#
Sending email from a web page

Last updated on 14 Dec 2016 | Posted on 07 Dec 2016

In order to send email in ASP.NET you need to use the MailMessage class located in the System.Net.Mail namespace.

MailMessage oMailMessage = new MailMessage();

The using statement should look like this:

using System.Net.Mail;

Below is an example of passing the MailMessage object some constructor values:

MailMessage oMailMessage = new MailMessage(
  "from@example.com",
  "to@example.com"
);

Or you could set all of the vital properties of your new object in one statement such as:

MailMessage oMailMessage = new MailMessage(
  "from@example.com",
  "to@example.com",
  "Subject text",
  "Body text"
);

Next, you need to create an SmtpClient object.

SmtpClient oClient = new SmtpClient();

You can, if necessary set the hostname via SmtpClient's constructor:

SmtpClient oClient = new SmtpClient("hostname");

Finally, you can then send the email via the Send method like so:

client.Send(oMailMessage);

UPDATE: Sample code snippet

using System;
using System.Net.Mail;

Here is a sample snippet method to send an email in C#:

public static bool Send(string fromAddress, string toAddress, string subject, string bodyText)
{
    MailMessage oMailMessage = new MailMessage(fromAddress, toAddress);
    oMailMessage.Subject = subject;
    oMailMessage.Body = bodyText;
    oMailMessage.IsBodyHtml = true;
    oMailMessage.Priority = MailPriority.Normal;

SmtpClient oClient = new SmtpClient();
try { oClient.Send(oMailMessage); // Do something else return true; } catch (Exception) { return false; } }
String or string in C#

String or string in C#

Posted on 04 Feb 2016

Some of you may have been or still are confused about your use of String or string in C#. If you haven't spotted already, one has a capital 'C' and th…

Read more