# Sending Multiple Emails At Once

Just like setting up emailing from a form, in general, is not difficult, neither is sending emails to multiple people at the same time. First, we need to dimension a variable called 'MyVar': to hold all the email addresses in a string:
Dim MyVar as String
In 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':

Dim MySQL as string = "Select email from yourTableName"
Dim MyConn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("UrAppString"))
Dim Cmd as New SQLCommand(MySQL, MyConn)
MyConn.Open()
MyVar=""
MyVar+=objDR("email")& ";"
End While
MyVar=MyVar.substring(0,(MyVar.Length-1))
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.

You might want to check out another Tutorial here, called 'Emailing Form Results'. It goes over much of this next section also.

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)
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.

Hopefully, this takes a little mystery out of the whole 'sending multiple emails at the same time' scenario.

• 本文已收录于以下专栏：

举报原因： 您举报文章：Sending Multiple Emails At Once 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)