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

Attachment Fails with Invalid Cast Exception

P: n/a

I have the following code that fails with an invalid cast exception:
....
System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();

msg.BodyFormat = MailFormat.Html;

msg.BodyEncoding = System.Text.Encoding.ASCII;

msg.Attachments.Add(@"c:\banner.jpg");
msg.Body = "<BR><H1>HELLO WORLD</H1>";

System.Web.Mail.SmtpMail.Send(msg);

The send is what causes the exception. If I don't add the attachment I
don't get the exception. Is this a common problem with ASP.NET? Am I doing
something wrong?

StackTrace " at System.Web.Mail.CdoSysHelper.Send(MailMessage
message)\r\n at System.Web.Mail.SmtpMail.Send(MailMessage message)\r\n
at Kpmg.Rts.BusinessLogic.EmailSender.SendEmails() in ...
Jan 17 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
> I have the following code that fails with an invalid cast exception:
...
msg.Attachments.Add(@"c:\banner.jpg");


The above line is at fault; the Attachments member is a typeless
collection, but is expected to contain MailAttachment objects, not
strings. You can construct one from a string, but not do an implicit
conversion. There's no compile-time error as the collection is a
typeless IList (so any kind of object can be added); in 2.0, a
specialised AttachmentCollection class is used.

So, to fix it, use
msg.Attachments.Add(new
System.Web.Mail.MailAttachment(@"c:\banner.jpg"));

Jan 17 '06 #2

P: n/a
Thanks Paul!

Now that the message is sending correctly, I see that the attachment is sent
as an attachment. Is there any way to encode the image into the message
without using an attachment?

I reference the image in the body of the message like this:

<IMG alt="blah" src="cid:banner.jpg">

It looks good, but the attachment is annoying.

"Paul Henderson" <pa***********@pittville.demon.co.uk> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
I have the following code that fails with an invalid cast exception:
...
msg.Attachments.Add(@"c:\banner.jpg");


The above line is at fault; the Attachments member is a typeless
collection, but is expected to contain MailAttachment objects, not
strings. You can construct one from a string, but not do an implicit
conversion. There's no compile-time error as the collection is a
typeless IList (so any kind of object can be added); in 2.0, a
specialised AttachmentCollection class is used.

So, to fix it, use
msg.Attachments.Add(new
System.Web.Mail.MailAttachment(@"c:\banner.jpg"));

Jan 17 '06 #3

P: n/a
> Is there any way to encode the image into the message
without using an attachment?


With .NET 1.1, which I guess you're using, there does not seem to be.
Version 2.0 adds this functionality in the System.Net.Mail (rather than
System.Web.Mail) classes, using the 'LinkedResource' class which lets
you attach chunks of data to different views of a multipart message.
There is no direct equivalent in .NET 1.1, and I do not know of a way
of manipulating the message content directly to add the required
fields. You would probably have to do your own mail class, that would
let you modify the content directly (set up a multipart/related chunk
and add a ContentID header to the image) and send it through SMTP (this
would not be trivial, but is perfectly possible if you have the
time...). Alternatively, upgrade to .NET 2.0, if that's an option for
you. It's also possible that someone else has written something that
does this, but I don't know of any

Jan 17 '06 #4

P: n/a
Hi Reticulated,

I think Paul's suggestion in reasonable. The System.Net.Mail namespace in
.net 2.0 provide powerful support for sending html rich mail with embeded
resources:

#Linked Resource
http://www.systemnetmail.com/faq/2.6.aspx

Also, if you're limited to .net 1.1., you can consider interop the CDO sys
component which can help created multipart html content, see the below
articles:

#Convert HTML to MHTML using ASP.NET
http://www.codeproject.com/aspnet/aspnethtml2mht.asp

#Build a C# Multipart MIME Encoding Library to Save Web Pages in "MHT"
Formathttp://www.eggheadcafe.com/articles/20040527.asp

Hope also helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| From: "Paul Henderson" <pa***********@pittville.demon.co.uk>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Subject: Re: Attachment Fails with Invalid Cast Exception
| Date: 17 Jan 2006 11:35:11 -0800
| Organization: http://groups.google.com
| Lines: 17
| Message-ID: <11**********************@g47g2000cwa.googlegroups .com>
| References: <up**************@TK2MSFTNGP10.phx.gbl>
| <11**********************@g49g2000cwa.googlegroups .com>
| <#n**************@TK2MSFTNGP12.phx.gbl>
| NNTP-Posting-Host: 80.176.138.59
| Mime-Version: 1.0
| Content-Type: text/plain; charset="iso-8859-1"
| X-Trace: posting.google.com 1137526516 20206 127.0.0.1 (17 Jan 2006
19:35:16 GMT)
| X-Complaints-To: gr**********@google.com
| NNTP-Posting-Date: Tue, 17 Jan 2006 19:35:16 +0000 (UTC)
| In-Reply-To: <#n**************@TK2MSFTNGP12.phx.gbl>
| User-Agent: G2/0.2
| X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB;
rv:1.7.12) Gecko/20050919 Firefox/1.0.7,gzip(gfe),gzip(gfe)
| Complaints-To: gr**********@google.com
| Injection-Info: g47g2000cwa.googlegroups.com; posting-host=80.176.138.59;
| posting-account=DT8-8g0AAADIbuXu9UnoJLlayTLfmGC6
| Path:
TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfee d00.sul.t-online.de!t-onli
ne.de!border2.nntp.dca.giganews.com!border1.nntp.d ca.giganews.com!nntp.gigan
ews.com!postnews.google.com!g47g2000cwa.googlegrou ps.com!not-for-mail
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:371507
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| > Is there any way to encode the image into the message
| > without using an attachment?
|
| With .NET 1.1, which I guess you're using, there does not seem to be.
| Version 2.0 adds this functionality in the System.Net.Mail (rather than
| System.Web.Mail) classes, using the 'LinkedResource' class which lets
| you attach chunks of data to different views of a multipart message.
| There is no direct equivalent in .NET 1.1, and I do not know of a way
| of manipulating the message content directly to add the required
| fields. You would probably have to do your own mail class, that would
| let you modify the content directly (set up a multipart/related chunk
| and add a ContentID header to the image) and send it through SMTP (this
| would not be trivial, but is perfectly possible if you have the
| time...). Alternatively, upgrade to .NET 2.0, if that's an option for
| you. It's also possible that someone else has written something that
| does this, but I don't know of any
|
|

Jan 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.