C sharp asp net send email template html

ASP.NET is a powerful programming language that allows developers to dynamic web applications. One common task in web development is sending email notifications to users. In this article, we will explore how to send email templates in HTML using ASP.NET.

To begin, let's take a look at the basic code structure for sending an email in ASP.NET:


using System;
using System.Net;
using System.Net.Mail;

namespace EmailSender
{
     class Program
    {
        static void Main(string[] args)
        {
            // Create a new MailMessage object
            MailMessage message = new MailMessage();

            // Set the sender's email address
            message.From = new MailAddress("sender@example.com");

            // Set the recipient's email address
            message.To.Add("recipient@example.com");

            // Set the subject of the email
            message.Subject = "Hello from ASP.NET";

            // Set the body of the email
            message.Body = "This is a test email sent from ASP.NET";

            // Create a new SmtpClient object
            SmtpClient smtpClient = new SmtpClient();

            // Set the SMTP server address and port number
            smtpClient.Host = "smtp.example.com";
            smtpClient.Port = 587;

            // Set the  for the SMTP server
            smtpClient.Credentials = new NetworkCredential("username", "");

            // Enable SSL encryption
            smtpClient.EnableSsl = true;

            // Send the email
            smtpClient.Send(message);
        }
    }
}

In the code above, we first import the necessary namespaces for sending . Then, we create a new `MailMessage` object and set the sender's email address, recipient's email address, subject, and body of the email.

Next, we create a new `SmtpClient` object and set the SMTP server address, port number, and credentials for authentication. We also enable SSL encryption for with the SMTP server.

Finally, we call the `Send` method of the `SmtpClient` object to send the email.

Using Email Templates in HTML

Sending plain text emails may not be sufficient. In many cases, we need to send emails with a more visually appealing layout using HTML templates. ASP.NET provides a convenient way to achieve this.

To send an email template in HTML, we can make use of the `AlternateViews` property of the `MailMessage` object. This property allows us to include multiple views of the email, such as plain text and HTML.

Let's take a look at an example:


// Create a new MailMessage object
MailMessage message = new MailMessage();

// Set the sender's email address
message.From = new MailAddress("sender@example.com");

// Set the recipient's email address
message.To.Add("recipient@example.com");

// Set the subject of the email
message.Subject = "Hello from ASP.NET";

// Create the HTML body of the email
string htmlBody = "

Welcome to our website!

This is a test email sent from ASP.NET.

"; // Create an alternate view for the HTML body AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, "text/html"); // Add the alternate view to the MailMessage object message.AlternateViews.Add(htmlView); // ... continue with the rest of the code to send the email

In the code above, we create a new `MailMessage` object and set the sender's email address, recipient's email address, and subject of the email as before.

Next, we create the HTML body of the email using a string variable. In this example, we have a simple HTML template with a heading and a paragraph.

We then create an `AlternateView` object using the `CreateAlternateViewFromString` method, passing in the HTML body, a null `ContentType`, and the MIME type of “text/html”. This creates an alternate view of the email in HTML format.

Finally, we add the alternate view to the `AlternateViews` property of the `MailMessage` object.

By including both a plain text view and an HTML view in the `AlternateViews` property, the email client can choose the view based on the capabilities of the recipient's email client.

Conclusion

In this article, we have explored how to send email templates in HTML using ASP.NET. By leveraging the `AlternateViews` property of the `MailMessage` object, we can include both plain text and HTML views in our emails, allowing for more visually appealing and dynamic content.

Sending email templates in HTML is a powerful feature of ASP.NET that can greatly enhance the user and engagement of your web applications.

Rate this post

Leave a Reply

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

Table of Contents