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>
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>