Sending Multiple Emails At Once
Dim MyVar as StringIn inline coding, this would be placed inside the script tag, but outside any Sub or Function.
Next we need to set up the BCC field, grabbing all the emails from a database field called (surprise!) 'email':
The last line merely removes the semi-colon from the end that will naturally be placed there due to the line inside the While section.
Dim MySQL as string = "Select email from yourTableName" Dim MyConn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("UrAppString")) Dim objDR as SQLDataReader Dim Cmd as New SQLCommand(MySQL, MyConn) MyConn.Open() objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection) MyVar="" While objDR.Read() MyVar+=objDR("email")& ";" End While MyVar=MyVar.substring(0,(MyVar.Length-1))
You might want to check out another Tutorial here, called 'Emailing Form Results'. It goes over much of this next section also.
The main sections that needs addressing here are the BCC field, the BODY field, and the TO field. For most, the TO field will be fairly trivial. You don't need any of the people in the email addresses for this one. I usually use a mail back to me, in order to be sure the email actually went out. In the BCC field, you see that the list of emails in the 'MyVar' variable goes here, without double quotes surrounding it, since it's a variable and not an explicit email address. The same goes for the BODY section. If you wanted to define a section of text, and assign it to a variable, before this block of code, then, you would merely put the variable name there, without double quotes. Naturally, the SMTP server is important, since that's the mail server which you assign to send the emails out.
Dim objEmail as New MailMessage objEmail.To="News@YourDomain.com" objEmail.FROM="You@YourDomain.com" objEmail.BCC=MyVar objEmail.SUBJECT="This is my Subject" objEmail.Body="Put text or a variable which represents the text - here" objEmail.BodyFormat = MailFormat.Text SmtpMail.SmtpServer ="mail.YourDomain.com" SmtpMail.Send(objEmail)
Hopefully, this takes a little mystery out of the whole 'sending multiple emails at the same time' scenario.