By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,491 Members | 1,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,491 IT Pros & Developers. It's quick & easy.

email multiple files attachments

P: n/a
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[i].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);

}

}
Nov 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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[i] );

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
"nabil m" <na***@hagedorn.com> wrote in message
news:uG*************@TK2MSFTNGP10.phx.gbl...
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[i].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);

}

}

Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.