Thursday, March 17, 2011

Send Multiple attachment in Mail

  public static void SendAttachmentMail(string MailBody, string EmailTo, string MailFrom, string Subject, string[] listpdf)
    {
        try
        {
            MailMessage sendMail = new MailMessage();
            SmtpClient smtp = new SmtpClient();
            NetworkCredential SmtpUser = new NetworkCredential();
            sendMail.To.Add(EmailTo);
            sendMail.From = new MailAddress(MailFrom);
            sendMail.Subject = Subject;
            sendMail.Body = MailBody;
            sendMail.IsBodyHtml = true;
          
            int cnt = listpdf.Count();
            for (int l = 0; l < cnt; l++)
            {
                Attachment at_l = new Attachment(listpdf[l]);
                sendMail.Attachments.Add(at_l);
            }
            smtp.EnableSsl = true;
            smtp.Send(sendMail);
            sendMail.To.Clear();
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message.ToString());
            HttpContext.Current.Response.End();
        }
    }