HttpPostedFile problem

H

huan

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.filename is not working even I have
System.Web.UI.HtmlControls 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.NullReferenceException: 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:


[NullReferenceException: Object reference not set to an
instance of an object.]
JBA.jp.JBAUser.EmailToMembers.SendBtn_Click(Object
sender, EventArgs e)
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
+108

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEv
entHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent
(IPostBackEventHandler sourceControl, String
eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent
(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1263




----------------------------------------------------------
Version Information: Microsoft .NET Framework
Version:1.0.3705.288; ASP.NET Version:1.0.3705.288


And Here is my code:

----------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Mail;
using System.Text;
using System.IO;

private void SendBtn_Click(object sender,
System.EventArgs e)
{
string source;
string strSQL;
string fileTitle;
int cnt;

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

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

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

//oMailer.BodyFormat=
System.Web.Mail.MailFormat.Html;

oMailer.BodyEncoding=Encoding.UTF8;

oMailer.To=txtEmailAddr.Text.Trim();

// Is there an
attachment?
m_eAttachment =
null;
if
(txtFile.PostedFile.FileName != "")
{
string
sPath=@"c:\";
fileTitle
= txtFile.PostedFile.FileName;
fileTitle
= fileTitle.Substring(fileTitle.LastIndexOf("\\") + 1);

m_eAttachment = sPath+fileTitle;

txtFile.PostedFile.SaveAs(m_eAttachment);
}
 
V

vMike

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.MailMessage()
mgMM.from = from
mgMM.to = mailto
mgMM.subject = subject
mgMM.body = sbbody.tostring()

dim j as int32

for j = 0 to strFile.getupperbound(0)
dim mgAttach as new mail.mailattachment(strPathBase & strFile(j))
mgMM.attachments.Add(mgAttach)

next j

smtpmail.smtpserver = ConfigurationSettings.AppSettings("smtpSvr")

SmtpMail.Send(mgMM)


huan said:
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.filename is not working even I have
System.Web.UI.HtmlControls 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.NullReferenceException: 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:


[NullReferenceException: Object reference not set to an
instance of an object.]
JBA.jp.JBAUser.EmailToMembers.SendBtn_Click(Object
sender, EventArgs e)
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
+108

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEv
entHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent
(IPostBackEventHandler sourceControl, String
eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent
(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1263




----------------------------------------------------------
Version Information: Microsoft .NET Framework
Version:1.0.3705.288; ASP.NET Version:1.0.3705.288


And Here is my code:

----------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Mail;
using System.Text;
using System.IO;

private void SendBtn_Click(object sender,
System.EventArgs e)
{
string source;
string strSQL;
string fileTitle;
int cnt;

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

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

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

//oMailer.BodyFormat=
System.Web.Mail.MailFormat.Html;

oMailer.BodyEncoding=Encoding.UTF8;

oMailer.To=txtEmailAddr.Text.Trim();

// Is there an
attachment?
m_eAttachment =
null;
if
(txtFile.PostedFile.FileName != "")
{
string
sPath=@"c:\";
fileTitle
= txtFile.PostedFile.FileName;
fileTitle
= fileTitle.Substring(fileTitle.LastIndexOf("\\") + 1);

m_eAttachment = sPath+fileTitle;

txtFile.PostedFile.SaveAs(m_eAttachment);
}
 
V

vMike

This link may help. It says you must save the file first before sending an
attachment.
http://www.systemwebmail.com/default.aspx
huan said:
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.filename is not working even I have
System.Web.UI.HtmlControls 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.NullReferenceException: 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:


[NullReferenceException: Object reference not set to an
instance of an object.]
JBA.jp.JBAUser.EmailToMembers.SendBtn_Click(Object
sender, EventArgs e)
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
+108

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEv
entHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent
(IPostBackEventHandler sourceControl, String
eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent
(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1263




----------------------------------------------------------
Version Information: Microsoft .NET Framework
Version:1.0.3705.288; ASP.NET Version:1.0.3705.288


And Here is my code:

----------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Mail;
using System.Text;
using System.IO;

private void SendBtn_Click(object sender,
System.EventArgs e)
{
string source;
string strSQL;
string fileTitle;
int cnt;

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

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

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

//oMailer.BodyFormat=
System.Web.Mail.MailFormat.Html;

oMailer.BodyEncoding=Encoding.UTF8;

oMailer.To=txtEmailAddr.Text.Trim();

// Is there an
attachment?
m_eAttachment =
null;
if
(txtFile.PostedFile.FileName != "")
{
string
sPath=@"c:\";
fileTitle
= txtFile.PostedFile.FileName;
fileTitle
= fileTitle.Substring(fileTitle.LastIndexOf("\\") + 1);

m_eAttachment = sPath+fileTitle;

txtFile.PostedFile.SaveAs(m_eAttachment);
}
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top