2

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);
            }
        }
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392

2 Answers2

3

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));
Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
1

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.

Yahia
  • 69,653
  • 9
  • 115
  • 144