Robert

Robert

  • NA
  • 22
  • 25.9k

Help required please: Document Upload and Email Verification

Feb 19 2008 1:21 PM
Hi All

I am trying to allow one to three (3) files to be uploaded to a server, then send an email to the recipient for verification.

The files upload correctly and the email is sent, however it sends three seperate emails for the one upload process.
What do I need to change in the code so that I only send one email for the complete process?

Any help would be appreciated.

Regards
Robert Caya


Here is the code for the process ...


using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

public partial class _mailUpload : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)

    {
        string filepath = "d:\\Uploads";
        HttpFileCollection uploadedFiles = Request.Files;
        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();
        MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);

        for (int i = 0; i < uploadedFiles.Count; i++)
        {
            HttpPostedFile userPostedFile = uploadedFiles[i];

            try
            {
                if (userPostedFile.ContentLength > 0)
                {
                    Label1.Text += "<u>File #" + (i + 1) + "</u><br />";
                    Label1.Text += "File Name: " + userPostedFile.FileName + "<br />";
                    Label1.Text += "File Size: " + userPostedFile.ContentLength + "kb<p>";
                   
                    userPostedFile.SaveAs(filepath + "\\" +
                       System.IO.Path.GetFileName(userPostedFile.FileName));

                }

            // Default is localhost or you can specify a host name or ipaddress of the email server
            smtpClient.Host = "localhost";

            //Default port is 25
            smtpClient.Port = 25;

            //From address will be given as a MailAddress Object
            message.From = fromAddress;

            // To address collection of MailAddress
            message.To.Add("[email protected]");
            message.Subject = "Client File Upload System";

            // CC and BCC optional
            // MailAddressCollection class is used to send the email to various users
            // You can specify Address as new MailAddress("[email protected]")
            //message.CC.Add("[email protected]");
            //message.CC.Add("[email protected]");

            // You can specify Address directly as string
            //message.Bcc.Add(new MailAddress("[email protected]"));
            //message.Bcc.Add(new MailAddress("[email protected]"));

            //Body can be Html or text format
            //Specify true if it is html message
            message.IsBodyHtml = true;

            // Message body content
            message.Body = txtMessage.Text + "<br /><br />The following files have been uploaded to the server.<br /><br />" + Label1.Text;
        
            // Send SMTP mail
            smtpClient.Send(message);

            lblStatus.Text = "Your email has been successfully sent.<br /><br /> The following files have been uploaded to the server.";
            }
            catch (Exception Ex)
            {
                Label1.Text += "There was an error sending your files ... <br>" + Ex.Message;
                lblStatus.Text += "Your email failed to send correctly ...<br>" + Ex.Message;
            }
        }
    }
    #region "Reset"
    protected void Button2_Click(object sender, EventArgs e)
    {
        txtName.Text = "";
        txtEmail.Text = "";
        txtMessage.Text = "";
        Label1.Text = "";
    }
    #endregion
   

}