473,504 Members | 13,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(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 = "su********@romancap.com";
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>
Feb 17 '08 #1
1 3850
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 = "su********@romancap.com";
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>
Feb 18 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3721
by: Paul Schmidt | last post by:
Dear list: I am new to python, and I am trying to figure out the short answer on something. I want to open a POP3 mailbox, read the enclosed mail using the POP3 module, , and then process it...
3
3130
by: LutherRevisited | last post by:
Is there a way I can put a message together without having to download any attachments there may be at the same time. I'm not having any problems dealing with attachments, but the way I'm doing...
15
3681
by: tabonni | last post by:
Hi I try to grab the checked files from HTML page and then send those PDF files as attachments. It can just send email, there are no PDF files attached. Can anybody point out my error? My...
6
1820
by: chris_fieldhouse | last post by:
Hi, I have a script for processing emails, The script finds email sent to a particular alias, grabs the body text of the email and stores it into a database. Problem is that certain character...
14
2127
by: bcap | last post by:
Hello, I really would apprciate help! =) What I want to do is be able to change the status of mulitple records using a drop down and a checkbox. I have a drop down called "ChangeStatus"...
1
6818
by: maxxxxel | last post by:
Hi Can anyone help me with some asp code , I changed the code to use CDO.message instead of the old cdont.sys to send mail from a ASP webpage which works fine. Our problem is that when we send...
4
6703
by: kpfunf | last post by:
When I email a report via macro or vba code, the attached report is named "Fuel Quote Report.(ext)". I have no idea how this name is used. I have three seperate macros sending three seperate reports....
3
1475
by: atlanteavila | last post by:
Hello all, I've got a database that is used to register couples preparing to get married in our Church to a class named Pre-Cana, we collect couples personal infomation along with email addresse,...
1
3189
by: deepaks85 | last post by:
Dear All, I want to send some data through a form with Multiple attachment in an HTML Format. I have tried it but it is not working for me. I am able to send data without attachment but with the...
0
7298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.