ASPX to ASP Conversion (E-mail Script)? - asp.net

I am a PHP Developer and I do not have any knowledge about ASP. Sadly I am the only developer here and a client had ASP (not ASP.NET) pages and they wanted a "Contact Us" Form.
I searched the internet to find out how to serve ASP files in Apache and it pointed me to mod_aspdotnet. It was so stupid of me to think that ASP is the same as ASP.NET! Now do you see my problem? I have developed a working ASP.NET email script. The files had .aspx extensions but the actual site had .asp extensions, they were using ASP and not ASP.NET!
So I would like to ask for help. This is my email script:
<%# Page Language="VB" Debug="true" %>
<%#Import Namespace="System.Web.Mail" %>
<script language="vb" runat="server">
Sub Send2Mail (sender as Object, e as EventArgs)
Dim objMail as New MailMessage()
if Logo.HasFile Then
Try
Logo.SaveAs(Server.MapPath("uploads/") + Request.form("strName") + "_Logo_" + Logo.FileName)
objMail.Attachments.Add(new MailAttachment(Server.MapPath("uploads/" + Request.form("strName") + "_Logo_" + Logo.FileName)))
Catch ex As Exception
msg.Text = "<b>The Logo could not be uploaded</b>. The following error occured: <i>" + ex.Message + "</i><br />"
msg.Visible = true
end try
end if
if Image1.HasFile Then
Try
Image1.SaveAs(Server.MapPath("uploads/") + Request.form("strName") + "_Image1_" + Image1.FileName)
objMail.Attachments.Add(new MailAttachment(Server.MapPath("uploads/" + Request.form("strName") + "_Image1_" + Image1.FileName)))
Catch ex As Exception
msg.Text = "Image 1 could not be uploaded. The following error occured: <i>" + ex.Message + "</i><br />"
msg.Visible = true
end try
end if
if Image2.HasFile Then
Try
Image2.SaveAs(Server.MapPath("uploads/") + Request.form("strName") + "_Image2_" + Image2.FileName)
objMail.Attachments.Add(new MailAttachment(Server.MapPath("uploads/" + Request.form("strName") + "_Image2_" + Image2.FileName)))
Catch ex As Exception
msg.Text = "Image 2 could not be uploaded. The following error occured: <i>" + ex.Message + "</i><br />"
msg.Visible = true
end try
end if
objMail.To = "example#example.com"
objMail.From = """Us"" <do-not-reply#foo.com>"
objMail.BodyFormat = MailFormat.Html
objMail.Priority = MailPriority.Normal
objMail.Subject = "Business Registration"
objMail.Body = "<html><body style='font-family: Verdana'><table style='font-family: Verdana; font-size: 11px'>"
objMail.Body += "<tr><td><b>Business Name:</b></td><td>" + Request.form("strName") + "</td></tr>"
objMail.Body += "<tr><td><b>Opening Business Description:</b></td><td>" + Request.form("strOpenDesc") + "</td></tr>"
objMail.Body += "<tr><td><b>Opening Hours:</b></td><td>" + Request.form("strHours") + "</td></tr>"
objMail.Body += "<tr><td><b>Business Description:</b></td><td>" + Request.form("strBusDesc") + "</td></tr>"
objMail.Body += "<tr><td><b>Servicing Area:</b></td><td>" + Request.form("strService") + "</td></tr>"
objMail.Body += "<tr><td><b>Website Address:</b></td><td>" + Request.form("strWebsite") + "</td></tr>"
objMail.Body += "<tr><td><b>Email Address:</b></td><td>" + Request.form("strEmail") + "</td></tr>"
objMail.Body += "<tr><td><b>Telephone Number:</b></td><td>" + Request.form("strPhone") + "</td></tr>"
objMail.Body += "<tr><td><b>Fax Number:</b></td><td>" + Request.form("strFax") + "</td></tr>"
objMail.Body += "<tr><td><b>Mobile Phone Number:</b></td><td>" + Request.form("strMobile") + "</td></tr>"
objMail.Body += "<tr><td><b>Suburb / Post Code:</b></td><td>" + Request.form("strPostCode") + "</td></tr>"
objMail.Body += "<tr><td><b>Proprietor Name:</b></td><td>" + Request.form("strPropName") + "</td></tr>"
objMail.Body += "<tr><td><br /></td></tr>"
objMail.Body += "<tr><td><b>Image 1 Caption:</b></td><td>" + Request.form("strImage1Caption") + "</td></tr>"
objMail.Body += "<tr><td><b>Image 2 Caption:</b></td><td>" + Request.form("strImage2Caption") + "</td></tr>"
objMail.Body += "</table><body></html>"
SmtpMail.SmtpServer = "localhost"
Try
SmtpMail.Send(objMail)
strMessage.Visible = true
Catch ex As Exception
msg.Text = "<b>The message was not sent</b>. The following error occured: <i>" + ex.Message + "</i><br />"
msg.Visible = true
End Try
End Sub
Will this work in ASP as it is? What do I need to change to make it work in ASP? I am also using the <asp:></asp:> tags. Will this work in ASP?
EDIT
I am sorry for the confusion of what server I am using for development and the host server. The host is using IIS. I am using Apache because I am really a PHP developer. The problem about running ASP pages in Apache was because the client does not want to give me access to their server. They told me that I should just deliver the files.

ASP and ASP.NET share names for pure marketing reasons but they are basically unrelated technologies, although the latter reuses concepts (such as being a language agnostic framework) and even some method names (such as Server.MapPath).
In both ASP's, you have to pick a supported programming language and code your scripts with it. Your sample code seems to use VisualBasic, which was not supported by classic ASP. You'll have to switch to (e.g.) VBScript. Unlike VisualBasic, VBScript is loosely typed, but apart from that it has a similar syntax.
The classic ASP way to send e-mail is the CDONTS library. A simple example:
<%
set mailer = CreateObject("CDONTS.NewMail")
mailer.from = "foo#example.com"
mailer.to = "bar#example.com"
mailer.subject = "Test e-mail message"
mailer.body = "This is the message body."
mailer.send
set mailer = nothing
%>
In general, classic ASP was very similar to PHP: you embed code in your HTML documents.
The difficult part is file upload handling. ASP did not have a native file upload feature: you had to purchase and install a binary commercial library or find one of the VBScript-only code snippets available at the Internet.
Edit: Does your client really run ASP over Apache? Microsoft only supports IIS. Apache modules for ASP were normally written by third-parties and often implemented different languages like Perl.

ASP (or Classic ASP as it is now known) and ASP.NET are different beasts. The first is a scripted language similar to PHP, while ASP.NET use the .NET Framework and is compiled to a MSIL binary either before being run and uploaded as a .NET assembly, or on the fly by IIS if the page contains inline script, either way they are then run by the CLR (common language runtime) which forms the core part of the .NET framework.
While some VB.NET code could be converted back to VBScript (the most commonly used Classic ASP language), anything that uses any part of the .NET framework outside of the Microsoft.VisualBasic namespace won't be available. To access this kind of functionality you would need to use third party ActiveX/COM components.
You say you want to serve up the ASP script on Apache, is that because you don't have access to an IIS server or that this site that is already running in on Apache? As far as I am aware the only way to run Classic ASP on Apache is to use ChilliSoft/SunOne ASP for Apache. Some hosting companies offer this, but it's a legacy product.
If it is running ChilliSoft ASP then there is a mail component that could be installed, if the host has installed the SpicePack. Take a look at this for more information:
http://developers.sun.com/asp/howto/chilimail.html
http://ns7.webmasters.com/caspdoc/pdf/chilisoft_asp_docs.pdf
I've ported numerous ASP applications from IIS to Apache using ChilliSoft ASP and generally it works very well.
Have a look at this answer for one way on how to send email using ASP: Sending Mail code in ASP
Failing everything else, just add yourself a page to send the mail in PHP and get the contact form to POST to that page, and then redirect back to an ASP page if you want it to appear seamless.

Related

catch an event from a message box in asp.net

So my boss gaved me the project of adapting a vb.net aplication into asp.net but i never worked with asp.net before and im learning from the internet so dont blame me xD. So in my page after the login i wanted a msgbox to pop up saying "welcome" or something like that, and i managed to come across this code
MessageBox("Welcome to wrox forum")
Private Sub MessageBox(ByVal msg As String)
Dim lbl As New Label
lbl.Text = "<script language='javascript'>" & Environment.NewLine & _
"window.alert('" + msg + "')</script>"
Page.Controls.Add(lbl)
End Sub
so this works fine but after the message (after the user press the ok button) i would like to redirect the user to other page but i dont know how to create the event wich will trigger after the user press "ok" any ideas?
thank you,
The code after alert will execute once the dialog is closed. So just add your code at the end of the script, e.g.,
lbl.Text = "<script language='javascript'>" & Environment.NewLine & _
"window.alert('" + msg + "');" & Environment.NewLine & _
"document.location = 'https://www.GoHere.com';</script>"

How to get package name (Source Name) from the event log for SSIS errors in ASP.NET

I’m successfully reading the server event log to display SSIS errors from the Application event log. The aim is to make it possible to do first line troubleshooting without having to log into the server as an Administrator.
I’m using the EventLogEntry in vb.net to show Errors where the source is SQLISPackage110. However what’s stumping me is that when viewing the errors in the event log there is a “Source Name” property that displays the package name. However the EventLogEntry doesn’t seem to have this property which makes it impossible to tell which package has returned the error.
Has anyone come across this and managed to find a way round it?
A screenshot of the error and some of my vb.net code is below
Dim myEventLogEntryCollection As EventLogEntryCollection = myEventLog1.Entries
myEventLog1.Close()
Dim x As Integer
Dim entry As EventLogEntry
If myEventLogEntryCollection.Count > 0 Then
strTable += "<table class='table table-bordered'><tr><th>Time Written</th><th>Source</th><th>Event type</th><th>Message</th></tr>"
For Each entry In myEventLogEntryCollection.Cast(Of EventLogEntry).Reverse 'cast and reverse to show newest first
If entry.Source = strEventSource And (entry.EntryType.ToString = "Error" Or entry.EntryType.ToString = "Warning") Then
strTable += "<tr>"
strTable += "<td>" + entry.TimeWritten.ToShortDateString + " " + entry.TimeWritten.ToShortTimeString + "</td>"
strTable += "<td>" + entry.Source.ToString + "</td>"
strTable += "<td>" + entry.EntryType.ToString + "</td>"
strTable += "<td>" + entry.Message + "</td>"
strTable += "</tr>"
x += 1
End If
If x > 100 Then Exit For 'could be 1000s in the log
Next
strTable += "</table>"
End If
As you've noticed, "Source" is NOT one of the LogEntry properties.
Instead the word "Source" is just text trapped inside of the larger Message property. So, to get the "Source" information you're going to have to parse it out of the Message. Split the message into lines. Then loop over the lines and do something when you find a line containing the word "Source"
This code is NOT tested. But, just to give the idea..
Dim messageLines As String() = Nothing
messageLines = entry.Message.Split(Environment.NewLine)
Dim line As String
For Each line In messageLines
If line.Contains("Source") Then
'your code here
Next line
SSIS packages can log to many destinations. And yes, logging to the Windows Event log is an option, but I've seldom seen it used in practice. Windows Event Logs aren't permanent. By default, they are going to start trimming the history once they get over twenty megabytes in size. They are a bad place to store SSIS log information for troubleshooting.
Would you consider gently suggesting to your package authors that they use SSIS SQL logging instead? If they logged to SQL then you'd have the information you need arranged, and stored in a nice searchable manner that would allow for easier querying, aggregation, and analysis across your entire organization. You'd even have the source column you could use to get the exact info you need.

VB.NET 2.0: Where does a URL in code come from?

I have to debug an old VB.NET 2.0 code of someone who has left the company. We have a production system (let us call it http://prod) and a test system (http://test). Both are nearly similiar including a documents repository. When looking at docs in production, all the hyperlinks showing up at the bottom are okay (meaning they say something like http://prod/download.ashx?id={GUID}).
However in test it is the same (http://prod/download.ashx?id={GUID}), even it should be http://test/download.ashx?id={GUID} instead.
After hours of debugging I have found the relevant line of code:
html += "<td><a href='" + HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.Url.PathAndQuery, "/") + "int/download.ashx?id=" + row.Item(0).ToString() + "' target='_blank' class='" + row.Item(3).ToString() + "'>" + row.Item(1).ToString() + "</a>" + privat + "</td><td>" + row.Item(2).ToString() + "</td>"
Looking at html this shows i.e.
"<table class='table_dataTable'><thead><tr><td>Name</td><td>Jahr</td></tr></thead><tbody><tr><td><a href='http://prod/int/download.ashx?id=4d280886-db88-4b25-98d8-cf95a685d4a4' target='_blank' class='doc'>Document for managers</a></td><td>2014</td>"
So I wonder, where does this come from incorrectly? I may have found the relevant part of coding, but I am not sure, what to do now, respectively if I am right on this?:
Public Class download : Implements IHttpHandler, IReadOnlySessionState
Dim debug As String = ""
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim fehler As String = ""
Try
' Get the file name from the query string
Dim queryFile As String = context.Request.QueryString("id")
debug += "id=" + queryFile + "<br>"
Any help is appreciated as VB.NET is not my main focus.
You have probably checked this but sometimes the obvious gets overlooked.
Verify the URL in your browser window. Make sure it has not changed to http://prod... while you were navegating.
Verify that your web application is not using frames. The page in question could be loaded in a frame using the prod URL. If this is the case your web.config might have a setting to say where this frame is loaded from or it might simply be hardcoded.
Check for URL Rewrite rules in IIS or your web.config

VB.Net Outlook email creation works locally, fails on server

When I run my project on my local machine it generates the Outlook email as expected, but if I upload all my code to our development server, it fails with this error message when run: [Exception: Cannot create ActiveX component.]
Microsoft.VisualBasic.Interaction.CreateObject(String ProgId, String ServerName) +509721
etc...
I know it's the Outlook that's failing because if I comment the sendmail() function, the rest of the page works fine. (it just doesn't create the email).
Local machine is Windows 7 with Outlook installed, Server is 2008R2 with no Office installed. I have other pages that can write to Excel files, but they're using CrystalReports to handle it so I'm not sure if the server needs an Outlook dll registered, or if something else needs to happen.
Aspx page with a VB.Net codebehind. My create email looks like:
Dim OApp2 As Object, OMail2 As Object, signature2 As String
OApp2 = CreateObject("Outlook.Application")
OMail2 = OApp2.CreateItem(0)
Dim alAttachList As New ArrayList(2)
Dim iCounter As Integer
alAttachList.Insert(0, "E:\Test\DEBUG CODE.txt")
alAttachList.Insert(1, "\\RemoteServer\z\Test\Hello.bmp")
sBody += "<br />" + "Attached are some files." + "<br />" + "They can also be found in: X:\Test\Test\Name "
With OMail2
.Display()
End With
signature2 = OMail2.HTMLBody
With OMail2
.Subject = sSubject
.To = sTo
.CC = sCC
.HTMLbody = sBody & "<br /><br />" & signature2
End With
Dim sBodyLen As Integer = Int(sBody.Length)
Dim oAttachs2 As Interop.Outlook.Attachments = OMail2.Attachments
Dim oAttach2 As Interop.Outlook.Attachment
For iCounter = 0 To alAttachList.Count - 1
oAttach2 = oAttachs2.Add(Source:=alAttachList(iCounter), DisplayName:=alAttachList(iCounter))
Next
OMail2.Display(True)
OApp2 = Nothing
OMail2 = Nothing
When you do something like:
OApp2 = CreateObject("Outlook.Application")
You are using the Office.Interop libraries which are installed with MS Office (or the individual applications if you go that route)
You MUST install MS Office, or at least Outlook, on the server to use Office.Interop. You might look into using MAPI instead of interop to send your emails. MAPI is part of .Net and does NOT require additional programs be installed on your server.
Firstly, Outlook must be installed. Secondly, is your code running in a service (such as IIS)? No Office app (Outlook included) can be used from a service.

New to asp.net. Need help debugging this email form

First of all, I am a php developer and most of .net is alien to me which is why I am posting here!
I just migrated over a site from one set of webhosting to another. The whole site is written in .net. None of the site is database driven so most of it works, except for the contact form. The output on the site simple states there was an error with "There has been an error - please try to submit the contact form again, if you continue to experience problems, please notify our webmaster." This is just a simple message it pops out of it gets to the "catch" part of the email function.
I went into web.config and changed the parameters:
<emailaddresses>
<add name="System" value="roeland#hoyespharmacy.com"/>
<add name="Contact" value="roeland#bythepixel.com"/>
<add name="Info" value="roeland#bythepixel.com"/>
</emailaddresses>
<general>
<add name="WebSiteDomain" value="hoyespharmacy.com"/>
</general>
Then the .cs file for contact contains the mail function EmailFormData():
private void EmailFormData()
{
try
{
StringBuilder body = new StringBuilder();
body.Append("Name" + ": " + txtName.Text + "\n\r");
body.Append("Phone" + ": " + txtPhone.Text + "\n\r");
body.Append("Email" + ": " + txtEmail.Text + "\n\r");
body.Append("Fax" + ": " + txtEmail.Text + "\n\r");
body.Append("Subject" + ": " + ddlSubject.SelectedValue + "\n\r");
body.Append("Message" + ": " + txtMessage.Text);
MailMessage mail = new MailMessage();
mail.IsBodyHtml = false;
mail.To.Add(new MailAddress(Settings.GetEmailAddress("System")));
mail.Subject = "Contact Us Form Submission";
mail.From = new MailAddress(Settings.GetEmailAddress("System"), Settings.WebSiteDomain);
mail.Body = body.ToString();
SmtpClient smtpcl = new SmtpClient();
smtpcl.Send(mail);
}
catch
{
Utilities.RedirectPermanently(Request.Url.AbsolutePath + "?messageSent=false");
}
}
How do I see what the actual error is. I figure I can do something with the "catch" part of the function.. Any pointers?
Thanks!
Change the catch to
catch(Exception ex)
{
throw;
}
The ex variable will hold your exception information, so you can put a breakpoint there. It'd be easier to step through it, but you can just throw the error as well.
Comment out Utilities.RedirectPermanently(Request.Url.AbsolutePath + "?messageSent=false");
and replace it with throw;
What development environment are you using?
It's probably best to use Visual Studio (Express if you don't have the full version), and debug this code, hitting F11 to step through each statement until it breaks. Then you should have access to more information.
I would suggest logging in the long term (such as log4net). But for the sake of speed try changing your catch statement to look like:
catch(Exception e)
and then use the debugger in VS to explore the actual exception.
Place a breakpoint on the first line of the EmailFormData method and run the application in debug mode. You can then step through the code line by line.

Resources