3
Answers

Help required please: Document Upload and Email Verification

Robert

Robert

16y
3.5k
1
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 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 ...


default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="_mailUpload" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<link rel="stylesheet" type="text/css" href="../App_Themes/default.css" />

    <title>Document Upload and Email Verification</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="text-align:center; color:Blue; font-size:x-large"> Document Upload and Email Form Sender </div><br /><br />
   
    <div>
       
        <table align="center" width="80%" border="0" cellpadding="0" cellspacing="0">
            <tr valign="top">
                <td>
                <table align=center>
                    <tr>
                        <td style="height: 94px">
                            <asp:FileUpload ID="FileUpload1" CssClass="textSmall" runat="server" Width="250px" />
                            <p>
                            <asp:FileUpload ID="FileUpload2" CssClass="textSmall" runat="server" Width="250px" />
                            <p>
                            <asp:FileUpload ID="FileUpload3" CssClass="textSmall" runat="server" Width="250px" />
                            </td></tr></table>
                </td>
                <td>
                    <table align=center>
                        <tr>
                            <td class="textSmall">&nbsp;Your Name:</td>
                            <td><asp:textbox id="txtName" CssClass="textSmall" Width="241" Runat="server"></asp:textbox></td>
                        </tr>
                        <tr>
                            <td class="textSmall">&nbsp;Your Email Address:</td>
                            <td><asp:textbox id="txtEmail" CssClass="textSmall" Width="241" Runat="server"></asp:textbox></td>
                        </tr>
                        <tr>
                            <td colSpan="2" class="textSmall" >&nbsp;Your Comments:</td>
                        </tr>
                        <tr>
                            <td align="center" colSpan="2" width=100%>
                            <asp:textbox id="txtMessage" Width="100%" Runat="server" Height="99" TextMode="MultiLine" MaxLength="400"></asp:textbox></td>
                        </tr>
                        <tr>
                            <td colSpan="2">&nbsp;</td>
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td style="text-align: right">
                   
                            <asp:Button ID="Button1" CssClass="textSmall" runat="server" Text="Send Now" OnClick="Button1_Click" />
                            &nbsp;
                            <asp:Button id="Button2" CssClass="textSmall" Runat="server" Text="Reset" OnClick="Button2_Click" />
                        </tr>
                        <tr>
                            <td colSpan="2">&nbsp;</td>
                        </tr>
                        <tr>
                            <td colSpan="2">
                            <asp:label id="lblStatus" Runat="server" EnableViewState="False"></asp:label></td>
                        </tr>
                        <tr>
                            <td colspan="2">&nbsp;</td>
                        </tr>
                        <tr>
                            <td colspan="2">
                           
                            <asp:Label ID="Label1" CssClass="textSmall" runat="server"></asp:Label>&nbsp;&nbsp;

                            </td>
                        </tr>
                        </table>
                            </td>
            </tr>
            </table>
       
    </div>
   
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
        ErrorMessage="You must enter your name." ControlToValidate="txtName"></asp:RequiredFieldValidator><br />
       
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
        ControlToValidate="txtEmail" ErrorMessage="You must enter your email address"></asp:RequiredFieldValidator>
    <br />
   
       
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
        ControlToValidate="txtEmail" ErrorMessage="Please correct the format of your email to: example &quot;[email protected]&quot;."
        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
   
    </form>
</body>
</html>


default.aspx.cs

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
   

}

Answers (3)