How to send email attachments in C#.
.NET provides an easy to use programming framework for developing applications. There are built in classes to do just about anything including email. Emailing with attachments isn’t as hard as you might think. You can email from the local host or an SMTP server with the <strong>SmtpClient</strong> class with just a few lines of code.<!–more–>
To send email attachments in C# you’re going to add some extra namespaces to your project, and then implement the code.
First you will need to add the proper namespaces to your code. You’re going to need the <strong>System.Net</strong>,<strong>System.Net.Mail</strong>, and <strong>System.Net.Mime</strong> namespaces to send attachments using C#. Check out the rest of the code below for a simple example. Keep in mind that this is "short" code and will not compile as is, however this was taken from an application tool I developed.
SmtpClient client = new SmtpClient("mail.smtp.com"); MailAddress from = new MailAddress((string) emailAddress); MailAddress to = new MailAddress((string) emailAddress); MailMessage message = new MailMessage(from, to); message.Subject = (string) Subject; message.SubjectEncoding = System.Text.Encoding.UTF8; Attachment data= new Attachment((string) fileName, MediaTypeNames.Application.Octet); message.Attachments.Add(data); message.Body = (string) emailBody; message.BodyEncoding = System.Text.Encoding.UTF8;
