473,545 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HttpPostedFile problem

Hi,

I am creating a function using C# to send emails with
attachment file. And I am using File Field control to get
file for attachment. Sending email works fine on
development machine, however when I deployed it on the
production machine, it doesn't work. It gives me an error
as below when I click send email button. And I tried to
debug it. Seems like File field control's
postedfile.file name is not working even I have
System.Web.UI.H tmlControls reference.

Did anyone ever have similar situation like this? Where
is the problem? Please help.

Thank you very much.

Huan

-----------------------------------------------------
Server Error in '/' Application.
----------------------------------------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the
execution of the current web request. Please review the
stack trace for more information about the error and
where it originated in the code.

Exception Details: System.NullRefe renceException: Object
reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution
of the current web request. Information regarding the
origin and location of the exception can be identified
using the exception stack trace below.

Stack Trace:
[NullReferenceEx ception: Object reference not set to an
instance of an object.]
JBA.jp.JBAUser. EmailToMembers. SendBtn_Click(O bject
sender, EventArgs e)
System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e)
+108

System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEv
entHandler.Rais ePostBackEvent( String eventArgument) +57
System.Web.UI.P age.RaisePostBa ckEvent
(IPostBackEvent Handler sourceControl, String
eventArgument) +18
System.Web.UI.P age.RaisePostBa ckEvent
(NameValueColle ction postData) +33
System.Web.UI.P age.ProcessRequ estMain() +1263


----------------------------------------------------------
Version Information: Microsoft .NET Framework
Version:1.0.370 5.288; ASP.NET Version:1.0.370 5.288
And Here is my code:

----------------------------
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
using System.Data.Sql Client;
using System.Configur ation;
using System.Web.Mail ;
using System.Text;
using System.IO;

private void SendBtn_Click(o bject sender,
System.EventArg s e)
{
string source;
string strSQL;
string fileTitle;
int cnt;

cnt=0;
//Send single mail
if (txtEmailAddr.T ext !=
null && txtEmailAddr.Te xt!="")
{
cnt=cnt+1;

//Send email
oMailer = new
System.Web.Mail .MailMessage();

oMailer.From =
txtSender.Text;
oMailer.Subject =
txtSubject.Text .Trim();
oMailer.Body =
txtBody.Text.Tr im();

//oMailer.BodyFor mat=
System.Web.Mail .MailFormat.Htm l;

oMailer.BodyEnc oding=Encoding. UTF8;

oMailer.To=txtE mailAddr.Text.T rim();

// Is there an
attachment?
m_eAttachment =
null;
if
(txtFile.Posted File.FileName != "")
{
string
sPath=@"c:\";
fileTitle
= txtFile.PostedF ile.FileName;
fileTitle
= fileTitle.Subst ring(fileTitle. LastIndexOf("\\ ") + 1);

m_eAttachment = sPath+fileTitle ;

txtFile.PostedF ile.SaveAs(m_eA ttachment);
}

Nov 18 '05 #1
3 3862
Hi,
I suggest to use cordbg to see the line that cause that error.

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #2
here is a snip of vb code i have used to send attachments maybe it will help
you. This code uses a local file but the concept should be the same for a
posted file using request.files(0 ) i would think or you may have to save it
to disk first. A couple of points, I believe that file attachments will not
work unless a mail server is specified for the application. As I recall,
the default server does not work for file attachments. Others may have
different observations.

dim mgMM as new Mail.MailMessag e()
mgMM.from = from
mgMM.to = mailto
mgMM.subject = subject
mgMM.body = sbbody.tostring ()

dim j as int32

for j = 0 to strFile.getuppe rbound(0)
dim mgAttach as new mail.mailattach ment(strPathBas e & strFile(j))
mgMM.attachment s.Add(mgAttach)

next j

smtpmail.smtpse rver = ConfigurationSe ttings.AppSetti ngs("smtpSvr")

SmtpMail.Send(m gMM)
"huan" <hu**@calsoftsy s.com> wrote in message
news:01******** *************** *****@phx.gbl.. .
Hi,

I am creating a function using C# to send emails with
attachment file. And I am using File Field control to get
file for attachment. Sending email works fine on
development machine, however when I deployed it on the
production machine, it doesn't work. It gives me an error
as below when I click send email button. And I tried to
debug it. Seems like File field control's
postedfile.file name is not working even I have
System.Web.UI.H tmlControls reference.

Did anyone ever have similar situation like this? Where
is the problem? Please help.

Thank you very much.

Huan

-----------------------------------------------------
Server Error in '/' Application.
----------------------------------------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the
execution of the current web request. Please review the
stack trace for more information about the error and
where it originated in the code.

Exception Details: System.NullRefe renceException: Object
reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution
of the current web request. Information regarding the
origin and location of the exception can be identified
using the exception stack trace below.

Stack Trace:
[NullReferenceEx ception: Object reference not set to an
instance of an object.]
JBA.jp.JBAUser. EmailToMembers. SendBtn_Click(O bject
sender, EventArgs e)
System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e)
+108

System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEv
entHandler.Rais ePostBackEvent( String eventArgument) +57
System.Web.UI.P age.RaisePostBa ckEvent
(IPostBackEvent Handler sourceControl, String
eventArgument) +18
System.Web.UI.P age.RaisePostBa ckEvent
(NameValueColle ction postData) +33
System.Web.UI.P age.ProcessRequ estMain() +1263


----------------------------------------------------------
Version Information: Microsoft .NET Framework
Version:1.0.370 5.288; ASP.NET Version:1.0.370 5.288
And Here is my code:

----------------------------
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
using System.Data.Sql Client;
using System.Configur ation;
using System.Web.Mail ;
using System.Text;
using System.IO;

private void SendBtn_Click(o bject sender,
System.EventArg s e)
{
string source;
string strSQL;
string fileTitle;
int cnt;

cnt=0;
//Send single mail
if (txtEmailAddr.T ext !=
null && txtEmailAddr.Te xt!="")
{
cnt=cnt+1;

//Send email
oMailer = new
System.Web.Mail .MailMessage();

oMailer.From =
txtSender.Text;
oMailer.Subject =
txtSubject.Text .Trim();
oMailer.Body =
txtBody.Text.Tr im();

//oMailer.BodyFor mat=
System.Web.Mail .MailFormat.Htm l;

oMailer.BodyEnc oding=Encoding. UTF8;

oMailer.To=txtE mailAddr.Text.T rim();

// Is there an
attachment?
m_eAttachment =
null;
if
(txtFile.Posted File.FileName != "")
{
string
sPath=@"c:\";
fileTitle
= txtFile.PostedF ile.FileName;
fileTitle
= fileTitle.Subst ring(fileTitle. LastIndexOf("\\ ") + 1);

m_eAttachment = sPath+fileTitle ;

txtFile.PostedF ile.SaveAs(m_eA ttachment);
}

Nov 18 '05 #3
This link may help. It says you must save the file first before sending an
attachment.
http://www.systemwebmail.com/default.aspx
"huan" <hu**@calsoftsy s.com> wrote in message
news:01******** *************** *****@phx.gbl.. .
Hi,

I am creating a function using C# to send emails with
attachment file. And I am using File Field control to get
file for attachment. Sending email works fine on
development machine, however when I deployed it on the
production machine, it doesn't work. It gives me an error
as below when I click send email button. And I tried to
debug it. Seems like File field control's
postedfile.file name is not working even I have
System.Web.UI.H tmlControls reference.

Did anyone ever have similar situation like this? Where
is the problem? Please help.

Thank you very much.

Huan

-----------------------------------------------------
Server Error in '/' Application.
----------------------------------------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the
execution of the current web request. Please review the
stack trace for more information about the error and
where it originated in the code.

Exception Details: System.NullRefe renceException: Object
reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution
of the current web request. Information regarding the
origin and location of the exception can be identified
using the exception stack trace below.

Stack Trace:
[NullReferenceEx ception: Object reference not set to an
instance of an object.]
JBA.jp.JBAUser. EmailToMembers. SendBtn_Click(O bject
sender, EventArgs e)
System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e)
+108

System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEv
entHandler.Rais ePostBackEvent( String eventArgument) +57
System.Web.UI.P age.RaisePostBa ckEvent
(IPostBackEvent Handler sourceControl, String
eventArgument) +18
System.Web.UI.P age.RaisePostBa ckEvent
(NameValueColle ction postData) +33
System.Web.UI.P age.ProcessRequ estMain() +1263


----------------------------------------------------------
Version Information: Microsoft .NET Framework
Version:1.0.370 5.288; ASP.NET Version:1.0.370 5.288
And Here is my code:

----------------------------
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
using System.Data.Sql Client;
using System.Configur ation;
using System.Web.Mail ;
using System.Text;
using System.IO;

private void SendBtn_Click(o bject sender,
System.EventArg s e)
{
string source;
string strSQL;
string fileTitle;
int cnt;

cnt=0;
//Send single mail
if (txtEmailAddr.T ext !=
null && txtEmailAddr.Te xt!="")
{
cnt=cnt+1;

//Send email
oMailer = new
System.Web.Mail .MailMessage();

oMailer.From =
txtSender.Text;
oMailer.Subject =
txtSubject.Text .Trim();
oMailer.Body =
txtBody.Text.Tr im();

//oMailer.BodyFor mat=
System.Web.Mail .MailFormat.Htm l;

oMailer.BodyEnc oding=Encoding. UTF8;

oMailer.To=txtE mailAddr.Text.T rim();

// Is there an
attachment?
m_eAttachment =
null;
if
(txtFile.Posted File.FileName != "")
{
string
sPath=@"c:\";
fileTitle
= txtFile.PostedF ile.FileName;
fileTitle
= fileTitle.Subst ring(fileTitle. LastIndexOf("\\ ") + 1);

m_eAttachment = sPath+fileTitle ;

txtFile.PostedF ile.SaveAs(m_eA ttachment);
}

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3400
by: Jeff | last post by:
I am using the HttpPostedFile to allow users to upload files to a server farm. The SaveAs() saves to a shared network path (\\SERVER\SHARE) so that no matter which web server is taking the request, all uploaded files go to the same location. The .SaveAs() call throws the error "unknown user name or bad password". The file share referenced...
4
2915
by: | last post by:
Hi! It seems the HttpPostedFile object can't save a file to a destination withing the web's structure. I want to upload pictures from a page and store them in a subfolder to the web folder (necessare to be able to show them on other pages) I can store the pics anywhere else on the server except into any directory within the web.
1
5104
by: terrorix | last post by:
I want to save uploaded file to disk. I have this construction: HttpPostedFile myFile = ((HttpRequest)Request).Files; if (myFile != null) { string fn = "c:\\Inetpub\\wwwroot\\MyWeb\\upload\\File_1"; try { myFile.SaveAs(fn); lblMsg.Text = "File was uploaded and saved successfully!";
8
8887
by: Al Smith | last post by:
Hi, I am able to use the HttpPostedFile.SaveAs() method to upload and save a file to the local web machine. However, I would really like to save the file to a VirtualDirectory. Does anyone know if this can be done and the syntax to do it? Thanks
2
7221
by: NATO24 | last post by:
Hello, I am trying to write a sub routine that I can pass a posted (image) file to. That routine will save the original file, then create a thumbnail and save it. When I try to create the image with the posted file, I get an error saying file type HttpPostedFile cannot be converted to an Image. When I save the original and try and use the...
5
4477
by: MSDN | last post by:
Does anyone know why I am getting Invalid cast exception??? For Each oFile As System.web.HttpPostedFile In Request.Files ....... etc..... Next I Checked that
0
2530
by: Rob Maui | last post by:
I use the following code in an aspx file to read the file name which I am uploading from the client PC onto the server; Dim MyFileName As String Dim MyFileColl As HttpFileCollection = Request.Files MyFileName = MyFileColl.Item(0).FileName or (alos tried) Dim PostFile As System.Web.HttpPostedFile
1
2519
by: Ralph Watermann | last post by:
Hi, we upload an Excel-file via a FileUpload-Control into an HttpPostedFile object. Now we want to access the contained data directly without saving it to a temp file. Is this possible? So far we have to to a HttpPostedFile.SaveAs(strFileName) and use a connection string to access the data via an OleDbDataAdapter: strConn = _
1
2689
by: mcarrera00 | last post by:
Hi, I have a problem with uploading files to a webserver. Here it's the code Dim strPath As String = MapPath(fileName).ToLower strPath = strPath.Replace("\usercntrls", "\uploads") fileUpload.PostedFile.SaveAs(strPath)
4
36522
by: macap.usenet | last post by:
Hello, I´ve a strange problem with my HttpPostedFile object. I coded a File Upload where the user can upload a zip file. My code looks like this: HttpFileCollection files = Page.Request.Files; HttpPostedFile file = files;
0
7475
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7409
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7664
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7918
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7766
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1897
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1022
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
715
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.