By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,272 Members | 1,442 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,272 IT Pros & Developers. It's quick & easy.

Help with email form and mulitple attachments

P: n/a
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 = "su********@romancap.com";
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>
Feb 17 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
--
Misbah Arefin

"budyerr" wrote:
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 = "su********@romancap.com";
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>
Feb 18 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.