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 🙂

Leave a comment