public static void SendEmail(string subject, string message,
string from, string to, bool isHtml)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(from);
mail.To.Add(to);
mail.Subject = subject;
mail.Body = message;
mail.IsBodyHtml = isHtml;
SmtpClient smtp = GetConfiguredSmtpClient();
smtp.Send(mail);
}
GetConfiguredSmtpClient below comes from the EPiServer sample templates.
public static SmtpClient GetConfiguredSmtpClient(string smtpServer,
int smtpPort, string userName, string password)
{
SmtpClient smtp = new SmtpClient();
// Setup the host and port
const int DefaultSmtpPort = 25;
if (smtpPort == -1)
smtpPort = (int)EPiServer.Global.EPConfig["EPnSmtpServerPort"];
if (smtpPort < 1 || smtpPort > 65535)
smtpPort = DefaultSmtpPort;
if (smtpPort != DefaultSmtpPort)
{
smtp.Port = smtpPort;
}
if (smtpServer == null)
smtpServer = (string)EPiServer.Global.EPConfig["EPsSmtpServer"];
if (smtpServer.Length > 0)
{
smtp.Host = smtpServer;
}
// Setup the smtp authentication
if (userName == null)
userName = (string)EPiServer.Global.EPConfig["EPsSmtpUser"];
if (password == null)
password = (string)EPiServer.Global.EPConfig["EPsSmtpPassword"];
// User/password may both be empty -- in that case just ignore everything...
if (userName.Length != 0 || password.Length != 0)
{
// If one has been defined, the other must also be defined
if (userName.Length == 0)
throw new EPiServerException("Undefined username");
if (password.Length == 0)
throw new EPiServerException("Undefined password");
smtp.Credentials = new NetworkCredential(userName, password);
}
return smtp;
}