Categories

Tools

bitdefender

1&1 Web Hosting

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 ...
}
Posted in ASP.NET by MdmSoft

If our work has been of help, you can help us with a small donation...
Our programmers will thank you!



All information contained in this web site are the property of MdmSoft. The information is provided "as is", MdmSoft will not be liable for any misuse of the code contained in these pages, nor can it be for inaccuracies, grammatical errors or other factors that may have caused damage or lost earnings. MdmSoft is not responsible for the content of comments posted by users.
The examples in this area have the educational and demonstration purposes only, and may be copied only for your reference, but cannot be used for commercial purposes, or for any other purpose, without the express written consent of MdmSoft.
MdmSoft also reserves the right to change, without notice, to your liking this web site, the pages and its sections, and may suspend temporarily or definitely the various services included on this site.
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.