Let's See the Code…
Now we are going to talk about how emails are sent when a visitor clicks on “Send" button. This example uses ASP.NET 2.0 (C#) coding standard.
Following are some steps you need to follow.
Step 1. Create a class clsEmail by right clicking on the project in solution explorer and add a new class object.
Step 2. Make use your class is using the Net.Mail liabrary. using System.Net.Mail;
Step 3. We now need to create a public method SendEmail within clsEmail class.
public static void SendEmail(string strTo, string strSubject, string strMessage, bool isHTML)
Notice we are going to be passing, for arguments.
strTo = recipient’s email address, strSubject = subject of the email, strMessage = the message body we want to email and isHTML = true / false to indicate if we want this specific email to go out as HTML content type or not.
Step 4. Now write logic within SendEmail method.
MailMessage msg = new MailMessage();
msg.From = new MailAddress(“info@YourWebSiteDomain.com"); msg.To.Add(strTo); //from argument
msg.Subject = strSubject; //from argument
msg.IsBodyHtml = isHTML; //from argument
msg.Body = strMessage; //from argument
SmtpClient smtp = new SmtpClient(“YourSMTPServer"); smtp.Credentials = new System.Net.NetworkCredential(“YourSMTPServerUserName", “YourSMTPServerPassword");
smtp.Send(msg); //send email out
msg.Dispose(); //get rid of the object
In the code above, you would need to change FROM email address to match your web site contact address; you also need to change your SMTP server address and username / password associated with your account / domain.
You can find your SMTP information from your hosting control panel.
Once the code is in clsEmail class, now here is how you can call the method.
clsEmail.SendEmail("Bob@yahoo.com","Hello","Hello Bob, how are you?",True | False);
Thats all you have to do to send the email. Email will go to Bob@yahoo.com with the subject "Hello" and body "Hello Bob, how are you?". If you pass "True" then this body will be HTML content or by passing "False" it will be plain-text.