You can send email from a .net application using System.Net.Mail.MailMessage namespace. It is very simple to send email using this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Be sure to include these namespaces using System.Net.Mail; using System.IO; ..... ..... MailMessage message = new MailMessage(); message.From = new MailAddress("someone@example.com"); message.To.Add(new MailAddress("someone@example.com")); message.Subject = "Simple email"; message.Body = "Hi, this is a email test"; message.IsBodyHtml = true; SmtpClient smtp = new SmtpClient("smtp.example.com"); smtp.Send(message); |
However you in case you can also include attachments with your email in case you need it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
// Be sure to include these namespaces using System.Net.Mail; using System.IO; ..... ..... MailMessage message = new MailMessage(); message.From = new MailAddress("someone@example.com"); message.To.Add(new MailAddress("someone@example.com")); message.Subject = "Email with attchment"; message.Body = "Hi, this is a attachment test"; message.IsBodyHtml = true; string filePath = @"C:\samplefolder\send.zip"; using (FileStream reader = new FileStream(filePath, FileMode.Open)) { byte[] data = new byte[reader.Length]; reader.Read(data, 0, (int)reader.Length); using (MemoryStream ms = new MemoryStream(data)) { message.Attachments.Add( new Attachment(ms, "send.zip")); SmtpClient smtp = new SmtpClient("smtp.example.com"); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Send(message); } } |