Showing posts with label attempting. Show all posts
Showing posts with label attempting. Show all posts

Saturday, March 24, 2012

Trouble shooting a simple System.Web.Mail test...

Hello all,

I have the following page on a .NET enabled server which acts as a test mail script. I am attempting to send mail on a much more complex page. When the page executed...nothing happened...no error messages...nothing. So, in an effort to troubleshoot I created this simple test and it seems that I may have found a direction to look in.


<%@dotnet.itags.org. Page Language = "VB" Debug = "True" Explicit = "True" %>
<%@dotnet.itags.org. Import Namespace="System.Web.Mail" %>
<script runat="server">
Sub Page_Load()
Dim mail As New MailMessage()
mail.To = "shane.fowlkes@dotnet.itags.org.drpt.virginia.gov"
mail.From = "webmaster@dotnet.itags.org.drpt.virginia.gov"
mail.Subject = "this is a test email."
mail.BodyFormat = MailFormat.Html
mail.Body = "this is my test email body.< br >< b >this part is in bold< /b >"
SmtpMail.SmtpServer = "localhost" 'your real server goes here
SmtpMail.Send(mail)
End Sub
</script>

The code above gives me the error message "The server rejected one or more recipient addresses. The server response was: 550 5.7.1 Unable to relay for webmaster@dotnet.itags.org.drpt.virginia.gov".

I looked on this helpful site: http://systemwebmail.com/faq/4.3.2.aspx and the second and third bullets on this page seem like they MAY apply. But the trouble is, I don't know if I "have permission to relay through the server" as the pointer tips indicate.

Can anyone shed some light on this or help me out? Thanks in advance!!Sure - here is the jist:

1.) If you site is hosted at www.mySite.com, then by default you should be allowed to send mail to anyone@.mysite.com -- however, if you want to send mail to anyone@.yahoo.com then the server/site must be set up with permissions. You should be able to send a request to your host to set this up (its an smtp setting in IIS)

2.) If its your server then keep in mind that by allowing domains (such as yahoo.com) to have sent, then spammers could potentially use your mail server for their malicious acts.

Let me know if you need further assistance.
Basically what it's telling you is that you're trying to "fake" mail. What's there to stop me from running the same code you posted and send 1,000,000 spam e-mails that look like they came from you?

Try changing localhost to your real mail server you use.
I actually tried that (earlier) and it worked. Thanks everyone.

Trouble updating

I have a Access database with 2 text fields. I'm reading one field into a text box, changing the value in the textbox and attempting to update the database with the new value. The page loads fine with the correct value in the textbox, but I'm getting a error on the Update statement. Below is the code. Any suggestions?

Sub LoadInventory()
Connect()
Dim strQuery As String = "SELECT Type, Number FROM Inventory WHERE type = 'squares'"

Dim dbComm As New OLEDBCommand(strQuery, objConnection)
Dim reader As OLEDBDataReader = dbComm.ExecuteReader()

reader.Read()
txtAmount.Text = reader.GetString(1)

reader.Close()
Disconnect()
End Sub

Sub btnChange_Click(ByVal Sender As Object, ByVal E As EventArgs)
UpdateInventory()
LoadInventory()
End Sub

Private Sub UpdateInventory()
Dim strSQL As String = "UPDATE Inventory SET Number = " & txtAmount.Text & " WHERE Type = 'squares'"
Connect()
Dim dbComm As New OLEDBCommand(strSQL, objConnection)
dbComm.ExecuteNonQuery()
Disconnect()
End Subinstead of

Dim strSQL As String = "UPDATE Inventory SET Number = " & txtAmount.Text & " WHERE Type = 'squares'"

try this
Dim strSQL As String = "UPDATE Inventory SET Number = " & Convert.ToInt32(txtAmount.Text) & " WHERE Type = 'squares'"

Jeff
Jeff,

Thanks for your reply. That however didn't work. Could it be the way I have my access database setup?

Jeremy
Jeff,

Does it work when you try putting in a static value for txtAmount.Text such as this:

"UPDATE Inventory SET Number =1 WHERE Type='squares'"

If it does work like that, then try doing this:


Try
Dim dbComm As New OLEDBCommand(strSQL, objConnection)
dbComm.ExecuteNonQuery()
Catch dbException As Exception
Response.Write(dbException.ToString())
End Try

Tell us what the exception is. Besides, it is always good practice anyway to put database queries inside a try statement - there are just way too many things that could go wrong to not catch those errors.

Aaron

Trouble with connection string in web.config

I am attempting to access a connection string from the web.config file, but I am receiving a "The ConnectionString property has not been initialized" error. If I place the connection string inline with my code it works perferctly, but when I attempt to access it via the web.config file I run into trouble. I have listed my code below which is very simple, because I am just learning .NET

Any help is greatly appreciated...

<configuration>
<appSettings>
<add key="ConnectionString" value="Server=localhost;database=msdb;Trusted_Connection=yes" />
</appSettings>
</configuration
Sub Page_Load(Sender as Object, E as EventArgs)
Dim strConnectionString as string = ConfigurationSettings.AppSettings("ConnectionString")
Dim strSQL As String = "SELECT * FROM log_shipping_databases"
Dim MyConnection as new Sqlconnection(strConnectionString)
Dim MyCommand as new SqlCommand(strSQL, MyConnection)

MyConnection.Open()
MyDataGrid.DataSource = MyCommand.ExecuteReader()
MyDataGrid.DataBind()
MyConnection.Close()
End Sub

Bump, still stuck on this problem. Any help would be greatly appreciated.
Have you had anything work from your web.config file?? The only thing I can think of is that your IIS application is not configured correctly. If this is the case, your .aspx pages will work, but IIS will not know where to look for your web.config file. This may not be the case at all....but it is my best guess at your problem. Check your site in IIS an make sure your application is configured correctly. And also double check to see that your web.config file is in the root of said application.

Hope that helps

-Jason
Jason,

Thanks for the reply. I went into IIS and made sure everything was ok, and as far as I can tell it seemed to be set up correctly. However, I deleted the existing virtual directory and created a brand new directory and set up the application again. It is still not working as intended though.

I also attempted to obtain other information that was contained in the web.config file and that did not work either.

Lastly, I am creating all my .aspx files in the root directory and that is where web.config resides as well.

Can you think of a particular setting I can check to verify everything is set up correctly? Thanks for the input thus far.

-Corey
Have you imported System.Configuration?

If you are going to use ConfigurationSettings.AppSettings("ConnectionString") then you need to import System.Configuration. I usually import System.Configuration.ConfigurationSettings and then just use AppSettings("ConnectionString") so there is less code to write.
Yes, I have also tried that as well. Below is the complete code for my project.


<%@. Page Language="VB" %>
Imports System.Configuration

<script runat="server"
Sub Page_Load(Sender as Object, E as EventArgs)
Dim strConnectionString as string = ConfigurationSettings.AppSettings("ConnectionString")
Dim strSQL As String = "SELECT * FROM log_shipping_databases"
Dim MyConnection as new System.Data.OleDb.OleDbConnection(strConnectionString)
Dim MyCommand as new System.Data.OleDb.OleDBCommand(strSQL, MyConnection)

MyConnection.Open()
MyDataGrid.DataSource = MyCommand.ExecuteReader()
MyDataGrid.DataBind()
MyConnection.Close()
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:datagrid id="MyDataGrid" runat="server"></asp:datagrid>
</form>
</body>
</html


Delete any web.configs that are outside of your virtual apps.
This can cause problems.
So inside wwwroot there will be no web.config file, they will only be inside your applications folders.
HTH
Not that it matters in this case, but you got the syntax for importing a namespace wrong. It should have been:
<%@. Page Language="VB" %>
<%@. Import Namespace="System.Configuration" %>

It doesn't matter in this case, as it's not necessary. Your code, as shown, is sufficient to call information from web.config.

So, your problem is not in code. It is likely a setup problem. For example, the web.config file must go in the root folder of your Application. Like this:

c:\
\inetpub
\wwwroot
\MyApp
web.config
default.aspx

If you've placed it inside wwwroot, rather than the root of your own Application, this would cause your problem.

Hence, can you confirm that you've placed your web.config and your ASPX page in their own App folder, as shown? And that that app has been made a Web Application with IIS?
I think the following link could be useful.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaspnetconfiguration.asp

The web.config is inheritable. This means you can have a web.config in wwwroot and in your application folder. When you call ConfigurationSettings.AppSettings("ConnectionString")
the MyApp web.config would be checked first. If that key isn't found then it would look in wwwroot. So, it is not required that you have a web.config in the app folder if you have one in wwwroot. I create a lot of applications that use a connection string so the connection string is placed in the wwwroot web.config that way it resides in one location. Then the web.config files in the app folder only hold application level settings.

Also, virtual roots or IIS applications do not affect this at all. I have tested it both ways. With or without a virtual root the web.config inheritance still works.

The last post is a good test but I don't think it's going to solve the problem. If you had a web.config in wwwroot and a connection string key in there, you wouldn't have to have a web.config in your application folder.

I actually am curious to see if the namespace correction mentioned above fixes things. SomeNewKid2, if he was using incorrect syntax on importing the namespace then I think it would matter, right? No namespace would mean no ConfigurationSettings.AppSettings exists to the application.
No, System.Configuration is a namespace that's implicitly imported with inline code. Hence, he can use ConfigurationSettings.AppSettings, without explicitly importing its namespace.

The way he had it meant it was simply considered literal text. No harm done, and it wouldn't have solved his problem even if his syntax was correct.

(I have used his own code to access a web.config. Works fine. The problem is not in his code.)
Below is where my web.config and default.aspx files reside:

c:\inetpub\wwwroot\training\web.config
c:\inetpub\wwwroot\training\default.aspx

Also, I have checked IIS to verify that it is set up as an application. I also deleted the virtual directory and setup it up as an application from scratch in IIS. I may very well be excluding a minor detail somewhere, but I can't seem to indetify it.

Thanks for all the help,
Corey
Back up your current default.aspx and web.config files. (Or just rename them temporarily.)

Try replacing your web.config with this very simple one:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration
<appSettings>
<add key="ConnectionString" value="Server=localhost;database=msdb;Trusted_Connection=yes" />
</appSettings
<system.web>
</system.web
</configuration>

Now, replace your default.aspx with this modified version:

<%@. Page Language="VB" Debug="true" %
<script runat="server"
Sub Page_Load(Sender as Object, E as EventArgs)
Dim strConnectionString as string = ConfigurationSettings.AppSettings("ConnectionString")
Dim strSQL As String = "SELECT * FROM log_shipping_databases"
'Dim MyConnection as new System.Data.OleDb.OleDbConnection(strConnectionString)
'Dim MyCommand as new System.Data.OleDb.OleDBCommand(strSQL, MyConnection)

'MyConnection.Open()
'MyDataGrid.DataSource = MyCommand.ExecuteReader()
'MyDataGrid.DataBind()
'MyConnection.Close()
Message.Text = strConnectionString
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:datagrid id="MyDataGrid" runat="server"></asp:datagrid>
<asp:Label id="Message" runat="server" />
</form>
</body>
</html>

This code works fine on my machine. Let me know if it works okay on yours. (It should simply show the ConnectionString value that it retrieved from web.config.)
It prints the connection string to the browser:

Server=localhost;database=msdb;Trusted_Connection=yes
Which means it worked.

Well, the .aspx file is just a simplified version of your own. Which suggests that there is merely an error in your web.config file.

Since the web.config file is an XML file, it is very sensitive to syntax and proper nesting of statements.
You probably just have a statement in the wrong place.

Do you want to post the code for your web.config file?