How to send email using C# Razor Syntax (with Sample Code)

Step 1: Create the CSHTMLfile

Step 2: Create a form with similar to shown below.
sample contact form
Step 3:  Use IsPost() to check whever your webpage gets refreshed.

i.e if IsPost() is false, your page have not refreshed. So, no need to perform any action.

If IsPost() is true, your page is refreshed. You can get the values from form & send email.

 

Step 4: Use WebMail class to create email setup.

WebMail.SmtpServer = "smtp.yourdomain.com";
WebMail.SmtpPort = 587;
WebMail.UserName = "[email protected]";
WebMail.Password = "yourpass";
WebMail.From = "[email protected]";

 

Step 5: Your send() method of WebMail class to send email.

WebMail.Send(to: "[email protected]", subject: "some subject", body: message);

Parameters:

//to:
The email address of the recipient or recipients. Separate multiple recipients
using a semicolon (;).

// subject:
The subject line for the email message.

// body:
The body of the email message. If isBodyHtml is true, HTML in the body is interpreted as markup.

// from:
(Optional) The email address of the message sender, or null to not specify a sender. The default value is null.

// cc:
(Optional) The email addresses of additional recipients to send a copy of the message to, or null if there are no additional recipients. Separate multiple recipients using a semicolon (;). The default value is null.

// filesToAttach:
(Optional) A collection of file names that specifies the files to attach to the email message, or null if there are no files to attach. The default value is null.

// isBodyHtml:
(Optional) true to specify that the email message body is in HTML format; false to indicate that the body is in plain-text format. The default value is true.

// additionalHeaders:
(Optional) A collection of headers to add to the normal SMTP headers included in this email message, or null to send no additional headers. The default value is null.

// bcc:
(Optional) The email addresses of additional recipients to send a “blind” copy of the message to, or null if there are no additional recipients. Separate multiple recipients using a semicolon (;). The default value is null.

// contentEncoding:
(Optional) The encoding to use for the body of the message. Possible values are property values for the System.Text.Encoding class, such as System.Text.Encoding.UTF8.  The default value is null.

// headerEncoding:
(Optional) The encoding to use for the header of the message. Possible values are property values for the System.Text.Encoding class, such as System.Text.Encoding.UTF8.  The default value is null.

// priority:
(Optional) A value (“Normal”, “Low”, “High”) that specifies the priority of the message. The default is “Normal”.

// replyTo:
(Optional) The email address that will be used when the recipient replies to the message. The default value is null, which indicates that the reply address is the value of the From property.

Overall WebMail.Send()

public static void Send(string to, string subject, string body, string from = null, string cc = null, IEnumerable<string> filesToAttach = null, bool isBodyHtml = true, IEnumerable<string> additionalHeaders = null, string bcc = null, string contentEncoding = null, string headerEncoding = null, string priority = null, string replyTo = null);

Sample Program:

@{
    if (IsPost)
    {
        var name = Request.Form["name"];
        var email = Request.Form["email"];
        var message = Request.Form["message"];

        try
        {
            WebMail.SmtpServer = "smtp.yourdomain.com";
            WebMail.SmtpPort = 587;
            WebMail.UserName = "[email protected]";
            WebMail.Password = "yourpass";
            WebMail.From = "[email protected]";
            
            WebMail.EnableSsl = true;

            WebMail.Send(to: "[email protected]", subject: "some subject", body: message);
            Email sent Successfully
        } 
        catch (Exception ex) {

             Failure! Error @ex.Message.ToString();
        } 
    } 
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.