Knowledgebase: PHP, ASP & CGI/Perl
Sample ASPX script to send emails
Posted by Diego Cousinet on 21 February 2011 10:35 AM

Following is the sample code for sending emails using an ASPX file.

Please create an email.aspx file in your website and put the following contents in it:

<% @Page Language="C#" %>
<% @Import Namespace="System.Net.Mail" %>

String from_email_address = "";
String to_email_address = "";
String smtp_username = "";
String smtp_password = "";
String smtp_server = "";

//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress(from_email_addres);

//set the content
mail.Subject = "This is an email";
mail.Body = "this is a sample body";

//send the message. Replace with your smtp server.
SmtpClient smtp = new SmtpClient(smtp_server);

// Using authentication to connect to above smtp server to send email.
// Replace txtSMTPUser and txtSMTPPass with your appropriate details.
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(smtp_username, smtp_password);
smtp.UseDefaultCredentials = false;
smtp.Credentials = SMTPUserInfo;

Please update the following variables in the script:

  • from_email_address (The email address which you want to display to the recepient as the sender.)
  • to_email_address    (The email address to whom you want to send the email to.)
  • smtp_username   (The smtp username of the server that you are using to send the email.)
  • smtp_password  (The smtp password)
  • smtp_server  (The smtp server)

Disclaimer: Above is a sample code only. Before implementing this code on a production website, please put necessary input validation code to avoid any misuse of this script. Web Drive takes no liability of any misuse of this script as this script is only provided to give some guidance of the technologies involved.

(40 vote(s))
Not helpful

Comments (0)