public static bool SendMail(string To_Mail, string Mail_Title, string Message_Body, string Mail_Model, bool Is_Html)
{
// smail represents our email message
// it has properties like From, To, Subject, Body, and so on.
System.Net.Mail.MailMessage smail = new System.Net.Mail.MailMessage();
// specifying if our email must be sent in html or text format
smail.IsBodyHtml = Is_Html;
// email Body Encoding
smail.BodyEncoding = System.Text.Encoding.GetEncoding("iso-8859-1");
// Now specify your email adress and your/company name
// you can even set a erroneous adress if you want to send spams for example.
smail.From = new System.Net.Mail.MailAddress("mail@xcess.info", "Sing It.");
// email adress of the receiver, we can add many receivers.
smail.To.Add(new System.Net.Mail.MailAddress(To_Mail));
// mail title/subject
smail.Subject = Mail_Title;
// we replace the newline with it's html equivalent in the message
Message_Body = Message_Body.Replace("\r\n", "<br />");
// then we place our message in the mail template by replacing
// the 'placemailhere' string by our message if it exists.
if (Mail_Model.ToLower().Contains("placemailhere"))
{
smail.Body = Mail_Model.Replace("placemailhere", Message_Body);
}
else
{
smail.Body = Message_Body;
}
// MailMessage instance to a specified SMTP server
System.Net.Mail.SmtpClient Client = new System.Net.Mail.SmtpClient();
// Setting up the Smtp server
Client.Host = System.Web.Configuration.WebConfigurationManager.AppSettings["smtphost"];
// Sending the mail
try
{
Client.Send(smail);
}
catch
{
return false;
}
return true;
} |