Help with email form and mulitple attachments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • budyerr

    Help with email form and mulitple attachments

    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(frmemai l) {
    apos=frmemail.t xtFrom.value.in dexOf("@")
    dotpos=frmemail .txtFrom.value. lastIndexOf("." )
    if (frmemail.txtFr om.value == "" || apos<1 || dotpos-apos<2)
    {
    alert("Please enter valid email address")
    frmemail.txtFro m.focus()
    return false;
    }

    if(frmemail.txt File1.value == "") {
    alert("Please attach a file");
    frmemail.txtFil e1.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@rom ancap.com";
    mail.Subject = "Web Point File Submission";
    mail.Body = txtMsg.Text;
    mail.BodyFormat = MailFormat.Html ;

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

    string strfilename1 = Path.GetFileNam e(txtFile1.Post edFile.FileName );
    txtFile1.Posted File.SaveAs(str dir+strfilename 1);
    mail.Attachment s.Add(new MailAttachment( strdir+strfilen ame1));

    string strfilename2 = Path.GetFileNam e(txtFile2.Post edFile.FileName );
    txtFile2.Posted File.SaveAs(str dir+strfilename 2);
    mail.Attachment s.Add(new MailAttachment( strdir+strfilen ame2));

    string strfilename3 = Path.GetFileNam e(txtFile3.Post edFile.FileName );
    txtFile3.Posted File.SaveAs(str dir+strfilename 3);
    mail.Attachment s.Add(new MailAttachment( strdir+strfilen ame3));

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

    File.Delete(str dir+strfilename 1);
    File.Delete(str dir+strfilename 2);
    File.Delete(str dir+strfilename 3);
    }
    Response.Redire ct("submit-ok.html");
    }

    </script>
  • =?Utf-8?B?TWlzYmFoIEFyZWZpbg==?=

    #2
    RE: Help with email form and mulitple attachments

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

    if(txtFile1.Pos tedFile != null)
    {
    string strfilename1 = Path.GetFileNam e(txtFile1.Post edFile.FileName );
    txtFile1.Posted File.SaveAs(str dir+strfilename 1);
    mail.Attachment s.Add(new MailAttachment( strdir+strfilen ame1));
    }

    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(frmemai l) {
    apos=frmemail.t xtFrom.value.in dexOf("@")
    dotpos=frmemail .txtFrom.value. lastIndexOf("." )
    if (frmemail.txtFr om.value == "" || apos<1 || dotpos-apos<2)
    {
    alert("Please enter valid email address")
    frmemail.txtFro m.focus()
    return false;
    }
    >
    if(frmemail.txt File1.value == "") {
    alert("Please attach a file");
    frmemail.txtFil e1.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@rom ancap.com";
    mail.Subject = "Web Point File Submission";
    mail.Body = txtMsg.Text;
    mail.BodyFormat = MailFormat.Html ;
    >
    string strdir = Server.MapPath( "/upload/");
    >
    string strfilename1 = Path.GetFileNam e(txtFile1.Post edFile.FileName );
    txtFile1.Posted File.SaveAs(str dir+strfilename 1);
    mail.Attachment s.Add(new MailAttachment( strdir+strfilen ame1));
    >
    string strfilename2 = Path.GetFileNam e(txtFile2.Post edFile.FileName );
    txtFile2.Posted File.SaveAs(str dir+strfilename 2);
    mail.Attachment s.Add(new MailAttachment( strdir+strfilen ame2));
    >
    string strfilename3 = Path.GetFileNam e(txtFile3.Post edFile.FileName );
    txtFile3.Posted File.SaveAs(str dir+strfilename 3);
    mail.Attachment s.Add(new MailAttachment( strdir+strfilen ame3));
    >
    try
    {
    SmtpMail.Send(m ail);
    }
    catch(Exception ex)
    {
    Response.Redire ct("submit-failed.html");
    }
    finally
    {
    // uploaded file delete after sending email
    >
    File.Delete(str dir+strfilename 1);
    File.Delete(str dir+strfilename 2);
    File.Delete(str dir+strfilename 3);
    }
    Response.Redire ct("submit-ok.html");
    }
    >
    </script>
    >

    Comment

    Working...