Sending Email in .net 2005

Hi everyone…

Follwoing is very handy class to send emails using .net 2005.

It was created using existing .net class and namespace and it exposes all the functionality via public properties and functions.

public class SendEMail
{
#region Private Variable
private string _smtpServer;
private string _message;
private string _fileAttachment;
private MailMessage _mailMessage;
private MailAttachment _attachments;
#endregion
#region Public Properties
public string ToAddress
{
set { _mailMessage.To = value; }
get { return _mailMessage.To; }
}
public string CCAddress
{
set { _mailMessage.Cc = value; }
get { return _mailMessage.Cc; }
}
public string BCCAddress
{
set { _mailMessage.Bcc = value; }
get { return _mailMessage.Bcc; }
}
public string FromAddress
{
set { _mailMessage.From = value; }
get { return _mailMessage.From; }
}
public string SMTPServer
{
set { _smtpServer = value; }
get { return _smtpServer; }
}
public string Message
{
set { _mailMessage.Body = value; }
get { return _mailMessage.Body; }
}
public string Subject
{
set { _mailMessage.Subject = value; }
get { return _mailMessage.Subject; }
}
public string FileAttachment
{
set { _fileAttachment = value; }
get { return _fileAttachment; }
}
#endregion
#region Constructors
public SendEMail()
{
_mailMessage = new MailMessage();
}
#endregion
#region Public Function
public bool SendMail()
{
try
{
if (ToAddress == null || ToAddress == string.Empty)
{
return false;
}
if (Message == null || Message == string.Empty)
{
return false;
}
if (FromAddress == null || FromAddress == string.Empty)
{
return false;
}
if (SMTPServer == null || SMTPServer == string.Empty)
{
SMTPServer = “localhost”;
}
SmtpMail.SmtpServer = SMTPServer;
_mailMessage.BodyFormat = MailFormat.Html;
_mailMessage.Priority = MailPriority.High;
if (FileAttachment != null && FileAttachment != string.Empty)
{
_attachments = new MailAttachment(FileAttachment);
_mailMessage.Attachments.Add(_attachments);
}
SmtpMail.Send(_mailMessage);
return true;
}
catch (Exception ex)
{
return false;
}
}
#endregion
}

keep smiling 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: