473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Attachment Fails with Invalid Cast Exception


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
4 2273
> 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
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Randell D. | last post by:
Folks, I have a number of years with HP Unix and Linux but I've never dipped my toes in to Perl - I currently have SuSE 8.1 at home and am using webmin (www.webmin.com) to update Perl 5.8.0-45...
3
by: Rob Richardson | last post by:
Greetings! I am trying to make it possible for new derived classes of an object to be used by my application without rebuilding the application. The new classes will be made known to my...
0
by: Alan Z. Scharf | last post by:
Win Server 2003 VS.Net 2003 --------------- 1. I'm having the same problem below on all six of my pages with a datagrid item. 2. These pages all worked fine for months until problem started....
3
by: John Howard | last post by:
Making the following call to a local MSAccess database works fine: Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Dim intRows As Integer Dim strSQL As String Dim ds As New...
1
by: Hifni Shahzard | last post by:
Hi, I got a stored procedure, where it returns a value. But if I execute it. It gives an error as "Invalid cast from System.Int32 to System.Byte.". To make clear how do I execute this, below I'm...
6
by: cs_hart | last post by:
I am getting an invalid cast exception - cast from string to type double is not valid. Dim curName As String Dim prevName As String = "" curName = CStr(rows.Item(i).Item(colSchName)) ' extract...
7
by: Chris Thunell | last post by:
I'm trying to loop through an exchange public folder contact list, get some information out of each item, and then put it into a vb.net datatable. I run though the code and all works fine until i...
4
by: Abhi | last post by:
Hi All, I have a web service method which works fine when called from my local machine i.e. from localhost but when I publish the web services to a remote machine it throws an invalid cast soap...
8
by: =?Utf-8?B?cm95SGU=?= | last post by:
I have a web service. It creates a email and attached a file sitting one the network drive \\servername\filename. I tried UNC format and also try to mapping drive (eg. g:\text.txt or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.