How to attach multiple files to the Email

5.7k Views Asked by At

How to attach multiple files to the send Email .

I use the following method to send Email .

   public static void sendMail(string to, string from, string password, string subject, string body, int dep_code)
        {
                MailMessage mail = new MailMessage();
                SmtpClient smtp = new SmtpClient();
                if (to == "")
                    to = "-------";
                MailAddressCollection m = new MailAddressCollection();
                m.Add(to);
                mail.Subject = subject;
                mail.From = new MailAddress(from);

                string banneredBody = @"<table width='100%' border='0' dir='rtl'>" +
                                          "<tr>" +
                                            "<td align='center'><img src=cid:Image1  /></td>" +
                                          "</tr>" +
                                          "<tr>" +
                                            "<td align='center'>" + body + "</td>" +
                                          "</tr>" +
                                       "</table>";

                mail.Body = banneredBody;
                //mail.Body = body;
                mail.IsBodyHtml = true;
                mail.ReplyTo = new MailAddress(from);
                mail.To.Add(m[0]);
                smtp.Host = "....";
                smtp.Port = 25;
                smtp.EnableSsl = false;
                smtp.Credentials = new System.Net.NetworkCredential(from, password);
                ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

                smtp.Send(mail);

        }

How to pass files parameter to attach them .

I use RadAsyncUpload :

 private List<Telerik.Web.UI.UploadedFileInfo> uploadedFiles = new List<Telerik.Web.UI.UploadedFileInfo>();
        public List<Telerik.Web.UI.UploadedFileInfo> UploadedFiles
        {
            get { return uploadedFiles; }
            set { uploadedFiles = value; }
        }

 private void PopulateUploadedFilesList()
        {
            foreach (UploadedFile file in rad_upload.UploadedFiles)
            {
                UploadedFileInfo uploadedFileInfo = new UploadedFileInfo(file);
                UploadedFiles.Add(uploadedFileInfo);
            }
        }
2

There are 2 best solutions below

0
Henk Mollema On BEST ANSWER

You should use the Attachments property:

foreach (var file in UploadedFiles)
{
    mail.Attachments.Add(file);
}

By the way, you can use MailAddress to specify an address you want to send the mail to:

mail.To.Add(new MailAddress(to));
0
Yahia On

MailMessage has a property called Attachments... you need to add all attachments you want to send to that property... some sample source code and explanation can be found on MSDN here and here.