Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple C# send email code snippet
public static bool SendEmail(string mailTo, string subject, string message) {
string mailFrom = ConfigurationManager.AppSettings.Get("MailAddress");
string username = ConfigurationManager.AppSettings.Get("MailUsername");
string password = ConfigurationManager.AppSettings.Get("MailPassword");
int port = Convert.ToInt32(ConfigurationManager.AppSettings.Get("MailOutgoingPort"));
string mailServer = ConfigurationManager.AppSettings.Get("MailServer");
try
{
MailMessage mailMessage = new MailMessage(mailFrom, mailTo, subject, message);
mailMessage.BodyEncoding = Encoding.UTF8;
mailMessage.SubjectEncoding = Encoding.UTF8;
SmtpClient smtpClient = new SmtpClient(mailServer, port);
smtpClient.EnableSsl = true;
smtpClient.Credentials = new NetworkCredential(username, password);
smtpClient.Send(mailMessage);
return true;
}catch
{
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.