...

Sending Newsletter

CurtWRC
01-31-2006, 10:23 PM
On a website I am building I have created a form where the user can enter their email address into an Access DB. I would then like to send a mass email to all the addresses in the DB. However I can't find out how to do this. One way I tried was by binding a checkboxlist (id="mailinglist") then the emails that have been checked are sent an email. However when I tried this by using this code:

oMessage.To = Mailinglist.SelectedItem.Text

Only the first email on the list was sent the email.

Can someone please tell me how to get round this?

Thanks,
Curt.

Nischumacher
02-04-2006, 11:43 AM
you cannot use SelectedItem for a checkboxlist...
you have to loop though each item and ckeck if it is selected...

CurtWRC
02-16-2006, 01:28 PM
you cannot use SelectedItem for a checkboxlist...
you have to loop though each item and ckeck if it is selected...
How would I do this?

Nischumacher
02-16-2006, 03:57 PM
try this
Dim li As ListItem
For Each li In CheckBoxList1.Items
If li.Selected = True Then
... do something ...
End If
Next

CurtWRC
02-16-2006, 08:10 PM
What would I put where it says: '...do something...'.
Apparently I can't put my whole button sub-routine in there as it wouldn't work.

Brandoe85
02-16-2006, 08:19 PM
How is your table set up? Do you have a specific table for the mailing list, and you store all the information in there? Or, is it apart of your users table, and you set a email flag on?

Either way, you should be able to do it with your query.

Select all of your fields from your table. For each person in the result send them an email.

Good luck;

CurtWRC
02-16-2006, 08:36 PM
I use a table called 'Mailing'. On the homepage (http://www.rallystuff.net/lomax/) there is a form to join the newsletter. This table has three columns. ID, Email, Date.

Brandoe85
02-16-2006, 08:41 PM
So you should be able to send an email to everyone in that table, what is the need for the checkboxes?

Select all the emails from the table and send them a message.

Good luck;

CurtWRC
02-16-2006, 08:47 PM
So you should be able to send an email to everyone in that table, what is the need for the checkboxes?

Select all the emails from the table and send them a message.

Good luck;
I was using checkboxes as a way of selecting the emails as I don't know what to put here otherwise:

oMessage.To = ???

What would you put where I have '???' in this case then?

Thanks.

Brandoe85
02-16-2006, 08:54 PM
Select them in your query.

Set up a mail function, have it take an email address as a parameter. Then, when you execute your query, loop through all of your email addresses passing that email address from the table to the function.

Or am I making no sense?

CurtWRC
02-16-2006, 09:22 PM
Select them in your query.

Set up a mail function, have it take an email address as a parameter. Then, when you execute your query, loop through all of your email addresses passing that email address from the table to the function.

Or am I making no sense?
You probably are making sense, but I haven't done much coding like this :).

Would you be able to give me an example code if its not too long please?

Thanks.

Brandoe85
02-16-2006, 09:25 PM
Sure, just have to check, are you using VB or C#?

Brandoe85
02-16-2006, 10:45 PM
I'll assume VB :)

Sub to get the emails from the table:

Private Sub Email()
Dim strConString = "your connection string"
Dim strQuery As String = "SELECT EMAIL FROM MAILING"
Dim objCon As New OleDbConnection(strConString)
Dim objCommand As New OleDbCommand(strQuery, objCon)
objCon.Open()
Dim reader As OleDbDataReader
reader = objCommand.ExecuteReader()
While reader.Read()
Me.SendEmail(reader.GetString(0))
End While
reader.Close()
objCon.Close()
End Sub


Sub to send the emails:

Private Sub SendEmail(ByVal strEmail As String)
' do all of your email stuff here
' oMessage.To = strEmail
End Sub


Then call Email() wherever.


Good luck;

CurtWRC
02-17-2006, 11:48 AM
Thanks for your help Brandoe85 :thumbsup:

CurtWRC
02-19-2006, 08:36 AM
I have put the two sub routines you mentioned into my script, and believe I have done that ok, but I don't know how to edit the button sub routine to get it to work correctly:

Sub btnsubmit_Click(ByVal sender As Object, ByVal e As EventArgs)

If Page.IsValid Then

Email()


End If
End Sub

Any ideas where I have gone wrong :confused:

Thanks.

CurtWRC
02-19-2006, 11:55 AM
This code only sends to my email address, which has the ID 1, so I'm guessing the problem is I have to loop it somehow. How would I do that if that is the problem?

Brandoe85
02-19-2006, 05:58 PM
How many records do you have in the table?

Try adding in a Response.Write() in the email sub and see how many emails you get:

Private Sub Email()
Dim strConString = "your connection string"
Dim strQuery As String = "SELECT EMAIL FROM MAILING"
Dim objCon As New OleDbConnection(strConString)
Dim objCommand As New OleDbCommand(strQuery, objCon)
objCon.Open()
Dim reader As OleDbDataReader
reader = objCommand.ExecuteReader()
While reader.Read()
'Me.SendEmail(reader.GetString(0))
Response.Write(reader.GetString(0) & "<br>")
End While
reader.Close()
objCon.Close()
End Sub


Good luck;

CurtWRC
02-19-2006, 07:44 PM
I'm using around 10 records but this will increase depending on who signs up. I tried the code above and it made no difference. Thanks anyway.

Brandoe85
02-19-2006, 08:01 PM
I think you missed my point. The Response.Write() should have printed out each email in your table. What happend when you ran the code?

I have tested on my machine with the Response.Write() and it prints out everything correctly for me.

CurtWRC
02-19-2006, 09:27 PM
I think you missed my point. The Response.Write() should have printed out each email in your table. What happend when you ran the code?

I have tested on my machine with the Response.Write() and it prints out everything correctly for me.
Oh right, I was using Response.Redirect() to redirect to my homepage. Now I have done this all emails are now printed onto the page. I don't understand what this is supposed to do though as I am trying to send the newsletter then automatically redirect to my homepage :confused:.

Thanks,
Curt.

Brandoe85
02-19-2006, 11:03 PM
I just wanted to see if it was pulling the results.

Add a write statement at the end of the SendEmail sub and see if it prints out the email is sending.

Private Sub SendEmail(ByVal strEmail As String)
' do all of your email stuff here
' oMessage.To = strEmail
Response.Write("Sending email to: " & strEmail & "<br>")
End Sub


What is the output of that?

Also, it could be the smtp server is not sending the emails, check your que folder and see if anything is in there.

Good luck;

CurtWRC
02-22-2006, 07:01 PM
Thanks for all your help Brandoe85! :D
This is a code I have been trying to figure out for months

I have voted for you in the codingforums.com nomination thread :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum