Created
April 5, 2017 19:37
-
-
Save nzpcmad/c575320099296ef3170616e98128ff77 to your computer and use it in GitHub Desktop.
C# code to send email
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
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("[email protected]", "[email protected]"); | |
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
http://nzpcmad.blogspot.co.nz/2017/04/c-sending-email.html