Saturday, March 7, 2009

Sending Email Through ASP.NET using C#

The following Sample Code is the code which can be used to send mails through ASP.Net C#.... its a working code for me.....

[code]
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.
WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//Calling the function SendMail
Response.Write( SendMail("meetuchoudhary@gmail.com","meetudmeet@gmail.com","meetudmeet@yahoo.com","Test Mail","Test Mail Body"));
}

public string SendMail(string toList, string from, string ccList, string subject, string body)
{
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
try
{
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(toList);
if (ccList != null && ccList != string.Empty)
message.CC.Add(ccList);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
smtpClient.Host = "mail.server.com";
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential("info@server.com", "password");

smtpClient.Send(message);
msg = "Successful";
}
catch (Exception ex)
{
msg = ex.Message;
}
return msg;
}

}
[/code]


--
Thanks and Regards
Meetu Choudhary

Overview of ASP.Net Framework

ASP.NET and the .NET Framework

ASP.NET is part of the Microsoft .NET Framework. To build ASP.NET pages, you need to take advantage of the features of the .NET Framework. The .NET Framework consists of two parts: The Framework Class Library (FCL) & The Common Language Runtime (CLR).

Understanding the Framework Class Library

The .NET Framework contains thousands of classes that you can use when building an application. The Framework Class Library was designed to make it easier to perform the most common programming tasks.
Some Examples are

File class Enables you to represent a file on your hard drive.

Graphics class Enables you to work with different types of images such as GIF, PNG, BMP, and JPEG images.

These are some examples of classes in the Framework. The .NET Framework contains almost 13,000 classes you can use when building applications.

You can view all the classes contained in the Framework by opening the Microsoft .NET Framework SDK documentation and expanding the Class Library node

Subscribe via email

Enter your email address:

Delivered by FeedBurner

MSDotnetMentor

MSDotnetMentor My Website http://msdotnetmentor.com