Trying to create a script to mail a forms to an email address in asp or java (needs to run in IIS)

Hello all,
First let me just apologize for my lack of skill, I have just recently(Last week) really started trying to learn how to program.

Now to the goodies. I am trying to code a simple script that will mail a persons name, email address and some text to an email address.

This is for my terminal server, so when my remote users have issues they can launch an IE shortcut from within their session and report any issues they are having. It will be a single page with some possible
links to some documentation for the end user to maybe read before calling me when they cannot print (fat chance).

I am going to preferably host it on the Terminal Server its self in IIS. I have attach a script and code I got from a template out on the internet, but still cannot seem to get it to work.

Thanks in advance for the help!!
<%@ Page Language="VB" EnableSessionState="False"  
EnableViewState="False" Trace="False" Debug="False"%>
<%@ Import Namespace="System.Web.Mail" %>  
<script language="VB" runat=server>  
 
Sub Page_Load(Sender as Object, E as EventArgs)  
   If Page.IsPostBack Then
       lblResponse.Text = "Your problem has been reported."
   End If
End Sub  
   
Sub btn_Click(sender as Object, e as System.EventArgs)
 If  Request.Form("Email") <> "" Then
   Dim objMail As New MailMessage()
   objMail.From = Request.Form("Email")
   objMail.To = "itsupport@abc.com"
   objMail.Subject = Request.Form("Subject")
   objMail.Body = Request.Form("Message")
   objMail.BodyFormat = MailFormat.Text
   SmtpMail.SmtpServer = "myexchangeserver.local"
   SmtpMail.Send(objMail)
 Else
   lblResponse.Text = "Please enter an email address."
 End If
End Sub
 
</script>  
<html>
<head>
<style>
.main {font-family:Verdana; font-size:12px;}
.title {font-family:Verdana; font-size:18px; font-weight:bold;}
</style>
</head>
<body>
<span class="title" align="center">Use this for to report problems to the IT support team.</span>
 
<br><br><asp:Label class="main" id="lblResponse"  
runat="server"/>
 
<form method="POST" name="MainForm" runat="server">
<table>
 <tr>
   <td class="main" align="right">Email:</td>
   <td class="main"><input type="text"  
   class="main" name="Email" value=""></td>
 </tr>
 <tr>
   <td class="main" align="right">
   Subject:</td>
   <td class="main"><input type="text"  
   class="main" name="Subject" value=""></td>
 </tr>
 <tr>
   <td class="main" align="right"  
   valign="top">Problem:</td>
   <td class="main">&nbsp;</td>
 </tr>
 
 
 <tr>
   <td class="main"><input name="Submit" type="Submit"  
   id="Submit2" value="Send" onserverclick="btn_Click"  
   runat="server" /></td>
   <td class="main">&nbsp;</td>
 </tr>
</table>
<span class="main">
<textarea name="Message"  
   cols="50" rows="8"></textarea>
</span>
</form>
<span class="main"></span>
</body>
</html>
 
 
</body>
</html>

Open in new window

LVL 12
bhnmiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Smart_ManCommented:
if i am on the right track. i can advice the usage of front page to get your page done. maybe few pages too. i suppose you have the FP extentions on your IIS.

if that is ok , just reply with ok to go forward in that about how to get it done.-in a nice easy shape.

waiting for your reply.

0
bhnmiAuthor Commented:
What are the advantages of using Front Page extensions?
0
Smart_ManCommented:
it looked like you are searching easiest web page with very user-friendly for posting the errors.

there in FP you can have some nice look buttons to lunch thier email client for example so they just write some lines and hit send.

does that suggestion suits you?

waiting for yoru reply
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

bhnmiAuthor Commented:
They are in a terminal server environment and have no access to applications (email clients). The server side script needs to send the form information on the back end.  
0
bhnmiAuthor Commented:
Never mind I got it to work. I had some bad code in the header.

Now I want to make a script to send a pre-defined attachment to an email inputed by the user. I have a template to send an attachment chosen by the user, that I would like to modify to my purpose. Any help much appreciated.


<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Mail" %>
<%@ Import Namespace="System.IO" %>
 
 
<script language = "javascript">
	function Tocheck(frmemail)  {
		if(frmemail.txtFile.value  == "")  {
		alert("Please attach a file");
		frmemail.txtFile.focus();
		return(false);
		}
		}
</script>
 
<script runat="server">
 
void btnSubmit_Click(Object sender, EventArgs e) {
 
MailMessage mail = new MailMessage();
mail.From = txtFrom.Text;
mail.To = txtTo.Text;
mail.Subject = txtSubject.Text;
mail.Body = txtMsg.Text;
mail.BodyFormat = MailFormat.Html;
 
string strdir = "D:\\temp\\";
 
string strfilename = Path.GetFileName(txtFile.PostedFile.FileName);
 
txtFile.PostedFile.SaveAs(strdir+strfilename);
 
mail.Attachments.Add(new MailAttachment(strdir+strfilename));
 
try
{ 
SmtpMail.Send(mail); 
} 
catch(Exception ex)
{
Response.Write("<b>Exception Occured:</b>   " +ex);
}
finally
{
Response.Write("Your E-mail has been sent sucessfully");
}
 
// Uploaded file deleted after sending e-mail
 
File.Delete(strdir+strfilename);
}
 
</script>    
 
 
 
 
<html>
 
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Using ASP.NET To Send Email - Part 2</title>
</head>
 
<body>
 
<div align="center">
<form method = "post" name = "frmemail" runat = "server" enctype="multipart/form-data" onSubmit = "return Tocheck(this)">
	<table border="1" width="637" height="196">
		<tr>
			<td height="196" width="637" valign="top">
			
			From : <asp:TextBox ID = "txtFrom" Runat =server></asp:TextBox>
			<p>To: <asp:TextBox ID = "txtTo" Runat = server></asp:TextBox>
			<p>Subject:<asp:TextBox ID = "txtSubject" Runat = server></asp:TextBox>
 
			<p>Message:<asp:TextBox runat="server" Height="57px" TextMode="MultiLine" Width="212px" ID="txtMsg"></asp:TextBox>
 
 
			<p>Attach:<input type = "file" id = "txtFile" runat = "server">
 
 
			<p align="center"><asp:Button Runat = server ID = "btnSubmit" Text = "SEND" OnClick = "btnSubmit_Click"></asp:Button></td>
		</tr>
	</table>
	</form>
</div>
 
</body>
 
</html>

Open in new window

0
Smart_ManCommented:
replace

string strdir = "D:\\temp\\";
 
string strfilename = Path.GetFileName(txtFile.PostedFile.FileName);
 
txtFile.PostedFile.SaveAs(strdir+strfilename);
 
mail.Attachments.Add(new MailAttachment(strdir+strfilename));


with

mail.Attachments.Add(strFilepath);

where stFilePath = "the file path"

remove

File.Delete(strdir+strfilename);

remove

<script language = "javascript">
        function Tocheck(frmemail)  {
                if(frmemail.txtFile.value  == "")  {
                alert("Please attach a file");
                frmemail.txtFile.focus();
                return(false);
                }
                }
</script>


try and waiting for your reply
0
bhnmiAuthor Commented:
whats the syntax to define the file path?
0
Smart_ManCommented:
you can know that by using the same old code and then put a line like

msgbox as line 32. or i think it is in teh form

drive letter:\\path\\filenaem.extention

or even add \\machine name\\ before that if it is not on teh same machine. check for the accessibility (permissions) if it is on a shared location.

waiting for your reply
0
bhnmiAuthor Commented:
When I add the unc with the backslash it thinks I am trying to comment it out. I will try with the double slashes as you showed.
0
Smart_ManCommented:
ok. waiting for your reply
0
bhnmiAuthor Commented:
Okay, I get some different errors now.

F:\www\send-print-client.aspx(19,22): error CS0103: The name 'strFilepath' does not exist in the class or namespace 'ASP.send_print_client_aspx'
F:\www\send-print-client.aspx(21,1): error CS0246: The type or namespace name 'where' could not be found (are you missing a using directive or an assembly reference?)
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Mail" %>
<%@ Import Namespace="System.IO" %>
 
 
 
 
<script runat="server">
 
void btnSubmit_Click(Object sender, EventArgs e) {
 
MailMessage mail = new MailMessage();
mail.From = txtFrom.Text;
mail.To = txtTo.Text;
mail.Subject = txtSubject.Text;
mail.Body = txtMsg.Text;
mail.BodyFormat = MailFormat.Html;
 
mail.Attachments.Add(strFilepath);
 
where stFilePath = "files\\TPRDPenN.zip";
 
 
try
{ 
SmtpMail.Send(mail); 
} 
catch(Exception ex)
{
Response.Write("<b>Exception Occured:</b>   " +ex);
}
finally
{
Response.Write("Your E-mail has been sent sucessfully");
}
 
// Uploaded file deleted after sending e-mail
 
 
}
 
</script>    
 
 
 
 
<html>
 
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Using ASP.NET To Send Email - Part 2</title>
</head>
 
<body>
 
<div align="center">
<form method = "post" name = "frmemail" runat = "server" enctype="multipart/form-data" onSubmit = "return Tocheck(this)">
	<table border="0" width="637" height="196">
		<tr>
			<td width="637" height="196" valign="top" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
			
			<div align="left">From : 
			  <asp:TextBox ID = "txtFrom" Runat =server></asp:TextBox>
			</div>			
			<p align="left">To:     
              <asp:TextBox ID = "txtTo" runat = server></asp:TextBox>              
              <p align="left">Subject:<asp:TextBox ID = "txtSubject" Runat = server></asp:TextBox>
 
			<p align="left">Message:<asp:TextBox runat="server" Height="57px" TextMode="MultiLine" Width="212px" ID="txtMsg"></asp:TextBox>
 
 
			<p align="left">Attach:<input type = "file" id = "txtFile" runat = "server">
 
 
		  <p align="left"><asp:Button Runat = server ID = "btnSubmit" Text = "SEND" OnClick = "btnSubmit_Click"></asp:Button></td>
		</tr>
	</table>
  </form>
</div>
 
</body>
 
</html>

Open in new window

0
Smart_ManCommented:
looks like you are total beginner in coding and i have to do it all myself for ya. grrrrr. looools
here you go. copy and paste


void btnSubmit_Click(Object sender, EventArgs e) {
 
MailMessage mail = new MailMessage();
mail.From = txtFrom.Text;
mail.To = txtTo.Text;
mail.Subject = txtSubject.Text;
mail.Body = txtMsg.Text;
mail.BodyFormat = MailFormat.Html;
 
mail.Attachments.Add("files\\TPRDPenN.zip");
 
 
 
try
{
SmtpMail.Send(mail);
}
catch(Exception ex)
{
Response.Write("<b>Exception Occured:</b>   " +ex);
}
finally
{
Response.Write("Your E-mail has been sent sucessfully");
}
 
// Uploaded file deleted after sending e-mail
 
 
}
 
</script>    
 
 
 
 
<html>
 
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Using ASP.NET To Send Email - Part 2</title>
</head>
 
<body>
 
<div align="center">
<form method = "post" name = "frmemail" runat = "server" enctype="multipart/form-data" onSubmit = "return Tocheck(this)">
      <table border="0" width="637" height="196">
            <tr>
                  <td width="637" height="196" valign="top" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
                  
                  <div align="left">From :
                    <asp:TextBox ID = "txtFrom" Runat =server></asp:TextBox>
                  </div>                  
                  <p align="left">To:    
              <asp:TextBox ID = "txtTo" runat = server></asp:TextBox>              
              <p align="left">Subject:<asp:TextBox ID = "txtSubject" Runat = server></asp:TextBox>
 
                  <p align="left">Message:<asp:TextBox runat="server" Height="57px" TextMode="MultiLine" Width="212px" ID="txtMsg"></asp:TextBox>
 
 
                  <p align="left">Attach:<input type = "file" id = "txtFile" runat = "server">
 
 
              <p align="left"><asp:Button Runat = server ID = "btnSubmit" Text = "SEND" OnClick = "btnSubmit_Click"></asp:Button></td>
            </tr>
      </table>
  </form>
</div>
 
</body>
 
</html>
0
bhnmiAuthor Commented:
Sorry about that, I am pretty new at coding, especially ASP and such. Learning allot today already! :P

I really appreciate the help... and now for some more!

MailMessage mail = new MailMessage();
mail.From = txtFrom.Text;
mail.To = txtTo.Text;
mail.Subject = txtSubject.Text;
mail.Body = txtMsg.Text;
mail.BodyFormat = MailFormat.Html;

Lets say I want to replace all the variables except for the mail.To with static entries. Like I want to define the mail.From to a specific email address.
Would it be like this mail.From = (itsupport@abc.com);  ?
0
Smart_ManCommented:
whenever you see text ==> "your text goes here"
// ==> for commenting

so it will be like

MailMessage mail = new MailMessage();
mail.From = "the sender email goes here";
mail.To = "the reciever email goes here";
mail.Subject = "the subject goes here";
mail.Body = "the message body goes here";

waiting for your reply
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bhnmiAuthor Commented:
Here is the error

Exception Occured: System.InvalidCastException: Specified cast is not valid. at System.Web.Mail.CdoSysHelper.Send(MailMessage message) at System.Web.Mail.SmtpMail.Send(MailMessage message) at ASP.send_print_client_aspx.btnSubmit_Click(Object sender, EventArgs e)Your E-mail has been sent sucessfully
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Mail" %>
<%@ Import Namespace="System.IO" %>
 
 
 
 
<script runat="server">
 
void btnSubmit_Click(Object sender, EventArgs e) {
 
MailMessage mail = new MailMessage();
mail.From = "itsupport@bluehaven.com";
mail.To = txtTo.Text;
mail.Subject = "Print Client Software";
mail.Body = "Here is the file to install the Print Client for your remote accounting printing.";
mail.BodyFormat = MailFormat.Html;
 
mail.Attachments.Add("files\\TPRDPenN.zip");
 
 
 
try
{
SmtpMail.Send(mail);
}
catch(Exception ex)
{
Response.Write("<b>Exception Occured:</b>   " +ex);
}
finally
{
Response.Write("Your E-mail has been sent sucessfully");
}
 
// Uploaded file deleted after sending e-mail
 
 
}
 
</script>    
 
 
 
 
<html>
 
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Using ASP.NET To Send Email - Part 2</title>
</head>
 
<body>
 
<div align="center">
<form method = "post" name = "frmemail" runat = "server" enctype="multipart/form-data" onSubmit = "return Tocheck(this)">
      <table border="0" width="637" height="196">
            <tr>
                  <td width="637" height="196" valign="top" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
                  
                  <div align="left">
                  </div>                  
                  <p align="left">To:    
              <asp:TextBox ID = "txtTo" runat = server></asp:TextBox>              
              
              <p align="left"><asp:Button Runat = server ID = "btnSubmit" Text = "SEND" OnClick = "btnSubmit_Click"></asp:Button></td>
            </tr>
      </table>
  </form>
</div>
 
</body>
 
</html>

Open in new window

0
Smart_ManCommented:
no no no. that way you are trying to give me a heart attack .. loooooooool

here is teh code

MailMessage mail = new MailMessage();
mail.From = "itsupport@bluehaven.com";
mail.To = "itsupport@bluehaven.com";
mail.Subject = "Print Client Software";
mail.Body = "Here is the file to install the Print Client for your remote accounting printing.";
mail.BodyFormat = MailFormat.Html;
 
mail.Attachments.Add("c:\\files\\TPRDPenN.zip");


and you have to have some experince in writing in any language . please.

:)

i am kidding. itis your previlige and i am happy to help :)

waiting for yoru reply

0
bhnmiAuthor Commented:
Ok,
You put

mail.To = "itsupport@bluehaven.com";

but I need it to pull the email from the form so I keep it like this right?

mail.To = txtTo.Text;

Also, where do I define the smtp server?
0
Smart_ManCommented:
it should import the smtp setting from teh user setting on the user machine. you shoudl try that.
and yes leave the text box to be gettin from teh form if that is what you want.

now make a run and coem back with the errors or the satisfaction of a working solution

waiting for your reply
0
bhnmiAuthor Commented:
No smtp server is defined for the user session. It is a locked down terminal server. Still getting the same error.

Exception Occured: System.InvalidCastException: Specified cast is not valid. at System.Web.Mail.CdoSysHelper.Send(MailMessage message) at System.Web.Mail.SmtpMail.Send(MailMessage message) at ASP.send_print_client_aspx.btnSubmit_Click(Object sender, EventArgs e)Your E-mail has been sent sucessfully

You think it is because it has no idea what to use for a smtp server?
0
Smart_ManCommented:
ok. look for the object mail.* , you will find somthing like

mail.smtp

then you can assign the smtp server string

waiting for yoru reply
0
bhnmiAuthor Commented:
Yup, I got that still getting the exceptions though.
MailMessage mail = new MailMessage();
mail.From = "itsupport@bluehaven.com";
mail.To = txtTo.Text;
mail.Subject = "Print Client Software";
mail.Body = "Here is the file to install the Print Client for your remote accounting printing.";
mail.BodyFormat = MailFormat.Html;
SmtpMail.SmtpServer = "192.168.1.17";     <------------- Is this correct?
mail.Attachments.Add("f:\\www\\files\\TPRDPenN.zip");

Open in new window

0
Smart_ManCommented:
i guess you should import the smtp object at the begining as you did for web.mail.

also look for teh appropriate syntax for the smtp object by using the local help system. it may requier port to be added or to be written in a differnt way.


it says you have invalid cast at

System.Web.Mail.SmtpMail.Send

try to look up in the local system help about the object smtp and the meethod smtp.send

whata are you using to write your code ?

waiting for your reply
0
bhnmiAuthor Commented:
Thanks for all the help so far!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.