Home » Categories » Multiple Categories

Sample Code to Send email using ASP.NET 2

The following code is developed in C#:

***************************************

//import the Mail Interface
using System.Net.Mail;

protected void sendMail()
{
     //create the mail message
     MailMessage mail = new MailMessage();
 
     //set the addresses
     mail.From = new MailAddress(txtEmail.Text.Trim());
     mail.To.Add("recipient@recipientAddress.com");
 
     //set the content
     mail.Subject = txtSubject.Text.ToString();
     mail.Body = txtQuestion.Text.ToString();
 
     //send the message
     SmtpClient smtp = new SmtpClient("localhost");
     smtp.Credentials = new NetworkCredential("yourAccount@yourDomain.com", "your_password");
 
     smtp.Send(mail);
}

Please note that you need to use localhost as the SMTP detail as there is no remote connection made between your site and our mail server.
Attachments Attachments
There are no attachments for this article.