Created
June 1, 2012 10:04
-
-
Save gzuri/2850914 to your computer and use it in GitHub Desktop.
Simple C# send email code snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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