Help with email form and mulitple attachments

B

budyerr

All, I am trying to build a email submission form using asp.net. I
currently have a web form page that will upload to my webhosting
server, attach to email then delete the file after sending. This
works great with one attachment. I am requiring that a file be attach
before submitting. Now I am trying to add the ability to add multiple
attachments and I am able to create this however, it will error out if
all of the attachment is not selected. I don't know how to add to my
code to say that if attachment 2 and 3 are not selected, then ignore
it and send attachment 1. Hope that makes sense. Here is the code I
have that does not work if you don't attach all 3 attachments.
Ideally I would have liked to add attachments using a listbox but I
can't find a successful example on the web. Thanks in advance.


<script language = "javascript">

function Tocheck(frmemail) {
apos=frmemail.txtFrom.value.indexOf("@")
dotpos=frmemail.txtFrom.value.lastIndexOf(".")
if (frmemail.txtFrom.value == "" || apos<1 || dotpos-apos<2)
{
alert("Please enter valid email address")
frmemail.txtFrom.focus()
return false;
}

if(frmemail.txtFile1.value == "") {
alert("Please attach a file");
frmemail.txtFile1.focus();
return(false);
}

}
</script>
<script runat="server">

void btnSubmit_Click(Object sender, EventArgs e) {

MailMessage mail = new MailMessage();
mail.From = txtFrom.Text;
mail.To = "(e-mail address removed)";
mail.Subject = "Web Point File Submission";
mail.Body = txtMsg.Text;
mail.BodyFormat = MailFormat.Html;

string strdir = Server.MapPath("/upload/");

string strfilename1 = Path.GetFileName(txtFile1.PostedFile.FileName);
txtFile1.PostedFile.SaveAs(strdir+strfilename1);
mail.Attachments.Add(new MailAttachment(strdir+strfilename1));

string strfilename2 = Path.GetFileName(txtFile2.PostedFile.FileName);
txtFile2.PostedFile.SaveAs(strdir+strfilename2);
mail.Attachments.Add(new MailAttachment(strdir+strfilename2));

string strfilename3 = Path.GetFileName(txtFile3.PostedFile.FileName);
txtFile3.PostedFile.SaveAs(strdir+strfilename3);
mail.Attachments.Add(new MailAttachment(strdir+strfilename3));

try
{
SmtpMail.Send(mail);
}
catch(Exception ex)
{
Response.Redirect("submit-failed.html");
}
finally
{
// uploaded file delete after sending email

File.Delete(strdir+strfilename1);
File.Delete(strdir+strfilename2);
File.Delete(strdir+strfilename3);
}
Response.Redirect("submit-ok.html");
}

</script>
 
M

Misbah Arefin

you need to first check which control had actual posted files
e.g

if(txtFile1.PostedFile != null)
{
string strfilename1 = Path.GetFileName(txtFile1.PostedFile.FileName);
txtFile1.PostedFile.SaveAs(strdir+strfilename1);
mail.Attachments.Add(new MailAttachment(strdir+strfilename1));
}

additionally you could also check the .ContentLength of the posted file
 
B

budyerr

Misbah, thanks for the reply. I added it to my code and got an error.
Unfortunately I am new to this and not sure where I'm making the
mistake. Can you please assist with directing me exactly what changes
I need to make to my code. Thanks
 
M

Misbah Arefin

try

void btnSubmit_Click(Object sender, EventArgs e)
{
MailMessage mail = new MailMessage();
mail.From = txtFrom.Text;
mail.To = "(e-mail address removed)";
mail.Subject = "Web Point File Submission";
mail.Body = txtMsg.Text;
mail.BodyFormat = MailFormat.Html;

string strdir = Server.MapPath("/upload/");

string strfilename1;
string strfilename2;
string strfilename3;

if(txtFile1.PostedFile != null && txtFile1.PostedFile.ContentLength != 0)
{
strfilename1 = Path.GetFileName(txtFile1.PostedFile.FileName);
txtFile1.PostedFile.SaveAs(strdir+strfilename1);
mail.Attachments.Add(new MailAttachment(strdir+strfilename1));
}

if(txtFile2.PostedFile != null && txtFile2.PostedFile.ContentLength != 0)
{
strfilename2 = Path.GetFileName(txtFile2.PostedFile.FileName);
txtFile2.PostedFile.SaveAs(strdir+strfilename2);
mail.Attachments.Add(new MailAttachment(strdir+strfilename2));
}

if(txtFile3.PostedFile != null && txtFile3.PostedFile.ContentLength != 0)
{
strfilename3 = Path.GetFileName(txtFile3.PostedFile.FileName);
txtFile3.PostedFile.SaveAs(strdir+strfilename3);
mail.Attachments.Add(new MailAttachment(strdir+strfilename3));
}

try
{
SmtpMail.Send(mail);
}
catch(Exception ex)
{
Response.Redirect("submit-failed.html");
}
finally
{
// uploaded file delete after sending email
if(strfilename1.Length != 0)
File.Delete(strdir+strfilename1);
if(strfilename2.Length != 0)
File.Delete(strdir+strfilename2);
if(strfilename3.Length != 0)
File.Delete(strdir+strfilename3);
}
Response.Redirect("submit-ok.html");
}

the above should work but can you please post the error message you are
getting... i cant relly help without knowing what the exact error was
 
B

budyerr

try

void btnSubmit_Click(Object sender, EventArgs e)
{
  MailMessage mail = new MailMessage();
  mail.From = txtFrom.Text;
  mail.To = "(e-mail address removed)";
  mail.Subject = "Web Point File Submission";
  mail.Body = txtMsg.Text;
  mail.BodyFormat = MailFormat.Html;

  string strdir = Server.MapPath("/upload/");

  string strfilename1;
  string strfilename2;
  string strfilename3;

  if(txtFile1.PostedFile != null && txtFile1.PostedFile.ContentLength != 0)
  {
    strfilename1 = Path.GetFileName(txtFile1.PostedFile.FileName);
    txtFile1.PostedFile.SaveAs(strdir+strfilename1);
    mail.Attachments.Add(new MailAttachment(strdir+strfilename1));
  }

  if(txtFile2.PostedFile != null && txtFile2.PostedFile.ContentLength != 0)
  {
    strfilename2 = Path.GetFileName(txtFile2.PostedFile.FileName);
    txtFile2.PostedFile.SaveAs(strdir+strfilename2);
    mail.Attachments.Add(new MailAttachment(strdir+strfilename2));
  }

  if(txtFile3.PostedFile != null && txtFile3.PostedFile.ContentLength != 0)
  {
    strfilename3 = Path.GetFileName(txtFile3.PostedFile.FileName);
    txtFile3.PostedFile.SaveAs(strdir+strfilename3);
    mail.Attachments.Add(new MailAttachment(strdir+strfilename3));
  }

  try
  {
    SmtpMail.Send(mail);
  }
  catch(Exception ex)
  {
    Response.Redirect("submit-failed.html");
  }
  finally
  {
    // uploaded file delete after sending email
    if(strfilename1.Length != 0)
      File.Delete(strdir+strfilename1);
    if(strfilename2.Length != 0)
      File.Delete(strdir+strfilename2);
    if(strfilename3.Length != 0)
      File.Delete(strdir+strfilename3);
  }
  Response.Redirect("submit-ok.html");

}

the above should work but can you please post the error message you are
getting... i cant relly help without knowing what the exact error was

--
Misbah Arefin





- Show quoted text -

Misbah, here is the error I got using your code.. It is currently
hosted on godaddy so I dont know how to view the detail errors.

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

Runtime Error
Description: An application error occurred on the server. The current
custom error settings for this application prevent the details of the
application error from being viewed remotely (for security reasons).
It could, however, be viewed by browsers running on the local server
machine.

Details: To enable the details of this specific error message to be
viewable on remote machines, please create a <customErrors> tag within
a "web.config" configuration file located in the root directory of the
current web application. This <customErrors> tag should then have its
"mode" attribute set to "Off".


<!-- Web.Config Configuration File -->

<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>


Notes: The current error page you are seeing can be replaced by a
custom error page by modifying the "defaultRedirect" attribute of the
application's <customErrors> configuration tag to point to a custom
error page URL.


<!-- Web.Config Configuration File -->

<configuration>
<system.web>
<customErrors mode="RemoteOnly"
defaultRedirect="mycustompage.htm"/>
</system.web>
</configuration>
 
M

Misbah Arefin

change your web.config and set <customErrors mode="Off"
then run your code and it will print the exact error message
revert the changes to customErrors in your web.config once you get the
exception message... the error/exception will show you a stack trace and will
list the exact line of code causing the problem
 
B

budyerr

change your web.config and set <customErrors mode="Off"
then run your code and it will print the exact error message
revert the changes to customErrors in your web.config once you get the
exception message... the error/exception will show you a stack trace and will
list the exact line of code causing the problem

--
Misbah Arefin














- Show quoted text -

Thanks, for the instructions.

Here is the error that it gave me.

Compilation Error
Description: An error occurred during the compilation of a resource
required to service this request. Please review the following specific
error details and modify your source code appropriately.

Compiler Error Message: CS0165: Use of unassigned local variable
'strfilename1'

Source Error:



Line 79: {
Line 80: // uploaded file delete after sending email
Line 81: if(strfilename1.Length != 0)
Line 82: File.Delete(strdir+strfilename1);
Line 83: if(strfilename2.Length != 0)


Source File: d:\hosting\inceptadmin\submit5.aspx Line: 81
 
M

Misbah Arefin

budyerr said:
Thanks, for the instructions.

Here is the error that it gave me.

Compilation Error
Description: An error occurred during the compilation of a resource
required to service this request. Please review the following specific
error details and modify your source code appropriately.

Compiler Error Message: CS0165: Use of unassigned local variable
'strfilename1'

Source Error:



Line 79: {
Line 80: // uploaded file delete after sending email
Line 81: if(strfilename1.Length != 0)
Line 82: File.Delete(strdir+strfilename1);
Line 83: if(strfilename2.Length != 0)


Source File: d:\hosting\inceptadmin\submit5.aspx Line: 81

try changing the lines

string strfilename1;
string strfilename2;
string strfilename3;

to

string strfilename1 = "";
string strfilename2 = "";
string strfilename3 = "";
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top