string path = @"C:\Users\johndoe\Documents\sample.pdf"; Attachment attachment = new Attachment(path); MailMessage message = new MailMessage(); message.Attachments.Add(attachment);
byte[] fileData = ReadFileDataFromDatabase(); Stream stream = new MemoryStream(fileData); Attachment attachment = new Attachment(stream, "myfile.pdf"); MailMessage message = new MailMessage(); message.Attachments.Add(attachment);This example demonstrates how to attach a file from a byte array to an email message. The byte array is first read from a database or a network stream, and then converted to a memory stream object. The memory stream object is then passed to the Attachment constructor, along with a filename, to create an Attachment object that is added to the Attachments collection of the MailMessage object. Package library: System.Net.Mail (part of the .NET framework)