How to send html template in mail in c

To send an HTML template in an email using C#, you can use the System.Net.Mail namespace and the MailMessage class. Here's an example:

First, create a new MailMessage object:

using System.Net.Mail;

MailMessage mail = new MailMessage();

Then, set the Subject and Body properties of the MailMessage object:

mail.Subject = "Test Email";
mail.Body = "<html><body><h1>Hello World!</h1></body></html>";

In this example, the Body property is set to an HTML string that contains an <h1> heading with the text "Hello World!".

To send the email, you'll need to create a SmtpClient object and set its Host and Port properties:

SmtpClient client = new SmtpClient();
client.Host = "your-smtp-server.com";
client.Port = 587; // or 25, depending on your SMTP server

Then, use the Send method of the SmtpClient object to send the email:

client.Send(mail);

Here's the complete code:

using System.Net.Mail;

class SendHtmlEmail
{
    static void Main(string[] args)
    {
        MailMessage mail = new MailMessage();
        mail.Subject = "Test Email";
        mail.Body = "<html><body><h1>Hello World!</h1></body></html>";

        SmtpClient client = new SmtpClient();
        client.Host = "your-smtp-server.com";
        client.Port = 587; // or 25, depending on your SMTP server

        client.Send(mail);
    }
}

Note that you'll need to replace your-smtp-server.com with the hostname of your SMTP server.

Alternatively, you can use a library like MailKit or MimeKit to send HTML emails. These libraries provide more advanced features and better support for HTML emails.

Here's an example using MailKit:

using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;

class SendHtmlEmail
{
    static void Main(string[] args)
    {
        var message = new MimeMessage();
        message.Subject = "Test Email";
        message.Body = new TextPart("html")
        {
            Text = "<html><body><h1>Hello World!</h1></body></html>"
        };

        using var client = new SmtpClient();
        client.Connect("your-smtp-server.com", 587, SecureSocketOptions.StartTls);
        client.Authenticate("your-email-address", "your-password");
        client.Send(message);
        client.Disconnect(true);
    }
}

This code creates a MimeMessage object and sets its Subject and Body properties. The Body property is set to a TextPart object with the Content-Type set to text/html. The Text property is set to the HTML string.

The SmtpClient object is used to connect to the SMTP server, authenticate, and send the email.