Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
C# code to send email
private void button1_Click(object sender, EventArgs e)
{
System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
try
{
mailMessage.From = new System.Net.Mail.MailAddress("me@help.com", "me@Sender.com");
mailMessage.To.Add("my-email");
mailMessage.Subject = "Hullo";
mailMessage.Body = "This is a test";
mailMessage.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient("my-smtp-server");
//Specifies how email messages are delivered. Here Email is sent through the network to an SMTP server.
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
// Need auth. ?
string loginName = "user";
string loginPassword = "password";
//string domain = "my-domain";
//System.Net.NetworkCredential networkCredential = new System.Net.NetworkCredential((loginName, loginPassword, domain);
System.Net.NetworkCredential networkCredential = new System.Net.NetworkCredential(loginName, loginPassword);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = networkCredential;
smtpClient.Send(mailMessage);
mailMessage.Dispose();
smtpClient = null;
}
catch (Exception ex)
{
throw ex;
}
finally
{
mailMessage.Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment