SMTP
"How to send an email with C#/ASP/Razor?" This is a question I have seen asked alot. So here is the quick and Dirty example. Since most likely you will want to pass certain variables to the call I have setup and example.
c#/ASP using System.Net.Mail
Razor @using System.Net.Mail
The Message Body can be HTML formatted to make your alerts look a little more readable IE:
string msgBody = @"<h2><font color = red></font>Danger Will Robinson DANGER</h2><br></br><h4>The value entered for <b><strong>A Server</strong></b></h4><h4>By: " + RandomName + " on " + DateTime.Now + "was wrong!</h4><br></br>";
string MailSUB = "Mail Subject";
SendMessage(msgBody, MailTO, MailSUB);
SendMessage(msgBody, MailTO, MailSUB);
private void SendMessage(string body,string MailTO,string sub) { try { string mgr="user@domain.local"; MailMessage mail = new MailMessage(); mail.IsBodyHtml =true; mail.From = new MailAddress("italerts@domain.local"); mail.To.Add(MailTO); mail.CC.Add(mgr);
mail.Subject = subject; mail.Body = body;
SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.Port = 25; smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Send(mail);
} catch (Exception) {
throw; } } This will send to local email server with no AUTH good for local SMTP servers. If you need to authenticate add smtp.Port = 587, smtp.Credentials = new NetworkCredential("email", "password"), smtp.EnableSsl = true
mail.Subject = subject; mail.Body = body;
SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.Port = 25; smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Send(mail);
} catch (Exception) {
throw; } } This will send to local email server with no AUTH good for local SMTP servers. If you need to authenticate add smtp.Port = 587, smtp.Credentials = new NetworkCredential("email", "password"), smtp.EnableSsl = true