RE: Help with email form and mulitple attachments



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 = "submission@xxxxxxxxxxxx";
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>

.



Relevant Pages

  • Re: Help with email form and mulitple attachments
    ... // uploaded file delete after sending email ... On Feb 17, 5:36 pm, Misbah Arefin ... string strfilename1 = Path.GetFileName; ... attach to email then delete the file after sending. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Problem with file attach in OWA
    ... so you are saying that neither Vista or XP computers can add attachments when sending email in OWA? ... owa always works Ok till I use some PCs with Vista. ... file, after pressing the attach button, the window remains in blank and do ...
    (microsoft.public.windows.server.sbs)