Ignora collegamentiHome / Blog

Blog


How to send an email with C #

In some applications it is necessary to send an email message. With the C # language this is very simple in the following example shows how to do this:

// IMPORTANT !!!
using System.Net.Mail;
...
...


///<summary>
///	This method retrieves the value of the SMTP client from the configuration file, or set the appropriate value.
///</summary>
///<returns>Return a SMTP Client (string)</returns>
private string GetSMTPClient()
{
	//string smtpClient = "smtp.yourmailprovider.com";
	string smtpClient = ConfigurationManager.AppSettings["SmtpClient"];
	return smtpClient; 
}

///<summary>
///	This method sends an email message to a recipient using the values ??passed as a parameter.
///</summary>
///<param name="messaggeFrom">Sender of the message </param>
///<param name="messaggeTo">Recipient of the message</param>
///<param name="messaggeSubject">Subject of the message</param>
///<param name="messaggeBody">Body of the message</param>
private void SendEmail(string messaggeFrom, string messaggeTo, string messaggeSubject, string messaggeBody)
{
	MailMessage message = new MailMessage(messaggeFrom, messaggeTo, messaggeSubject, messaggeBody);
	message.IsBodyHtml = true;
	SmtpClient client = new SmtpClient(GetSMTPClient());
	client.Send(message);               
}


Category: C#