Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Cannot send emails from asp.net

Posted on 2005-04-08
35
Medium Priority
?
275 Views
Last Modified: 2012-05-05
i am trying to send email to a list of email addresses retrieved from sql server, but it doesn't seem to be sending. i even tried to send it to my own address but i still get nothing. i looked in C:\Inetpub\mailroot\ and in the Queue folder i have two messages there. i went into IIS to see if smpt is running, and it is.

how can i get the emails to send?

here is the code below, i dont know if i'm doing something wrong here:

        If DropDownList2.SelectedValue = "0" Then

            Dim Sqlstrg As SqlConnection
            Dim Sqlquery As String
            Dim cmdSelect1 As SqlCommand

            Sqlstrg = New SqlConnection("server=localhost;uid=sa;pwd=;database=stmdb;")

            Sqlquery = "SELECT EmailAddress, IsStudent FROM TblUsername WHERE IsStudent = '1'"

            cmdSelect1 = New SqlCommand(Sqlquery, Sqlstrg)

            Dim objMailMsg As MailMessage

            objMailMsg = New MailMessage

            objMailMsg.To = "Sqlquery"
            objMailMsg.Subject = "New coursework document"
            objMailMsg.Priority = MailPriority.High
            objMailMsg.Body = "this is a test"

            SmtpMail.Send(objMailMsg)


        End If

0
Comment
Question by:Rpatel108
  • 20
  • 13
  • +1
35 Comments
 
LVL 4

Expert Comment

by:w3tim
ID: 13736167
Hi Rpatel108,

Try changing;

SmtpMail.Send(objMailMsg)

to

Dim smtpObj As System.Web.Mail.SmtpMail
smtpObj.SmtpServer("mail.yourserver.com")
smtpObj.Send(objMailMsg)

Let me know if this works.

Thanks,
Tim
0
 
LVL 10

Expert Comment

by:djhex
ID: 13736169
SmtpMail.smtpserver = "MYSMTPSERVER"
0
 

Author Comment

by:Rpatel108
ID: 13737997
its ok my firewall seems to be blocking it.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Expert Comment

by:vladimir_kalashnikov
ID: 13738722
you need to set the "from" field as well as specify the server, this is code that i have used (with different values, obviously):


Dim msg As MailMessage
msg = new MailMessage
msg.To = "you@hotmail.com"
msg.From = "me@hotmail.com"
msg.Subject = "This is your subject."
msg.BodyFormat = MailFormat.Html
msg.Body = "This is the mail body."
SmtpMail.SmtpServer = "mail.myserver.com"
SmtpMail.Send(msg)
0
 

Author Comment

by:Rpatel108
ID: 13739257
ok i know my code works fine cos it sends when i turn off norton, but when it is on and i have added the correct port, it doesn't send whne norton is on.
0
 

Author Comment

by:Rpatel108
ID: 13740138
i cant seem to pass the values from the sql query, does anyone know how?

thanks
0
 

Author Comment

by:Rpatel108
ID: 13740639
i mean, i cant get the values out from the database and pass that in the: objMailMsg.To = "sqlquery"

when i send emails using the code below the emails get sent in the Drop folder.

I'm not really sure how to do it.

here is my code:
        If DropDownList2.SelectedValue = "0" Then

            Dim Sqlstrg As SqlConnection
            Dim Sqlquery As String
            Dim cmdSelect1 As SqlCommand

            SmtpMail.SmtpServer = "localhost"

            Sqlstrg = New SqlConnection("server=localhost;uid=sa;pwd=;database=stmdb;")
            Sqlstrg.Open()
            Sqlquery = "SELECT EmailAddress FROM TblUsername"

            cmdSelect1 = New SqlCommand(Sqlquery, Sqlstrg)
            Dim dr As SqlDataReader
            dr = cmdSelect1.ExecuteReader(CommandBehavior.CloseConnection)

            Dim objMailMsg As MailMessage

            objMailMsg = New MailMessage
            objMailMsg.From = "StudentServices@stm.com"
            objMailMsg.To = "Sqlquery"
            objMailMsg.Subject = "new coursework documents"
            objMailMsg.Priority = MailPriority.High
            objMailMsg.Body = "rukmini patel"


            SmtpMail.Send(objMailMsg)

        End If
0
 
LVL 4

Accepted Solution

by:
vladimir_kalashnikov earned 2000 total points
ID: 13741314
ok, there is a problem here.  your need to get the results from the reader like so:

Dim toString As String
toString = ""

If dr.Read() Then // you need this line to start reading
    toString = dr["EmailAddress"].ToString
End If

dr.Close // always close the reader when its done

Dim objMailMsg As MailMessage

objMailMsg = New MailMessage
objMailMsg.From = "StudentServices@stm.com"
objMailMsg.To = toString
objMailMsg.Subject = "new coursework documents"
objMailMsg.Priority = MailPriority.High
objMailMsg.Body = "rukmini patel"

SmtpMail.Send(objMailMsg)
0
 

Author Comment

by:Rpatel108
ID: 13741602
i get a jagged blue line for this statement :  dr["EmailAddress"].ToString

saying that dr cannot be converted into sting, and for ["EmailAddress"]  an is identifier expected.
0
 
LVL 4

Expert Comment

by:vladimir_kalashnikov
ID: 13741645
sorry, it might be the C# syntax i am using, try this:

dr("EmailAddress").ToString
0
 

Author Comment

by:Rpatel108
ID: 13741712
ok the code runs fine, however i get an error:

At least one recipient is required, but none were found.

highlighting this line: SmtpMail.Send(objMailMsg)

Also i am having some trouble with my firewall and SMTP, i have to disable my firewall and then send the emails. I checked the C:\Inetpub\mailroot\Drop folder and it has lots of files in there, which i think are the files that i am trying to send.
0
 
LVL 4

Expert Comment

by:vladimir_kalashnikov
ID: 13754632
the error means that you are not setting the objMailMsg.To field.  check to make sure that the string you are assigning to it has a valid value.  As for your firewall, there should be a setting to allow outgoing email.  Sorry it took me so long to reply, but i don't use my computer on weekends.
0
 

Author Comment

by:Rpatel108
ID: 13755024
thank you for replying! i have been stuck since. I dont know where the problem is really because i have assigned the objMailmsg.To field i did it as you showed me:

            Dim toString As String
            toString = ""

            If dr.Read() Then '  start reading the datareader
                dr("EmailAddress").ToString()
            End If

            dr.Close() ' close the reader when its done

            Dim objMailMsg As MailMessage

            objMailMsg = New MailMessage
            objMailMsg.From = "StudentServices@stm.com"
            objMailMsg.To = toString
            objMailMsg.Subject = "new coursework documents"
            objMailMsg.Priority = MailPriority.High
            objMailMsg.Body = "blah blah"


            SmtpMail.Send(objMailMsg)

am i missing something? i've checked the database too to make sure the email addresses are valid which they are.

Please help me!!

Thanks
0
 
LVL 4

Expert Comment

by:vladimir_kalashnikov
ID: 13755113
try outputing the 'to' value to the screen to verify that the value is being returned properly.  you could try this as well:

    Dim toString As String
            toString = "youremail@stuff.com"

By putting your email here, then you can see if it sends it to you.  If it does then the address in the database is not being returned, which problably means there is something wrong with your SQL command.
0
 

Author Comment

by:Rpatel108
ID: 13757078
ok i emailed it to myself and it worked. which means your right there is something wrong with my sql statement.

but i cant see how my select statement is wrong? i've even checked it on sql server and it seems to be correct.

"SELECT EmailAddress FROM TblUserName"

when i insert a break point at the select statement, the watch says the sqlquery is true.

i dont know what else to do?
0
 
LVL 4

Expert Comment

by:vladimir_kalashnikov
ID: 13757556
Make sure:

you are using this select statement:

SELECT EmailAddress FROM TblUsername  // * Note that this is different that in your original post where you had IsStudent = '1'

AND

you have run the statement and gotten the correct values from the server (in query analyzer or something).

This is just weird because the only reason that the datareader would be empty is if the sql statement isn't returning any values.

Maybe you should post all of the code again for the data access and sending the mail, there may just be a simple logic error.
0
 

Author Comment

by:Rpatel108
ID: 13757616
Sorry i changed it to another database to test if the email addresses would work, so i changed the select statement too.  



0
 

Author Comment

by:Rpatel108
ID: 13761085
hi there, sorry i forgot to post up my code last night. i have put my select statement back to its original state as it doesn't seem to make a difference if i change the statement or not. i also checked the select statement in query anaylser, and it gave me the email addresses as well as the users that were IsStudents.

here is my code:

  If DropDownList2.SelectedValue = "0" Then

            Dim Sqlstrg As SqlConnection
            Dim Sqlquery As String
            Dim cmdSelect1 As SqlCommand

            SmtpMail.SmtpServer = "localhost"


            Sqlstrg = New SqlConnection("server=localhost;uid=sa;pwd=;database=stmdb;")
            Sqlstrg.Open()
            Sqlquery = "SELECT EmailAddress, IsStudent FROM TblUsername WHERE IsStudent = '1'"

            cmdSelect1 = New SqlCommand(Sqlquery, Sqlstrg)

            Dim toString As String
            toString = ""

            Dim dr As SqlDataReader
            dr = cmdSelect1.ExecuteReader(CommandBehavior.CloseConnection)

            If dr.Read() Then '  start reading the datareader
                dr("EmailAddress").ToString()
            End If

            dr.Close() ' close the reader when its done

            Dim objMailMsg As MailMessage

            objMailMsg = New MailMessage
            objMailMsg.From = "StudentServices@stm.com"
            objMailMsg.To = toString
            objMailMsg.Subject = "new coursework documents"
            objMailMsg.Priority = MailPriority.High
            objMailMsg.Body = "blah blah"

            SmtpMail.Send(objMailMsg)

        End If
0
 

Author Comment

by:Rpatel108
ID: 13761134
i also have lots of emails in the C:\Inetpub\mailroot\Drop folder. when i open one of them it says:


This is an automatically generated Delivery Status Notification.

Delivery to the following recipients failed.

       myemail@email.com

0
 
LVL 4

Expert Comment

by:vladimir_kalashnikov
ID: 13764892
first of all, just checking, but you should be using your own email address (for testing purposes), not the 'myemail@email.com'.

assuming that you are using your own email address though, it seems that there might be a problem with your mail server.

other than that, make sure that your connection string works (maybe just selecting something from a different table),  you could try the dr.HasRows and see what value it has (before you try to read), not sure how that would help though.

I can't really see what's wrong here, it must be something really simple, too obvious to notice.
0
 

Author Comment

by:Rpatel108
ID: 13764992
haha - yeah i did use my own email address.

could there be something wrong with SMTP? but it works when i put in my own email address in teh objmailmsg.to string.

0
 
LVL 4

Expert Comment

by:vladimir_kalashnikov
ID: 13765104
you get the emails AND get the Delivery Status Notification message?
0
 

Author Comment

by:Rpatel108
ID: 13765178
ok when i inserted my own email address in the To string:            

Dim toString As String
toString = "rukmini23@yahoo.com"

i receive an email.

but then when i take it out it and run it i get this error msg:

At least one recipient is required, but none were found.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: At least one recipient is required, but none were found.

Source Error:


Line 70:             objMailMsg.Body = "blah blah"
Line 71:
Line 72:             SmtpMail.Send(objMailMsg)
Line 73:
Line 74:         End If
 
0
 

Author Comment

by:Rpatel108
ID: 13765197
oh and i haven't got anything in the drop folder or any of the other folders either
0
 
LVL 4

Expert Comment

by:vladimir_kalashnikov
ID: 13765719
yeah, that makes sense, if you don't put your address in, it has none to send, since for some reason, the datareader isn't returning any data.  so you need to figure out why the DB isn't returning data.  I think you should just see if you can get any data from the database and then, once that works, we have somewhere to go from.
0
 

Author Comment

by:Rpatel108
ID: 13765869
do you know how i can to get the to string to be outputted to the screen?

0
 

Author Comment

by:Rpatel108
ID: 13765992
ok i got the values to display in a datagrid and i get all the email addresses from the database.
0
 

Author Comment

by:Rpatel108
ID: 13766429
i dont know if this helps but i inserted trace statements to find out what the to string was outputing and i got this:

Unhandled Execution Error
Could not access 'CDO.Message' object.
  at System.Web.Mail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args)
  at System.Web.Mail.CdoSysHelper.Send(MailMessage message)
  at System.Web.Mail.SmtpMail.Send(MailMessage message)
  at test.email.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\test\email.aspx.vb:line 75
  at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
  at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
  at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
  at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
  at System.Web.UI.Page.ProcessRequestMain() 0.065189 0.044272


0
 
LVL 4

Expert Comment

by:vladimir_kalashnikov
ID: 13766758
to output to the screen just try:

Response.Write(toString)

this just outputs the string to the top of the html document

so now that you can get the values, try using the same sql and get them in a datareader, then just try this outputing them in a while loop:

while dr.Read
    Response.Write(dr("EmailAddress"))
0
 

Author Comment

by:Rpatel108
ID: 13767770
my friend helped me and found that the toString wasn't assigning to the data from the database. so it was just sending "" which was blank.  then i looked at your earlier comment and you had written it the right way i must have done something somewhere and accidentally deleted it...my bad

do you know how to get it to increment through the database so that it loops through the code? i need some sort of for loop

0
 
LVL 4

Expert Comment

by:vladimir_kalashnikov
ID: 13768341
now i see what you mean, simple mistake.

and it is easy to loop through the rows, i can show you in C# (i don't know the syntax for looping in VB), but it is the same idea.

// this loops through all of the rows, each time it calls Read(), it gets the next row.  It stops reading and exits the loop when it gets to the end.  This is the way to read from datareaders.
while (dr.Read())
{
    dr["EmailAddress"].ToString();  // You do stuff with the row here
}
0
 

Author Comment

by:Rpatel108
ID: 13768415
do u know what it means when the emails are queued?
0
 
LVL 4

Expert Comment

by:vladimir_kalashnikov
ID: 13768552
it means that there are a bunch of messages to send and they get put into a line (also known as a queue).  they get sent sequentially.
0
 

Author Comment

by:Rpatel108
ID: 13770864
it seems to be working when its not temper mental! Thanks for your help, you've been really helpful and a good teacher - Thanks
0
 
LVL 4

Expert Comment

by:vladimir_kalashnikov
ID: 13774401
np

Good luck
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Integration Management Part 2
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question