How do I send an email from my ASP.NET page?

Include the System.Web.Mail.MailMessage and the System.Web.Mail.SmtpMail classes to send email from ASPX pages.

Below is a sample of sending email using said classes in C#.

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Mail" %>
<html>
<head>
<title>Mail Test</title>
</head>
<script language="C#" runat="server">
private void Page_Load(Object sender, EventArgs e)
{
try
{
MailMessage mailObj = new MailMessage();
mailObj.From = "xyz@programmersheaven.com";
'mail from address
mailObj.To = "abc@hotmail.com";
'mail to address
mailObj.Subject = "New Article Suggestion";
'mail subject
mailObj.Body = "I am sending a link to this article which is really very interesting";
'mail body
mailObj.BodyFormat = MailFormat.Text;
SmtpMail.SmtpServer = "mail.programmersheaven.com";
'specify mail server name depending on your mail server 
SmtpMail.Send(mailObj);
Response.Write("Mail sent successfully");
}
catch (Exception x)
{
Response.Write("Your message was not sent: " + x.Message);
}
} 
</script>
<body>
<form id="mail_test" method="post" runat="server">
</form>
</body>
</HTML>


Tip

In order to send email through your mail server, you should set the static SmtpServer property of the SmtpMail class to mail-fwd.

Written by Sandeep Mogulla, Webmaster at www.startvbdotnet.com

Index

 
Printer friendly version of the FAQ-ASPNET-Email-Attachments page


Sponsored links

3 Months Free - ASP.NET Web Hosting
3 Months Free & No Setup Fees on ASP.NET 3.5/2.0 Hosting on Windows 2008/2003 Servers ? Click Here!
SFTP components for .NET
Add complete SSH and SFTP support to your .NET framework application
PureCM Software Configuration Management
Version control and integrated issue tracking - powerful and easy to use. Get your FREE trial now!
Six Sigma Certification
100% Online-Six Sigma Certificate from Villanova - Find Out More Now.
Web based bug tracking - AdminiTrack.com
AdminiTrack offers an effective web-based bug tracking system designed for professional software development teams.

Advertisement



Free Magazine

Free Magazines
eWeek The essential technology information source for builders of e-business.... subscribe now

Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.