C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

System Net Mail


Posted Date: 13 Oct 2009    Resource Type: Articles    Category: .NET Framework
Author: LaljiMember Level: Diamond    
Rating: 1 out of 5Points: 3



System.Net.Mail is the namespace used to send email if you are using the 2.0 (or higher) .NET Framework.

Unlike System.Web.Mail, which was introduced in the 1.0 Framework, it is not built upon the CDO/CDOSYS libraries. Instead it is written from the ground up without any interop. Thus, it is not dependant upon other COM libraries. System.Net.Mail introduces brand new classes for creating and sending email.

Although some functionality has been removed, the new System.Net.Mail namespace is much more versatile than the older CDO dependant System.Web.Mail.

Example
-------
using System;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.ComponentModel;
namespace Utils.Mail
{
public class MailSender
{
public static string mailStatus = String.Empty;
public static event EventHandler NotifyCaller;
protected static void OnNotifyCaller()
{
if (NotifyCaller != null) NotifyCaller(mailStatus, EventArgs.Empty);
}

public static bool SendMailMessage(string SMTPServer, string fromAddress,
string fromName, string toAddress, string toName, string msgSubject, string msgBody)
{
try
{
SmtpClient client = new SmtpClient(SMTPServer);
MailAddress from = new MailAddress(fromAddress, fromName);
MailAddress to = new MailAddress(toAddress, toName);
MailMessage message = new MailMessage(from, to);
message.Subject = msgSubject;
message.Body = msgBody;
client.Send(message);
}
catch(System.Net.Mail.SmtpException )
{
throw;
}
catch(Exception )
{
throw;
}
return true;
}


public static void SendMailMessageAsync(string SMTPServer, MailMessage message)
{
try
{
SmtpClient client = new SmtpClient(SMTPServer);
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
client.SendAsync(message, message.To.ToString());
}
catch (System.Net.Mail.SmtpException)
{
throw;
}
catch (Exception)
{
throw;
}
}

public static bool SendMailMessage(string SMTPServer, MailMessage message)
{
try
{
SmtpClient client = new SmtpClient(SMTPServer);
client.Send(message);
}
catch (System.Net.Mail.SmtpException)
{
throw;
}
catch (Exception)
{
throw;
}
return true;
}


public static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
String token = (string)e.UserState;
if (e.Cancelled)
{
mailStatus = token + ” Send canceled.”;
}
if (e.Error != null)
{
mailStatus=”Error on “+ token + “: ” +e.Error.ToString();
}
else
{
mailStatus = token + ” mail sent.”;
}
OnNotifyCaller();
}
}
}



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
System Net Mail  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Object Lifetime In .Net - Part 1
Previous Resource: Create two dimensional Chart using c#
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use