Attachment Fails with Invalid Cast Exception

R

Reticulated Ember

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 ...
 
P

Paul Henderson

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"));
 
R

Reticulated Ember

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.
 
P

Paul Henderson

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
 
S

Steven Cheng[MSFT]

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" <[email protected]>
| 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: <[email protected]>
| References: <uphu#[email protected]>
| <[email protected]>
| <#[email protected]>
| 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: (e-mail address removed)
| NNTP-Posting-Date: Tue, 17 Jan 2006 19:35:16 +0000 (UTC)
| In-Reply-To: <#[email protected]>
| 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: (e-mail address removed)
| Injection-Info: g47g2000cwa.googlegroups.com; posting-host=80.176.138.59;
| posting-account=DT8-8g0AAADIbuXu9UnoJLlayTLfmGC6
| Path:
TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onli
ne.de!border2.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.gigan
ews.com!postnews.google.com!g47g2000cwa.googlegroups.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
|
|
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top