Sending mass e-mails using asp.netby faisal khan.
OverviewThis article is part II of a series of articles on sending emails from ASP.NET. In the part I, we learned how to send simple text / html email messages. What that article did not cover was how to validate user input ie what if user enters nothing or enters invalid information in the HTML form input boxes and how to send mass emails to a list of email addresses without the user knowing that the same email has been sent to other users as well.
Why use mass email? Some of the reasons you May Want To Use Mass EmaiLing Are:
To maintain a newsletter on your web site to keep your web site members informed of any updates or information of interest. To send market information to a list of subscribe.
How does mass emailing work? There are lots of techniques of sending mass emails to users. We'll use the first and most basic one known as "Blind Carbon Copying" or simply "BCC". In this technique, the users that receive the email remain unaware (blind) of any other users receiving this email. Besides, there can be any number of user emails specified in this field and all of them will receive the email (should the email gets sent properly).
. The System.Web.Mail.MailMessage class contains a public property known as "BCC" Any emails separated by semicolons ( ';') referenced by this field will receive the email We'll be using this technique in this article..
Why use form validation? When receiving input from the user through HTML form fields, usually there are certain fields which are * required * and others are optional. For example, In the part I of this article, we asked users to enter information like ' From ',' To ',' MailFormat ',' Subject 'and' Body 'in their respective input fields. To successfully complete the transaction we needed all of this information and that ASP.NET script would have failed had you missed any input field .So how will we validate user input? We'll make use of built-in ASP.NET validator controls to do all the validation for us. They give us great deal of flexibility in handling input validation, we can specify which fields to be validated, what error message to show if user entered invalid information, the ability to display all the error messages as a list to the user and to proceed if all the information has been validated otherwise wait and display the validation summary to the user.
BRIEF SUMMARY OF ASP.NET VALIDATOR Controlsfollowing IS A List of All Asp.net Validator Controls:
SendEmail.aspxOk, let's create the new SendEmail.aspx page for part II of this article. Rename the old "SendEmail.aspx" ASP.NET page that you created in part I of this article to some other name (so that we can create The New and Better Sendemail.aspx Page for this article and create a new "sendemail.aspx" Page in The Same Folder.
Now, Copy / Paste Following Code in IT:
<% @ Page language = "vb" autoeventwireup = "false"%>
<% @ Import namespace = "system.web.mail"%>
Protected Sub Sendemail (Byval E AS Object, ByVal E As Eventargs)
IF isvalid = false kil
EXIT SUB
END IF
DIM Mail as new mailmessage ()
Dim stringarr as string () = getData ()
Mail.to = Request.form ("to")
Mail.From = Request.form ("from")
If Request.form ("Format"). Equals ("text") THEN
Mail.BodyFormat = mailformat.textelse
Mail.BodyFormat = mailformat.html
END IF
DIM I as integer
DIM Buffer As new stringbuilder ()
For i = 0 to stringarr.Length - 1
IF i <> 0 THEN
Buffer.Append (";")
END IF
Buffer.Append (Stringarr (i))
NEXT
Mail.bcc = buffer.toString ()
Mail.subject = Request.form ("Subject")
Mail.Body = Request.form ("Body")
Try
SMTPMAIL.SMTPSERVER = "Localhost"
SMTPMAIL.SEND (MAIL)
Catch exception
Message.text = " "& _
EXMESSAGE & " span>"
EXIT SUB
END TRY
message.Text = "Message Sent ... "href =" "Sendemail.aspx"> Go back
End Sub
Protected function getdata () AS String ()
DIM STRINGARR (3) AS STRING
Stringarr (0) = "YouRemail1@fakeaddress.com"
Stringarr (1) = "YouRemail2@fakeaddress.com"
Stringarr (2) = "YouRemail3@fakeaddress.com"
Return Stringarr
END FUNCTION
script>
Body {Background-Color: White;}
Body, Select {font-family: verdana; font-size: 100%;
TEXTAREA, INPUT {Font-Family: Verdana; Font-Size: 100%;
.stdinput {width: 400;
. Submit {Width: 20; Height: 20;
style>
hEAD>
p>