email multiple files attachments

N

nabil m

hi i have 5 checkboxes i would like to when the user click on 1 or multiple
checkbox i would like to email 1 or multiple files attachments to them
ex: mailMsg.Attachments.Add(myAttachment+i);
but getting error: Specified cast is not valid.

thank you in advance

my code below:
MailAttachment myAttachment0 = new MailAttachment
(this.Server.MapPath("../serverForms/Auto_Form_01.pdf"),
MailEncoding.Base64);
MailAttachment myAttachment1 = new MailAttachment
(this.Server.MapPath("../serverForms/Homeowners_Form_01.pdf",
MailEncoding.Base64);
MailAttachment myAttachment3 = new MailAttachment
(this.Server.MapPath("../serverForms/Condo_Coop_Form_01.pdf",
MailEncoding.Base64);
MailAttachment myAttachment4 = new MailAttachment
(this.Server.MapPath("../serverForms/Renters_Form_01.pdf",
MailEncoding.Base64);

for (int i=0;i<this.CheckBoxListForms.Items.Count;i++)
{
if (this.CheckBoxListForms.Items.Selected)
{

// would like to attach the appropriate checkbox file in my checkbox
list
// GETTING ERROR - Exception Details: System.InvalidCastException:
Specified cast is not valid.
mailMsg.Attachments.Add(myAttachment+i);

}

}
 
W

William F. Robertson, Jr.

You are trying to add a number and a MailAttachment. There is not cast for
it.

If you want to keep your for loop code and use the i (counter) you will need
to put all the MailAttachments in a variable.

MailAttachment [] attachments = new MailAttachment[5];
attachments[0] = new MainAttachment(
this.Server.MapPath("../serverForms/Auto_Form_01.pdf"),
MailEncoding.Base64);
....
add the rest of them.

In your for loop change this code:
mailMsg.Attachments.Add(myAttachment+i);
to:
mailMsg.Attachments.Add( attachments );

You can not add a integer and a MailAttachment together and get a new
variable name. If that sentence doesn't make any sense, it is because it
doesn't make any sense and that is why you received a Specified cast is not
valid error.

HTH,

bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top