AspNet4YouTop 10sDevelopers WorldForums
Home
About
ASPNet Books
ASPNet Sites
ASPNet Hosters
View Articles
Search Articles
Authors
View Forums
 
Quick Menus
HOME
About
AspNet4You Forums
Articles
Authors
Search
Articles Summary
Articles(RSSFeed)
AspNet Books
AspNet Sites
AspNet Hosters
 Top 10 ASP.NET Books 
Professional ASP.NET 1.1
Essential ASP.NET With Examples in C#
ASP.NET Unleashed
Programming Data Driven Web Applications with ASP.NET
Professional ASP.NET Web Services
Beginning ASP.NET 1.1 with Visual C# .NET 2003
Programming Microsoft ASP.NET
Beginning ASP.NET Databases Using VB.NET
ASP.NET Security
Developing Microsoft ASP.NET Server Controls and Components
More...
 Top 10 ASP.NET Hosters 
WebHost4Life
DiscountASP.NET
MaximumASP
Brinkster
ORCS Web
myhosting.com
ISQSolutions
ASPwebhosting.com, LLC
Active ISP
Aquest Hosting
More...
 Top 10 ASP.NET Sites 
Asp.Net
GotDotNet
4GuysFromRolla.com
123aspx.com
EggHeadCafe.com
CShrp.Net
.NET 247
DevelopersDex.com
Csharp-Corner.com
dotnetspider
More...
Search Articles
Google
ASPNET4YOU      
Category:   Search Type:   Match Type:  
How to send EMail using System.Web.Mail (in C#)
Author: Saha, ProdipPosted: 3/2/2005 8:10:20 PM

How to send EMail using System.Web.Mail (in C#)

Sending SMTP email from a .Net Framework installed server is very easy. You need not to use any 3rd-party component since it is very simple and the Web Servers (that supports .Net Framework) already has the built-in class System.Web.Mail installed on them. All you have to know is - SMTP server name or ip address. If you are trying to send email from your website, you may be restricted to use a fixed email address in the From field.

Here is the complete implementation of the SMTPMail class. Use the SendSMTPMail method and supply all the parameters, and the email will be on your way.

using System;
using System.Web.Mail;

namespace ASPNET4YOU.COM
{
	public class SMTPMail
	{
		public SMTPMail()
		{
			//
			// TODO: Add constructor logic here
			//
		}


		public bool SendSMTPMail(string SMTPServerName, string strFrom, string strTo, string strSubject, string strBody, string strCC, string strBCC, string strAttachmentList, bool blnUseHTML)
		{
			bool blnTemp=false;
			try
			{
				MailMessage objMsg = new MailMessage();
				objMsg.From = strFrom;
				objMsg.To = strTo;
				objMsg.Subject = strSubject;
				objMsg.Body = strBody;
				if (strCC !="")
				{
					objMsg.Cc = strCC;
				}
				if (strBCC !="")
				{
					objMsg.Bcc = strBCC;
				}
				if(blnUseHTML==true)
				{
					objMsg.BodyFormat=MailFormat.Html;
				}
				else
				{
					objMsg.BodyFormat=MailFormat.Text;
				}

				//A list of attachment files separated by comma(,)
				if (strAttachmentList != "")
				{
					char[] delim = new char[] {','};
					foreach (string sSubstr in strAttachmentList.Split(delim))
					{
						MailAttachment attachment = new MailAttachment(sSubstr);
						objMsg.Attachments.Add(attachment);
					}
				}
			
				SmtpMail.SmtpServer = SMTPServerName;
				SmtpMail.Send(objMsg);
				blnTemp=true;

				return blnTemp;

			}
			catch(Exception ex)
			{
				throw ex;
			}
		}
	}
}

					

Prodip K. Saha
The Architect of WWW.ASPNET4YOU.COM.

Terms and Conditions