kumar
kumar

Reputation: 31

send multiple attachements to mail

I am sending multiple attachements to single email. but my requirement is sendig files by using foreach loop. here i am sending to attachements by creating two attachementcontents and two filenames, this is not a perfect code. please help me. thank you in advance.

**Here it is send mail method:**

public static void SendMail(EmailDetails ObjEmailDtl){

try

{

SmtpClient client = new SmtpClient();         

client.DeliveryMethod = SmtpDeliveryMethod.Network;   

client.EnableSsl = false;    

   string hostName = string.Empty;

 using (eCoreDbEntities db = new eCoreDbEntities())

  {
  hostName = db.Parameters.Where(m => m.Name == "SMTP").FirstOrDefault().Value;
  }

 client.Host = hostName;

            client.Port = 25;

            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("[email protected]");
            if (!string.IsNullOrEmpty(ObjEmailDtl.ToMailId))
            {
                string[] arrCC = ObjEmailDtl.ToMailId.Trim().Split(',');
                foreach (var item in arrCC)
                {
                    msg.To.Add(new MailAddress(item));
                }
            }

            if (!string.IsNullOrEmpty(ObjEmailDtl.CCList))
            {
                string[] arrCC = ObjEmailDtl.CCList.Trim().Split(',');
                foreach (var item in arrCC)
                {
                    msg.CC.Add(new MailAddress(item));
                }
            }

            msg.Subject = ObjEmailDtl.Subject;
            msg.IsBodyHtml = true;
            msg.Body = ObjEmailDtl.Body;
            if (ObjEmailDtl.AttachmentContect != null)
            {                   
                Attachment att = new Attachment(new MemoryStream(ObjEmailDtl.AttachmentContect), ObjEmailDtl.AttachmentName);
                msg.Attachments.Add(att);       
            }
            if (ObjEmailDtl.AttachmentContect1 != null)
            {
                Attachment att = new Attachment(new MemoryStream(ObjEmailDtl.AttachmentContect1), ObjEmailDtl.AttachmentName1);
                msg.Attachments.Add(att);
            }
            client.Send(msg);
        }
        catch (Exception ex)
        {
            Common.WriteLog("Common-SendMail", "", ex.Message);
        }

    }

Here it is my model class i added multiple attachement content names and multiple file names

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Services.Models
{
public class CommonModel
{
    public class EmailDetails
    {
        public string ToMailId { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public string CCList { get; set; }
        public byte[] AttachmentContect { get; set; }
        public byte[] AttachmentContect1 { get; set; }
        public string AttachmentName { get; set; }
        public string AttachmentName1 { get; set; }
    }
}
}

THIS IS THE CODE I AM CREATING FILES

byte[] file1 = Encoding.ASCII.GetBytes(str1.ToString());
byte[] File = Encoding.ASCII.GetBytes(str.ToString());
EmailDetails objEmail = new EmailDetails();
var tomail = db.Parameters.ToList().Where(m => m.Name == "ToList" && 
m.Category == "DynamicReport").FirstOrDefault().Value;
                objEmail.ToMailId = tomail;
                objEmail.AttachmentContect = File;
                objEmail.AttachmentContect1 = file1;
                objEmail.Subject = "Katalister Dynamic Report";
                objEmail.Body = "&nbsp; Dear Leader,<br/><br/>   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Please find the attached Katalister Daily Report and Order Details Report.<br/><br/><br/><br/> Team Karvy";
                objEmail.CCList = db.Parameters.ToList().Where(m => m.Name == "CCList" && m.Category == "DynamicReport").FirstOrDefault().Value;
                objEmail.AttachmentName = "katalister_daily_report_" + DateTime.Now + ".xls";
                objEmail.AttachmentName1 = "OrderDetails_BD.xls";
                Common.SendMail(objEmail);

Upvotes: 0

Views: 144

Answers (1)

Chetan
Chetan

Reputation: 6911

You should create a class which represents the Attachment details as following.

public class AttachmentDetails
{
    public byte[] Content {get;set;}
    public string Name {get;set;}
}

And then use collection of this class objects in EmailDetails class.

public class EmailDetails
{
    public string ToMailId { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
    public string CCList { get; set; }
    public List<AttachmentDetails> Attachments {get;set;}
}

Then you should populate the attachments as following.

byte[] file1 = Encoding.ASCII.GetBytes(str1.ToString());
byte[] File = Encoding.ASCII.GetBytes(str.ToString());
EmailDetails objEmail = new EmailDetails();
var tomail = db.Parameters.ToList().Where(m => m.Name == "ToList" && 
m.Category == "DynamicReport").FirstOrDefault().Value;
objEmail.ToMailId = tomail;
objEmail.Attachments = new List<AttachmentDetails>();
var attachment = new AttachmentDetails();
attachment.Name = "katalister_daily_report_" + DateTime.Now + ".xls";
attachment.Content = File;
objEmail.Attachments.Add(attachment);

attachment = new AttachmentDetails();
attachment.Name = "OrderDetails_BD.xls";
attachment.Content = file1;
objEmail.Attachments.Add(attachment);

objEmail.Subject = "Katalister Dynamic Report";
objEmail.Body = "&nbsp; Dear Leader,<br/><br/>   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Please find the attached Katalister Daily Report and Order Details Report.<br/><br/><br/><br/> Team Karvy";
objEmail.CCList = db.Parameters.ToList().Where(m => m.Name == "CCList" && m.Category == "DynamicReport").FirstOrDefault().Value;

And then loop thru Attachment property in SendMail method as following.

//Skipping the other code before this in the method.
msg.Subject = ObjEmailDtl.Subject;
        msg.IsBodyHtml = true;
        msg.Body = ObjEmailDtl.Body;
foreach(var attachment in ObjEmailDtl.Attachments)
{
    Attachment att = new Attachment(new MemoryStream(attachment.Content), attachment.Name);
            msg.Attachments.Add(att);      
}
// Other code in the method.

This should help you resolve your issue.

Upvotes: 1

Related Questions