A simple C# method to send emails in HTML format
ASP.NET and C# provides programmers functions to send email messages. With the addition of a few instructions you can send messages in HTML format.
Code:
using System;
using System.Configuration;
using System.Net.Mail;
public partial class mail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
// ASP.NET provides programmers functions to send email messages.
// With the addition of a few instructions you can send messages in HTML format.
private bool SendEmail(string to, string subject, string body)
{
// Read the values from the configuration file
// Or setting it manually, e.g. smtp.domain.com and name@domain.com
string host = ConfigurationManager.AppSettings["SmtpClient"];
string from = ConfigurationManager.AppSettings["MessageFrom"];
// Declare a MailMessage object
MailMessage message = new MailMessage(from, to, subject, body);
// Sets a value indicating whether the mail message body is in Html
message.IsBodyHtml = true;
// Declare an SMTP client to send mail using the Simple Mail Transfer Protocol.
SmtpClient client = new SmtpClient(host);
try
{
client.Send(message);
return true;
}
catch (Exception excp)
{
return false;
}
}
// Other methods ...
}