Skip to content

Instantly share code, notes, and snippets.

@nzpcmad
Created April 5, 2017 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nzpcmad/c575320099296ef3170616e98128ff77 to your computer and use it in GitHub Desktop.
Save nzpcmad/c575320099296ef3170616e98128ff77 to your computer and use it in GitHub Desktop.
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();
}
}
@nzpcmad
Copy link
Author

nzpcmad commented Apr 5, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment