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" %>
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 += "File #" + (i + 1) + "
";
Label1.Text += "File Name: " + userPostedFile.FileName + "
";
Label1.Text += "File Size: " + userPostedFile.ContentLength + "kb
";
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 + "
The following files have been uploaded to the server.
" + Label1.Text;
// Send SMTP mail
smtpClient.Send(message);
lblStatus.Text = "Your email has been successfully sent.
The following files have been uploaded to the server.";
}
catch (Exception Ex)
{
Label1.Text += "There was an error sending your files ...
" + Ex.Message;
lblStatus.Text += "Your email failed to send correctly ...
" + Ex.Message;
}
}
}
#region "Reset"
protected void Button2_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtEmail.Text = "";
txtMessage.Text = "";
Label1.Text = "";
}
#endregion
}
RobertPosted Feb 20, 2008, 8:58 AM
I was able to solve the problem with your hint.
Robert
RobertPosted Feb 18, 2008, 11:33 AM
Your help is appreciated.
Thanks
Robert
PRASHANT KAMBLEPosted Feb 14, 2008, 6:12 PM
Hi Robert,
You need to close the for loop before e-mail notification code segment.
That would solve your problem.
Hope this helps.
Regards,