How to Programatically Send an eMail through ASP.NET 2.0 and C#

Visited 624 times | Submited on 2007-04-20 11:48:45

There are several ways to programatically send an email from a given web site. It depends on what platform and what language you are using.

This code shows the code behind a push button that could be added to a form of type aspx. Just add a push button; label it with the wanted description like "Send message now"; double-click the button; and finally, copy and paste this code to the click event handler of the push button. Surely, the aspx form should have some text boxes, radio buttons, check boxes, combo boxes, lists, and labels asking the user to fill them out. Do not forget the validition control that would help a lot in filtering the wrong input.

protected bool SendMessageAsEmail(){
   bool RetVal = true;
   MailMessage objEmail = new MailMessage();
   objEmail.To.Add("toemail@somewebsite.com");
   objEmail.From = new MailAddress("fromemail@somewebsite.com");
   objEmail.Subject = "Message from a visitor";
   objEmail.Body = "this is the body of the message";
   objEmail.Priority = MailPriority.High; // if you want to change the priority; else, remove this line
   try{
      SmtpClient smtp = new SmtpClient("mailserver.com");
      smtp.Port = 5544; // if port has a specific value; else, remove this line
      smtp.EnableSsl = false;
      smtp.Credentials = new NetworkCredential("emailAccountName", "emailAccountPassword");
      smtp.Send(objEmail);
   }
   catch (Exception ex){
      MessageBox.Show(ex.Message);
      RetVal = false;
   }
   return RetVal;
}


By Andre Showairy



Add your comment

Name:(required)
E-mail address:(optional)
Comment:(required)
Repeat the number for validation: (required)

Browse by Tags:


Related Articles:

Text Link Ads

Statistics

Total 296 articles submitted
Latest submission at January 28, 2008 15:13

Feedback

Use this email below to send us your suggestions and feedback. We value your opinion.
info (at) theitarticles.com